博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SXSSFWorkbook使用
阅读量:7057 次
发布时间:2019-06-28

本文共 2695 字,大约阅读时间需要 8 分钟。

// 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);
}
}

转载于:https://www.cnblogs.com/523823-wu/p/8780957.html

你可能感兴趣的文章
C++——虚函数表解析
查看>>
重磅!共享单车漏洞独家发布。
查看>>
html中特殊符号
查看>>
为什么 SharedPreferences 可以直接 调用,前面却没有对象
查看>>
php fsockopen 中多线程的解决办法
查看>>
yii框架后台过滤器的使用 安全防护
查看>>
[nginx]lua操作redis
查看>>
第四章 串和数组 (主要kmp算法)
查看>>
laravel 把对象换为数组以及 foreach循环遍历
查看>>
mongodb的docker化安装
查看>>
R笔记
查看>>
【数据结构-ZZU】01. 绪论
查看>>
剑指offer——面试题23:链表中环的入口节点
查看>>
X-Frame-Options,X-XSS-Protection,X-Content-Type-Options
查看>>
iOS下的多种字符串加密方式
查看>>
新的技术点-----每天更新
查看>>
[Leetcode]628. Maximum Product of Three Numbers
查看>>
Linux网络编程(四)
查看>>
Python定制类(进阶6)
查看>>
关于web系统整体优化提速总结
查看>>