poi导出excel ,浏览器直接打开

poi导出excel ,浏览器直接打开工具类 public static void exportExcelR HttpServletR response String fileName List sheetNames List list columnLists List list list gt dataLists throws Exception list list list

大家好,我是讯享网,很高兴认识大家。

//工具类

public static void exportExcelResponse(HttpServletResponse response, String fileName, List sheetNames,List<List> columnLists, List<List<List>> dataLists)throws Exception{
//声明输出流
OutputStream os = response.getOutputStream();
// OutputStream os = new FileOutputStream(fileName+“.xlsx”);
//设置响应头
setResponseHeader(response,fileName);
try {
//获取输出流
// os = response.getOutputStream();
//内存中保留1000条数据,以免内存溢出,其余写入硬盘
HSSFWorkbook wb = new HSSFWorkbook();


讯享网

 for (int k = 0; k < sheetNames.size(); k++) { //获取该工作区的第一个sheet Sheet sheet1 = wb.createSheet(sheetNames.get(k)); int excelRow = 0; //创建标题行 Row titleRow = sheet1.createRow(excelRow++); for(int i = 0;i<columnLists.get(k).size();i++){ //创建该行下的每一列,并写入标题数据 Cell cell = titleRow.createCell(i); cell.setCellValue(columnLists.get(k).get(i)); } //设置内容行 if (dataLists.get(k) != null && dataLists.get(k).size() > 0) { //外层for循环创建行 for (int i = 0; i < dataLists.get(k).size(); i++) { Row dataRow = sheet1.createRow(excelRow++); //内层for循环创建每行对应的列,并赋值 for (int j = 0; j < dataLists.get(k).get(0).size(); j++) { Cell cell = dataRow.createCell(j); cell.setCellValue(dataLists.get(k).get(i).get(j)); } } } } //将整理好的excel数据写入流中 wb.write(os); } catch (IOException e) { e.printStackTrace(); } finally { try { // 关闭输出流 if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } } 

讯享网

//测试
public static void main(String[] args) {
List sheetNames=new ArrayList<>();
sheetNames.add(“项目文件统计”);
sheetNames.add(“质疑列表”);

讯享网 List<List<String>> titles=new ArrayList<>(); titles.add(titleList); titles.add(questionTitleList); List<List<List<String>>> datas=new ArrayList<>(); datas.add(dataLists.get(0)); datas.add(questionDataLists.get(0)); ExcelUtils.exportExcelResponse(response, filepath, sheetNames, titles, datas); } 
小讯
上一篇 2025-03-24 17:40
下一篇 2025-01-09 17:16

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/38256.html