poi_word常用操作poi310版本
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
模板
郑州市教育局教学研究室制基本信息表:
效果
郑州市中小学生研究性学习成果
申报表
学校名称:郑州1中
成果名称:1
成果负责人:1
联系电话:145****6767
电子邮箱:**************
郑州市教育局教学研究室制基本信息表:
package com.zzstxx.yjxx.services;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.annotation.Resource;
import ng.StringUtils;
import ermodel.BreakType;
import ermodel.ParagraphAlignment; import ermodel.TextAlignment;
import ermodel.UnderlinePatterns;
import ermodel.XWPFDocument;
import ermodel.XWPFParagraph;
import ermodel.XWPFRelation;
import ermodel.XWPFRun;
import ermodel.XWPFTable;
import ermodel.XWPFTableCell;
import ermodel.XWPFTableRow;
import
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColor ;
import
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts ;
import
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMe asure;
import
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyper link;
import
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; import
org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnder line;
import org.springframework.stereotype.Service;
import com.zzstxx.yjxx.models.YjxxCg;
import com.zzstxx.yjxx.models.YjxxCgExt;
import com.zzstxx.yjxx.models.YjxxStudent;
import com.zzstxx.yjxx.models.YjxxTeacher;
import com.zzstxx.yjxx.models.YjxxUrl;
import com.zzstxx.yjxx.models.YjxxZj;
@Service
public class ExportCgService2 {
@Resource
YjxxCgService yjxxCgService;
@Resource
private YjxxCgExtService cgExtService;
@Resource
private YjxxStudentService studentService;
@Resource
private YjxxTeacherService teacherService;
@Resource
private YjxxZjService zjService;
@Resource
private YjxxUrlService urlService;
private final XWPFDocument docCache = new XWPFDocument();
public void setCg(XWPFDocument doc ,String cgid){ YjxxCg cg = this.getCg(cgid);
this.setCgBase(doc, cg);
List<XWPFTable> tables = doc.getTables();
if(tables.size() > 0){
XWPFTable table = tables.get(0);
List<YjxxTeacher> teachers = this.getTeacher(cgid);
this.setTeacher(table, teachers);
List<YjxxStudent> students = this.getStudent(cgid);
this.setStudent(table, students);
List<YjxxZj> zjs = this.getZj(cgid);
this.setZj(table, zjs);
YjxxCgExt cgExt = this.getCgExt(cgid);
this.setCgExt(table, cgExt);
List<YjxxUrl> urls = this.getUrls(cgid);
this.setUrl(table, urls);
}
}
private void setCgBase(XWPFDocument doc, YjxxCg cg){ XWPFParagraph paragraph = docCache.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("郑州市中小学生研究性学习成果");
run.addCarriageReturn();
run.setTextPosition(100);
run.setText("申报表");
run.setBold(true);
run.setFontSize(18);
run.setFontFamily("方正小标宋简体");
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
paragraph.setSpacingBeforeLines(600);
paragraph.setSpacingAfterLines(300);
doc.setParagraph(paragraph, 0);
XWPFParagraph paragraph2 = docCache.createParagraph();
setBase(paragraph2, "学校名称:", cg.getDwmc());
setBase(paragraph2, "成果名称:", cg.getName());
setBase(paragraph2, "成果负责人:", cg.getFzr());
setBase(paragraph2, "联系电话:", cg.getLxdh());
setBase(paragraph2, "电子邮箱:", cg.getEmail());
paragraph2.setIndentationLeft(800);
paragraph2.createRun().addBreak(BreakType.PAGE);;;
doc.setParagraph(paragraph2, 1);
XWPFTable table = doc.getTables().get(0);
XWPFTableCell cellLB = table.getRow(0).getCell(1);
if("1".equals(cg.getLb())){
XWPFRun runLB =
cellLB.getParagraphs().get(0).createRun();
runLB.setText("✔学科类");
runLB.addBreak();
runLB.setText("□综合类");
runLB.setFontSize(14);
}
else{
XWPFRun runLB =
cellLB.getParagraphs().get(0).createRun();
runLB.setText("□学科类");
runLB.addBreak();;
runLB.setText("✔综合类");
runLB.setFontSize(14);
}
SimpleDateFormat format = new
SimpleDateFormat("yyyy-MM-dd");
String starttime = "";
if(cg.getStarttime() != null)
starttime = format.format(cg.getStarttime());
String endtime = "";
if(cg.getEndtime() != null)
endtime = format.format(cg.getEndtime());
XWPFTableCell cellTime = table.getRow(0).getCell(3);
XWPFRun runTime =
cellTime.getParagraphs().get(0).createRun();
runTime.setText(starttime+"至"+endtime);
runTime.setFontSize(14);
table.getRow(16).getCell(1).setText(cg.getZy());
table.getRow(17).getCell(1).setText(cg.getGjz());
}
public void setBase(XWPFParagraph paragraph,String caption, String value){
XWPFRun run1 = paragraph.createRun();
run1.setText(caption);
run1.setFontSize(15);
run1.setFontFamily("方正小标宋简体");
run1.setTextPosition(60);
XWPFRun run2 = paragraph.createRun();
run2.setText(value);
run2.setFontSize(15);
run2.setFontFamily("方正小标宋简体");
run2.setUnderline(UnderlinePatterns.SINGLE);
run2.addCarriageReturn();
run2.setTextPosition(60);
}
private void setStudent(XWPFTable table, List<YjxxStudent> students){
int rowIndex = 2;
for(YjxxStudent student : students){
XWPFTableRow row = table.getRow(rowIndex);
row.getCell(1).setText(student.getName());
row.getCell(2).setText(student.getSex());
row.getCell(3).setText(student.getAge()+"");
row.getCell(4).setText(student.getGrade());
row.getCell(5).setText(student.getXmfg());
row.getCell(6).setText(student.getBz());
rowIndex++;
}
}
private void setTeacher(XWPFTable table, List<YjxxTeacher> teachers){
int rowIndex = 9;
for (YjxxTeacher teacher : teachers) {
XWPFTableRow row = table.getRow(rowIndex);
row.getCell(1).setText(teacher.getName());
row.getCell(2).setText(teacher.getSubject());
row.getCell(3).setText(teacher.getZc());
row.getCell(4).setText(teacher.getRjnj());
row.getCell(5).setText(teacher.getBz());
rowIndex++;
}
}
private void setZj(XWPFTable table, List<YjxxZj> zjs){ int rowIndex = 13;
for (YjxxZj zj : zjs) {
XWPFTableRow row = table.getRow(rowIndex);
row.getCell(1).setText(zj.getName());
row.getCell(2).setText(zj.getDwzw());
row.getCell(3).setText(zj.getYjzc());
row.getCell(4).setText(zj.getBz());
rowIndex++;
}
}
private void setUrl(XWPFTable table, List<YjxxUrl> urls){ int rowIndex = 15;
XWPFTableCell cell = table.getRow(rowIndex).getCell(1);
if(urls.size() > 0){
YjxxUrl url = urls.remove(0);
XWPFParagraph paragraph =
cell.getParagraphs().get(0);
paragraph.createRun().setText(url.getName()+":");
if(StringUtils.isNotEmpty(url.getUrl())) appendExternalHyperlink(url.getUrl(),url.getUrl(),paragraph );
}
for(YjxxUrl url : urls){
XWPFParagraph paragraph = cell.addParagraph();
paragraph.createRun().setText(url.getName()+":");
if(StringUtils.isNotEmpty(url.getUrl())) appendExternalHyperlink(url.getUrl(),url.getUrl(),paragraph );
}
}
private void setCgExt(XWPFTable table, YjxxCgExt cgExt){
table.getRow(18).getCell(1).setText(cgExt.getXtyj());
table.getRow(19).getCell(1).setText(cgExt.getYjjz());
table.getRow(20).getCell(1).setText(cgExt.getYjnr());
table.getRow(21).getCell(1).setText(cgExt.getHjgc());
table.getRow(22).getCell(1).setText(cgExt.getJl());
table.getRow(23).getCell(1).setText(cgExt.getFs());
table.getRow(24).getCell(1).setText(cgExt.getCkwx());
table.getRow(25).getCell(1).setText(cgExt.getBz());
}
private YjxxCg getCg(String cgid){
return yjxxCgService.findByID(cgid);
}
private YjxxCgExt getCgExt(String cgid){
return cgExtService.findByCgID(cgid);
}
private List<YjxxStudent> getStudent(String cgid){
return studentService.findByCgid(cgid);
}
private List<YjxxTeacher> getTeacher(String cgid){
return teacherService.findByCgid(cgid);
}
private List<YjxxZj> getZj(String cgid){
return zjService.findByCgid(cgid);
}
private List<YjxxUrl> getUrls(String cgid){
return urlService.findByCgid(cgid);
}
public void appendExternalHyperlink(String url, String text, XWPFParagraph paragraph) {
try{
// Add the link as External relationship
String id = paragraph
.getDocument()
.getPackagePart()
.addExternalRelationship(url,
XWPFRelation.HYPERLINK.getRelation()).getId();
// Append the link and bind it to the relationship
CTHyperlink hyperLink =
paragraph.getCTP().addNewHyperlink();
hyperLink.setId(id);
// Create the linked text
CTText ctText = CTText.Factory.newInstance();
ctText.setStringValue(text);
CTR ctr = CTR.Factory.newInstance();
ctr.setTArray(new CTText[] { ctText });
CTRPr rpr = ctr.addNewRPr();
// 设置超链接样式
CTColor color = CTColor.Factory.newInstance();
color.setVal("0000FF");
rpr.setColor(color);
rpr.addNewU().setVal(STUnderline.SINGLE);
// 设置字体
CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr .addNewRFonts();
fonts.setAscii("微软雅黑");
fonts.setEastAsia("微软雅黑");
fonts.setHAnsi("微软雅黑");
// 设置字体大小
CTHpsMeasure sz = rpr.isSetSz() ? rpr.getSz() :
rpr.addNewSz();
sz.setVal(new BigInteger("24"));
// Insert the linked text into the link
hyperLink.setRArray(new CTR[] { ctr });
}catch(Exception e){
paragraph.createRun().setText(url);
}
}
}
public void saveCgAsWord(String realPath,String cgid) throws IOException{
//成果文件夹
String folder=realPath+"/files/CG/";
File cgFolder=new File(folder);
if (!cgFolder.exists()) {
cgFolder.mkdirs();
}
String fileName = "cg"+cgid+".docx";
InputStream input = null;
OutputStream out = null;
String templateFile = realPath+"template/yjxxCg2.docx";
try{
input = new FileInputStream(templateFile);
out = new FileOutputStream(folder+"/"+fileName);
XWPFDocument doc = new XWPFDocument(input);
exportCgService2.setCg(doc, cgid);
doc.write(out);
out.flush();
}finally{
if(input != null)
input.close();
if(out != null)
out.close();
}
}。