SXSSFWorkbook使用

// 1.导入excel模板
String targetPath = rs.getSession().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=wb.getSheetAt(0).getLastRowNum();

if(fileName.contains("横向明细")){
wb.getSheetAt(0).getRow(3).getCell(0).setCellValue("分配期号:"+hxmxFpqh);
}
SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(wb,1000);

Sheet sheet = sxssfWorkbook.getSheetAt(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 < list.size(); i++) {
row = sheet.createRow((int) i + lastRowNum + 1);
row.createCell(0).setCellValue(i + 1);
Object tjb = list.get(i);
Class class1 = (Class) tjb.getClass();
Field[] fs = class1.getDeclaredFields();
for (int j = 1; j < fs.length; j++) {
Field f = fs[j];
f.setAccessible(true);
Object v = f.get(tjb);
String type = f.getType().toString();
if (v == null || v.toString() == "") {
row.createCell(j).setCellValue("");
} else {

if (type.endsWith("Double") || type.endsWith("double")) {
row.createCell(j).setCellValue(
Double.parseDouble(v.toString()));
} else if (type.endsWith("Integer") || type.endsWith("int")) {
row.createCell(j).setCellValue(
Double.parseDouble(v.toString()));
}

else if (type.endsWith("String")) {
row.createCell(j).setCellValue(v.toString());
}
}
}
}
}

-------------3---------------------

public static void tkDownload2(String sheetName, SXSSFWorkbook wb,
HttpServletResponse res) throws IOException {
String fileName = sheetName;
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);

byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
res.reset();
res.setContentType("application/vnd.ms-excel;charset=utf-8");

res.addHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(fileName + ".xlsx", "UTF-8"));// 设置文件名

try (ServletOutputStream out = res.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(out)){

byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
//e.printStackTrace();
logger.error("tkDownload failed |{}",()->e.toString());
logger.debug("failed {}", e);
}
}

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

前端性能优化 - 雅虎军规

无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化35条军规,这样对于优化有一个比较清晰的方向.35条军规1.尽量减少HTTP请求个数——须权衡2.使用CDN(内容分发网络)3.为文件头指定Expires或CacheControl,使内容具有缓存性。4.避免空的