// 1.导入excel模板
String targetPath = ().getServletContext()
.getRealPath("/excelModel/" + fileName + ".xlsx");
// 2.创建一个workbook,对应一个Excel文件
File fi = new File(targetPath);
FileInputStream is = new FileInputStream(fi);
XSSFWorkbook wb = new XSSFWorkbook(is);
int lastRowNum=(0).getLastRowNum();
if(("横向明细")){
(0).getRow(3).getCell(0).setCellValue("分配期号:"+hxmxFpqh);
}
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(wb,1000);
Sheet sheet = (0);
// 开始填写查找出来的数据
writeExcel(list, sheet ,lastRowNum);
// 下载
tkDownload2(sheetName, sxssfWorkbook, res);
-------------2-------------------
public static void writeExcel(List<?> list, Sheet sheet, int lastRowNum)
throws IOException, IllegalArgumentException,
IllegalAccessException {
Row row;
for (int i = 0; i < (); i++) {
row = ((int) i + lastRowNum + 1);
(0).setCellValue(i + 1);
Object tjb = (i);
Class class1 = (Class) ();
Field[] fs = ();
for (int j = 1; j < ; j++) {
Field f = fs[j];
(true);
Object v = (tjb);
String type = ().toString();
if (v == null || () == "") {
(j).setCellValue("");
} else {
if (("Double") || ("double")) {
(j).setCellValue(
(()));
} else if (("Integer") || ("int")) {
(j).setCellValue(
(()));
}
else if (("String")) {
(j).setCellValue(());
}
}
}
}
}
-------------3---------------------
public static void tkDownload2(String sheetName, SXSSFWorkbook wb,
HttpServletResponse res) throws IOException {
String fileName = sheetName;
ByteArrayOutputStream os = new ByteArrayOutputStream();
(os);
byte[] content = ();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
();
("application/-excel;charset=utf-8");
("Content-Disposition", "attachment;filename="
+ (fileName + ".xlsx", "UTF-8"));// 设置文件名
try (ServletOutputStream out = ();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(out)){
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = (buff, 0, ))) {
(buff, 0, bytesRead);
}
} catch (Exception e) {
//();
("tkDownload failed |{}",()->());
("failed {}", e);
}
}