java使用poi生成excel的步骤-mile米乐体育
使用poi生成excel通常包含一下几个步骤
- 创建一个工作簿
- 创建一个sheet
- 创建一个row对象
- 创建一个cell对象(1个row 1个cell构成一个单元格)
- 设置单元格内容
- 设置单元格样式. 字体 字体大小 是否加粗
- 保存
- 关闭流对象
生成一个工作簿
2010以上格式使用xssfworkbook对象, 2003格式使用hssfworkbook对象, 其他对象操作基本一样.
生成2003格式
public void test1() { hssfworkbook workbook = new hssfworkbook(); cellstyle cellstyle = workbook.createcellstyle(); cellstyle.setborderbottom(borderstyle.thin); cellstyle.setborderleft(borderstyle.thin); cellstyle.setborderright(borderstyle.thin); cellstyle.setbordertop(borderstyle.thin); font font = workbook.createfont(); font.setfontname("宋体"); font.setfontheightinpoints((short) 12); cellstyle.setfont(font); hssfsheet sheet = workbook.createsheet("sheet1"); //设置单元格宽度 sheet.setcolumnwidth(0, 30 * 256); sheet.setcolumnwidth(1, 30 * 256); sheet.setcolumnwidth(2, 30 * 256); row row0 = sheet.createrow(0); cell cell0 = row0.createwww.cppcns.comcell(0); cell0.setcellvalue("序号"); cell0.setcellstyle(cellstyle); cell cell1 = row0.createcell(1); cell1.setcellvalue("姓名"); cell cell2 = row0.createcell(2); cell2.setcellvalue("成绩"); outputstream os = null; try { os = new fileoutputstream("d:\\测试生成2003.xls"); workbook.write(os); os.close(); } catch (exception e) { e.printstacktrace(); } }
生成2010以上格式
@test public void test2() { xssfworkbook workbook = new xssfworkbook(); cellstyle cellstyle = workbook.createcellstyle(); cellstyle.setborderbottom(borderstyle.thin); cellstyle.setborderleft(bordjaeajqmuwerstyle.thin); cellstyle.setborderright(borderstyle.thin); cellstyle.setbordertop(borderstyle.thin); font font = workbook.createfont(); font.setfontname("宋体"); font.恰卡编程网setfontheightinpoints((short) 12); cellstyle.setfont(font); xssfsheet sheet = workbook.createsheet("sheet1"); row row0 = sheet.createrow(0); cell cell0 = row0.createcell(0); cell0.setcellvalue("序号"); cell0.setcellstyle(cellstyle); cell cell1 = row0.createcell(1); cell1.setcellvalue("姓名"); cell cell2 = row0.createcell(2); cell2.setcellvalue("成绩"); outputstream os = null; try { os = new fileoutputstream("d:\\测试生成2010.xlsx"); workbook.write(os); os.close(); } catch (exception e) { e.printstacktrace(); } }
合并单元格
合并单元格在生成excel中算常见的一个场景, 通常先合并单元, 单元格内容居中,并设置单元格边框.
poi合并单元格使用cellrangeaddress类, 构造函数包括4个参数firstrow, lastrow, firstcol, lastcol根据自己需要传入行和列.
public cellrangeaddress(int firstrow, int lastrow, int firstcol, int lastcol) { }
合并单元格后设置边框poi已提供了regionutil静态类, 可直接使用.
cellrangeaddress region = new cellrangeaddress(0, 0, 0, 2); sheet.addmergedregion(region); regionutil.setborderbottom(borderstyle.thin, region, sheet); regionutil.setborderleft(borderstyle.thin, region, sheet); regionutil.setbordertop(borderstyle.thin, region, sheet); regionutil.setborderright(borderstyle.thin, region, sheet);
设置单元格样式
左右居中 上下居中 自动换行
cellstyle.setalignment(horizontalalignment.center); cellstyle.setverticalalignment(verticalalignment.center); cellstyle.setwraptext(true);
使用springmvc/springboot导出excel
@controller @getmapping("/excel2003") public void excel2003(httpservletresponse httpservletresponse){ try { //2010格式设置 //response.setcontenttype("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //2003格式设置 response.setcontenttype("application/vnd.ms-excel"); httpservletresponse.addheader("content-disposition", "attachment;filename=" urlencoder.encode("学生成绩单.xls", "utf-8")); servletoutputstream outputstream = httpservletresponse.getoutputstream(); hssfworkbook workbook = new hssfworkbook(); cellstyle cellstyle = workbook.createcellstyle(); cellstyle.setborderbottom(borderstyle.thin); cellstyle.setborderleft(borderstyle.thin); cellstyle.setborderright(borderstyle.thin); cellstyle.setbordertop(borderstyle.thin); font font = workbook.createfont(); font.setfontname("宋体"); font.setfontheightinpoints((short) 12); cellstyle.setfont(font); hssfsheet sheet = workbook.createsheet("sheet1"); row row0 = sheet.createrow(0); cell cell0 = row0.createcell(0); cell0.setcellvalue("序号"); cell0.setcellstyle(cellstyle); cell cell1 = row0.createcell(1); cell1.setcellvalue("姓名"); cell cell2 = row0.createcell(2); cell2.setcellvalue("成绩"); workbook.write(outputstream); } catch (exception e) { e.printstacktrace(); } }
总结
如果你在web项目中导出excel后,打开excel文件时提示文件已损坏,但是文件还可以打开, 则需要在httpservletresponse上设置响应头, 2003和2010设置方式不同
2003
response.setcontenttype("application/vnd.ms-excel");
2010
response.setcontenttype("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
下载文件名如果包含中文的话需要编码
httpservletresponse.addheader("content-disposition", "attachment;filename=" urlencoder.encode("学生成绩单.xls", "utf-8"));
这种设置在浏览器里下载文件中文是没问题的, 只是如果你使用swagger或者postman测试的话,文件名还是经过编码的, 这个没问题说明文件下载已经没问题.
到此这篇关于java使用poi生成excel的文章就介绍到这了,更多相关java生成excel内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!