Java poi导出Excel下载到客户端

网友投稿 370 2023-02-20


Java poi导出Excel下载到客户端

java poi 导出Excel并下载到客户端,具体内容如下

Maven配置,包含了其他文件格式的依赖,就全贴出来了

org.apache.poi

poi-excelant

3.12

org.apache.poi

poi-scratchpad

3.12

org.apache.poi

poi-ooxml

3.8

org.apache.poi

poi-ooxml-schemas

3.8

Service层

@Override

public void export(Long sblsh, String excelName, OutputStream out) {

try {

// 第一步,创建一个webbook,对应一个Excel文件

HSSFWorkbook wb = new HSSFWorkbook();

//生成一个表格

HSSFSheet sheet = wb.createSheet(excelName);

// 第三步,在sheet中添加表头第0行

HSSFRow row = sheet.createRow(0);

// 第四步,创建单元格,并设置值表头 设置表头居中

HSSFCellStyle style = wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

HSSFCell cell = row.createCell(0);

cell.setCellStyle(style);

Byte kjzz = qyjhttp://bxxMapper.getKjzz(sblsh);

List record = this.selectBySblsh(sblsh);

this.insertData(wb, sheet, row, record, out);

}

} catch (Exception e) {

logger.info(e.getMessage());

}

}

/**

* 导入数据到表格中

* @param wb execl文件

* @param sheet 表格

* @param row 表格行

* @param record 要导出的数据

* @param out 输出流

*/

private void insertData(HSSFWorkbook wb,HSSFSheet sheet,HSSFhttp://Row row,List record,

OutputStream out){

try {

row = sheet.createRow(1);

for(int i=0;i

row.createCell(i).setCellValue(title[i]);

}

for(int i=0;i

row = sheet.createRow(i+2);

A data = record.get(i);

row.createCell(0).setCellValue(data.getHc());

row.createCell(1).setCellValue(data.getXm());

BigDecimal je = data.getJe();

if(je!=null){

row.createCell(2).setCellValue(je.doubleValue());

}

}

//合并单元格,前面2位代表开头结尾行,后面2位代表开头结尾列

CellRangeAddress region = new CellRangeAddress(0,0,0,title.length-1);

sheet.addMergedRegion(region);

wb.write(out);

out.flush();

out.close();

wb.close();

} catch (Exception e) {

logger.info(e.getMessage());

}

}

Controller

@RequestMapping("/export")

public void export(Long sblsh, HttpServletRequest request, HttpServletResponse response){

response.setContentType("octets/stream");

String excelName = "文件名";

try {

response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");

OutputStream out = response.getOutputStream();

aService.export(sblsh,excelName ,out);

} catch (Exception e) {

e.printStackTrace();

}

}

row.createCell(i).setCellValue(title[i]);

}

for(int i=0;i

row = sheet.createRow(i+2);

A data = record.get(i);

row.createCell(0).setCellValue(data.getHc());

row.createCell(1).setCellValue(data.getXm());

BigDecimal je = data.getJe();

if(je!=null){

row.createCell(2).setCellValue(je.doubleValue());

}

}

//合并单元格,前面2位代表开头结尾行,后面2位代表开头结尾列

CellRangeAddress region = new CellRangeAddress(0,0,0,title.length-1);

sheet.addMergedRegion(region);

wb.write(out);

out.flush();

out.close();

wb.close();

} catch (Exception e) {

logger.info(e.getMessage());

}

}

Controller

@RequestMapping("/export")

public void export(Long sblsh, HttpServletRequest request, HttpServletResponse response){

response.setContentType("octets/stream");

String excelName = "文件名";

try {

response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");

OutputStream out = response.getOutputStream();

aService.export(sblsh,excelName ,out);

} catch (Exception e) {

e.printStackTrace();

}

}

row = sheet.createRow(i+2);

A data = record.get(i);

row.createCell(0).setCellValue(data.getHc());

row.createCell(1).setCellValue(data.getXm());

BigDecimal je = data.getJe();

if(je!=null){

row.createCell(2).setCellValue(je.doubleValue());

}

}

//合并单元格,前面2位代表开头结尾行,后面2位代表开头结尾列

CellRangeAddress region = new CellRangeAddress(0,0,0,title.length-1);

sheet.addMergedRegion(region);

wb.write(out);

out.flush();

out.close();

wb.close();

} catch (Exception e) {

logger.info(e.getMessage());

}

}

Controller

@RequestMapping("/export")

public void export(Long sblsh, HttpServletRequest request, HttpServletResponse response){

response.setContentType("octets/stream");

String excelName = "文件名";

try {

response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");

OutputStream out = response.getOutputStream();

aService.export(sblsh,excelName ,out);

} catch (Exception e) {

e.printStackTrace();

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:利用spring的拦截器自定义缓存的实现实例代码
下一篇:Vue多种方法实现表头和首列固定的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~