书接上集,我们已经实现了将对应的试题数据转化为Word文档并下载,那如果需要批量下载为压缩包呢?本文就带大家实现这一步。
首先是前端请求如何发送:
var url = "请求地址/方法名?"参数A"+参数A的值+"参数B"参数B的值;
window.open(url, "批量导出试卷");
接下来是controller层:
@GetMapping("/paperBatchExport")
public void paperBatchExport(Comprehensive comprehensive, HttpServletRequest request, HttpServletResponse response)
{
//Comprehensive为你自己的实体类对象
List<Comprehensive> list = comprehensiveTestService.selectComprehensiveList(comprehensive);
List<Map> userPaperFileList = new ArrayList<>();
//遍历根据查询条件获取到的集合
for (Comprehensive item : list) {
//做题记录详情
Comprehensive comprehensive1 = new Comprehensive();
comprehensive1.setSimPaperID(item.getSimPaperID());
comprehensive1.setUserID(item.getUserID());
List<Comprehensive> list1 = comprehensiveTestService.getExamDetail(comprehensive1);
AtomicInteger num = new AtomicInteger(1);
list1.stream().forEach(o->{
o.setSequence(num.get());
num.getAndIncrement();
});
try {
//日期格式转化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String clickTimeStr = sdf.format(item.getClickTime());
byte[] bytes1 = getExamPaperWordOutputBytes(item.getMemberName(),item.getIdentityCardId(),item.getCourseName(),item.getClickTime(),item.getSubmitTime(),item.getSpendTime(),list1);
if (bytes1 == null) {
logger.error("获取试卷字节数组为空,无法导出试卷,bytes1为:" + bytes1);
continue; //跳过当前循环
}
String fileName = item.getMemberName()+clickTimeStr+"试卷.doc";
Map wordOut=new HashMap();
wordOut.put("fileName",fileName);
wordOut.put("outByte",bytes1);
userPaperFileList.add(wordOut);
}catch (Exception e){
e.printStackTrace();
}
}//for循环结束
//调用压缩方法
String fileNameNew = "XXX试卷";
zipFiles(fileNameNew,userPaperFileList,response);
}
getExamPaperWordOutputBytes方法:
/*试卷导出字节流*/
public byte[] getExamPaperWordOutputBytes(String memberName, String identityCardId, String courseName, Date clickTime, Date submitTime, Integer spendTime, List<Comprehensive> list){
byte[] outBytes=null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();//构建字节输出流
Document document = new Document(PageSize.A4); // 创建word文档,并设置纸张的大小
RtfWriter2.getInstance(document, baos);
//开始设置doc样式
document.open();
// 设置字体
BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
// 主标题字体风格
Font titleFont = new Font(font, 20, Font.BOLD);
// 副标题字体风格
Font subTitleFont = new Font(font, 12, Font.BOLD);
// 正文字体风格
Font contextFont = new Font(font, 12, Font.NORMAL);
//大标题
Paragraph title = new Paragraph("试题详情");
// 设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
if (org.apache.commons.lang.StringUtils.isEmpty(identityCardId)) {
identityCardId = " 暂无 ";
}
//日期格式转化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String clickTimeStr = sdf.format(clickTime);
String submitTimeStr = sdf.format(submitTime);
//用户信息
Paragraph userInfoTitle = new Paragraph("学生姓名:" + memberName + " 身份证号码:" + identityCardId + " 课程名称:" + courseName + " 开始做题时间:" + clickTimeStr + " 提交时间:" + submitTimeStr + " 做题时长:" + spendTime + "秒");
// 设置标题格式对齐方式
userInfoTitle.setAlignment(Element.ALIGN_CENTER);
userInfoTitle.setFont(subTitleFont);
document.add(userInfoTitle);
Paragraph tabTitle = new Paragraph("");
document.add(tabTitle);
// 数据输出
int i = 1;//每个题目的序号
//每个题目开始遍历
for (Comprehensive paperQuestion : list) {
Paragraph questionTitle = new Paragraph(i + ". " + paperQuestion.getContent());
// 设置标题格式对齐方式
questionTitle.setAlignment(Element.ALIGN_LEFT);
questionTitle.setFont(contextFont);
document.add(questionTitle);
List<Options> optionList = paperQuestion.getOptions();
//开始每个题目的选项进行遍历
for (Options questionOption : optionList) {
Paragraph optionTitle = new Paragraph(questionOption.getQuesValue() + ". " + questionOption.getQuesOption());
//如果是判断题只要“对”,“错“ 去掉”Y“,”N“
if ("Y".equals(questionOption.getQuesValue()) || "N".equals(questionOption.getQuesValue())) {
optionTitle = new Paragraph(questionOption.getQuesOption());
}
// 设置标题格式对齐方式
optionTitle.setAlignment(Element.ALIGN_LEFT);
if (null != paperQuestion.getUserAnswer() && questionOption.getQuesValue().equals(paperQuestion.getUserAnswer())) {
contextFont.setColor(Color.red);
}
optionTitle.setFont(contextFont);
document.add(optionTitle);
contextFont.setColor(Color.black);
}
Paragraph userAnswerTitle = new Paragraph("您的答案:" + paperQuestion.getUserAnswer());
// 设置标题格式对齐方式
userAnswerTitle.setAlignment(Element.ALIGN_LEFT);
userAnswerTitle.setFont(contextFont);
document.add(userAnswerTitle);
Paragraph answerTitle = new Paragraph("正确答案:" + paperQuestion.getAnswer());
// 设置标题格式对齐方式
answerTitle.setAlignment(Element.ALIGN_LEFT);
answerTitle.setFont(contextFont);
document.add(answerTitle);
Paragraph analysisTitle = new Paragraph("解析:" + paperQuestion.getAnalysis());
// 设置标题格式对齐方式
analysisTitle.setAlignment(Element.ALIGN_LEFT);
analysisTitle.setFont(contextFont);
document.add(analysisTitle);
i++;
document.add(new Paragraph(""));
}
document.close();
//把创建好的word写入到输出流
outBytes = baos.toByteArray();
}catch (Exception e){
e.printStackTrace();
}
return outBytes;
}
zipFiles方法用于将字节文件转化为zip格式:
引入依赖:
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
方法详情:
//多个文件压缩成压缩包并下载
public void zipFiles(String fileNameNew,List<Map> fileList,HttpServletResponse httpResponse) {
try {
ZipOutputStream zipOutputStream = new ZipOutputStream(httpResponse.getOutputStream());
zipOutputStream.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
//下载压缩包
httpResponse.setContentType("application/zip");
httpResponse.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileNameNew+".zip", "UTF-8"));
// 创建 ZipEntry 对象
for (Map map:fileList){
ZipEntry zipEntry = new ZipEntry((String) map.get("fileName"));
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.setEncoding("GBK");
// zipOutputStream.setEncoding("UTF-8");
zipOutputStream.write((byte[]) map.get("outByte"));
zipOutputStream.closeEntry();
}
if(zipOutputStream!=null){
zipOutputStream.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
这样我们就可以把多个Word文件组成一个压缩包并下载辣,但是需要注意的是,最好是将数据分批次进行操作!!!