一、Maven加入aspose.words依赖
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.2</version>
</dependency>
二、后台生成表格并传输到本地
1、前台编写js,使其可以跳转至Controller
location.href="/wordExport/exportScoreList?username=sa&readFlag=1"
2、创建Controller类和处理方法
@RequestMapping("/wordExport")
@RestController
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();
Document document=new Document();
DocumentBuilder builder = new DocumentBuilder(document);
//单元格水平居中对齐
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
//段落居中对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.writeln("考试成绩单");
//段落左对齐
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
String pickname="";
if(testRecordExtList!=null&&testRecordExtList.size()>0){
pickname=testRecordExtList.get(0).getPickname();
}
builder.writeln("姓名:"+pickname);
builder.startTable();
//表头
builder.insertCell();
builder.write("序号");
builder.insertCell();
builder.write("考试");
builder.insertCell();
builder.write("考试时长");
builder.insertCell();
builder.write("成绩");
builder.insertCell();
builder.write("排名");
builder.insertCell();
builder.write("考试类型");
builder.endRow();
//表格内容
for(int i=0;i<testRecordExtList.size();i++){
TestRecordExt testRecordExt=testRecordExtList.get(i);
builder.insertCell();
builder.write((i+1)+"");
builder.insertCell();
builder.write(testRecordExt.getTestName());
builder.insertCell();
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+" 秒";
}
builder.write(testLengthStr);
builder.insertCell();
builder.write(testRecordExt.getTestScore()+"分");
builder.insertCell();
builder.write(testRecordExt.getRankNo()+"");
builder.insertCell();
builder.write(testRecordExt.getTestTypeName());
builder.endRow();
}
builder.endTable();
document.save(outputStream, SaveFormat.DOC);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
最终效果图
三、遇到的问题
1、Maven加入aspose.words依赖出现下载不了jar包的情况
解决请参考下面这条博客
2、将文件下载到本地时,出现文件名中的中文无法显示
3、Word文档中有水印,而且有三个水印
扫描二维码关注公众号,回复:
10655556 查看本文章
![](/qrcode.jpg)
4、Maven加上 aspose.words依赖之后,Spring Boot启动需要10分钟。
有哪位仁兄解决了上面这两个问题请务必告诉我
如果大家对使用poi组件实现Word生成表格并下载至本地感兴趣的话,可以去看我的另一篇博客