个人觉得这个功能实在搞笑,没什么意义,没办法提了需求就要实现,(太好说话了把我)
我的实现是再word中生成一个与 excel行,列 一样的一个表格,然后把从excel拿到的数据(exList参数)依次放到word表格中
public static void createFile(HttpServletResponse response, String fileName, List<List<String>> exList) {
try {
setResponseHeader(response, fileName);
//生成一个word模版文件
XWPFDocument document = new XWPFDocument();
XWPFTable table = document.createTable(exList.size(), exList.get(0).size());
XWPFTableRow row;
for (int i = 0; i < exList.size(); i++) {
List<String> sdf = exList.get(i);
row = table.getRow(i);
for (int j = 0; j < exList.get(i).size(); j++) {
String s =sdf.get(j);
row.getCell(j).setText(s);
row.getCell(j).setWidthType(TableWidthType.AUTO);
}
//将数据插入表格中 pos:0 表示 第一个表格
document.setTable(0,table);
}
ServletOutputStream outputStream = response.getOutputStream();
BufferedOutputStream bufferStream = new BufferedOutputStream(outputStream, 1024);
document.write(bufferStream);
document.close();
bufferStream.close();;
} catch (IOException e) {
e.printStackTrace();
}
}
public static void setResponseHeader(HttpServletResponse response, String name) {
try {
name = new String(name.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("multipart/form-data");
//要保存的文件名
response.setHeader("Content-Disposition", "attachment;filename=" + name + ".docx");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
}