首先我来讲一下需要用到的类:
XWPFDocument代表一个docx文档,其可以用来读docx文档,也可以用来写docx文档
XWPFParagraph代表文档、表格、标题等种的段落,由多个XWPFRun组成
XWPFRun代表具有同样风格的一段文本
XWPFTable代表一个表格
XWPFTableRow代表表格的一行
XWPFTableCell代表表格的一个单元格
一、Maven加入poi依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
二、后台生成表格并传输到本地
1、前台编写js,使其可以跳转至Controller
location.href="/wordExport/exportScoreList?username=sa&readFlag=1"
2、创建Controller类和处理方法
@RestController
@RequestMapping("/wordExport")
public class WordExportController {
@Autowired
private TestRecordService testRecordService;
@GetMapping("/exportScoreList")
public void exportScoreList(HttpServletResponse response,String username,
String testType,String testName,String readFlag){
username=username==null?"":username;
testType=testType==null?"":testType;
testName=testName==null?"":testName;
readFlag=readFlag==null?"":readFlag;
List<TestRecordExt> testRecordExtList=testRecordService.getTestRecordExtList("",username,readFlag,testName,testType);
try {
String fileName="考试成绩单"+ DateUtil.getCurrentTimeByDay()+".doc";
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
OutputStream outputStream=response.getOutputStream();
XWPFDocument document =getTestScoreListWord(testRecordExtList);
document.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
//生成考试成绩单Word文档
public XWPFDocument getTestScoreListWord(List<TestRecordExt> testRecordExtList){
XWPFDocument document = new XWPFDocument();
//第一行,文字居中对齐
XWPFParagraph titleParagraph = document.createParagraph();
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
titleParagraph.createRun().setText("考试成绩单");
//第二行
String pickname="";
if(testRecordExtList!=null&&testRecordExtList.size()>0){
pickname=testRecordExtList.get(0).getPickname();
}
document.createParagraph().createRun().setText("姓名:"+pickname);
//生成表格
XWPFTable table = document.createTable(testRecordExtList.size()+1, 6);
List<XWPFTableRow> rowList = table.getRows();
//表头下标
final int COL_NO=0;//序号
final int COL_TESTNAME=1;//考试
final int COL_TESTRECORDLENGTH=2;//考试时长
final int COL_TESTSCORE=3;//成绩
final int COL_RANKNO=4;//排名
final int COL_TESTTYPENAME=5;//考试类型
//设置表头
List<XWPFTableCell> cellList=rowList.get(0).getTableCells();
//序号
XWPFParagraph parapraph=cellList.get(COL_NO).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText("序号");
//考试
parapraph= cellList.get(COL_TESTNAME).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText("考试");
//考试时长
parapraph=cellList.get(COL_TESTRECORDLENGTH).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText("考试时长");
//成绩
parapraph=cellList.get(COL_TESTSCORE).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText("成绩");
//排名
parapraph=cellList.get(COL_RANKNO).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText("排名");
//考试类型
parapraph=cellList.get(COL_TESTTYPENAME).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText("考试类型");
//设置表格内容
for(int i = 0; i < testRecordExtList.size(); i++) {
TestRecordExt testRecordExt = testRecordExtList.get(i);
cellList = rowList.get(i+1).getTableCells();
//序号
parapraph=cellList.get(COL_NO).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText((i+1)+"");
//考试
parapraph=cellList.get(COL_TESTNAME).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText(testRecordExt.getTestName());
//考试时长
int testRecordLength=testRecordExt.getTestRecordLength();
String testLengthStr="";
int testHour=testRecordLength/3600;
if(testHour>0){
testLengthStr+=testHour+" 小时";
}
int testMinutes=testRecordLength%3600/60;
if(testMinutes>0){
testLengthStr+=testMinutes+" 分钟";
}
int testSecond=testRecordLength%60;
if(testSecond>0){
testLengthStr+=testSecond+" 秒";
}
parapraph=cellList.get(COL_TESTRECORDLENGTH).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText(testLengthStr);
//成绩
parapraph=cellList.get(COL_TESTSCORE).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText(testRecordExt.getTestScore()+"分");
//排名
parapraph=cellList.get(COL_RANKNO).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText(testRecordExt.getRankNo()+"");
//考试类型
parapraph=cellList.get(COL_TESTTYPENAME).getParagraphArray(0);
parapraph.setAlignment(ParagraphAlignment.CENTER);
parapraph.createRun().setText(testRecordExt.getTestTypeName());
}
return document;
}
}
最终效果图
在使用上来讲,相对于aspose.words还是太繁琐了,感兴趣的可以去看我的另一篇博客,是用aspose.words实现Word生成表格并下载至本地。