POI workbook.write(outputStream)无法写入数据

HOLD ON! 2019-01-04 11:02:39

List<VCloTemplate> templates = ts.selectCloTemplate(record); //查询模版记录, 按年份月份

ExcelFactoryTemplateCost<List<VCloTemplate>> excelInstance = new ExcelFactoryTemplateCost<>(templates);

File file = FileUtil.getFile(request.getSession().getServletContext().getRealPath("/")+"xls/" + excelInstance.getExcelAllName()); //创建文件

//创建OutputSream, 写入Excel文件
OutputStream output= null;
try {
output = new FileOutputStream(file);

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("1");
sheet.createRow(0).createCell(0).setCellValue("1");
wb.write(output);

// ??????????????????????????????????????excelInstance.getWorkbook().write();

//返回访问路径
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
String path = basePath+"xls/" + excelInstance.getExcelAllName();

response.setCharacterEncoding("UTF-8");
response.getWriter().write(path);


} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}


这是一个SSM框架中, Controler层编写的写excel的代码
但是, 我尝试了两种方式,
1.在Controller中直接 new HSSFWorkbook()后生成workbook对象, 然后setCellValue()写入数据, 然后workbook.wirte(outputStream)写IO, 导出的excel表中有数据
2.在Controller中通过一个自己写的Util类, 向Util类中传入后台数据, 这个Util类会同样地处理数据, 生成一个workbook对象, 然后我再Controller中通过Util类的方法得到这个workbook对象, 再调用workbook.write(outputStream), 这时导出的excel表中没有数据

就是数据经过Util类处理, 再通过Util类中的方法返回的workbook对象, workbook.write(outputStream)无法将处理后的数据写入excel中
...全文
4192 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
HOLD ON! 2019-10-08
  • 打赏
  • 举报
回复
引用 2 楼 Sever123555 的回复:
你解决这个问题了吗?我现在也遇到这个问题。找了好久资料没有思绪。
所以 sheet的行数和列数 在 每次 for 循环的时候 增加, 所以 for循环 永远 结束 不了
HOLD ON! 2019-10-08
  • 打赏
  • 举报
回复
引用 2 楼 Sever123555 的回复:
你解决这个问题了吗?我现在也遇到这个问题。找了好久资料没有思绪。
之前好像是这么写的

for(int i=0;i<[sheet的行数];i++){
            for (int j=9;j<[这一行的列数];j++){
                this.wb.getSheet(this.excelName).createRow(i).createCell(j).setCellValue("OK");
            }
            System.out.println();
}
  • 打赏
  • 举报
回复
给你参考参考https://blog.csdn.net/mr_zql/article/details/99938908你这代码直接一行解决看得我有点蒙
Sever123555 2019-10-08
  • 打赏
  • 举报
回复
你解决这个问题了吗?我现在也遇到这个问题。找了好久资料没有思绪。
HOLD ON! 2019-01-04
  • 打赏
  • 举报
回复

for(int i=0;i<10;i++){
for (int j=9;j<20;j++){
this.wb.getSheet(this.excelName).createRow(i).createCell(j).setCellValue("OK");
}
System.out.println();
}

for循环中一直 createRowcreateCell..............................

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧