Java使用Apache.POI中HSSFWorkbook导出到Excel的实现方法
程序员文章站
2022-07-06 17:11:52
使用apache.poi中hssfworkbook导出到excel,具体内容如下所示:1.引入poi依赖(3.12)依赖如下: o...
使用apache.poi中hssfworkbook导出到excel,具体内容如下所示:
1.引入poi依赖(3.12)
依赖如下:
<dependency> <groupid>org.apache.poi</groupid> <artifactid>poi</artifactid> <version>3.12</version> </dependency>
2.创建实体类(user.java)
package com.kd.nm.entity.pojo; /** * 实体类(user) * * author 小辰哥哥 */ public class user { // 用户编号 private string userno; // 用户名称 private string username; // 年龄 private string age; // 无参构造 public user() { } // 有参构造 public user(string userno, string username, string age) { this.userno = userno; this.username = username; this.age = age; } // get与set方法进行封装 public string getuserno() { return userno; } public void setuserno(string userno) { this.userno = userno; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getage() { return age; } public void setage(string age) { this.age = age; } // 重新tostring方法 @override public string tostring() { return "user{" + "userno='" + userno + '\'' + ", username='" + username + '\'' + ", age='" + age + '\'' + '}'; } }
3.excel相关工具类(excelutil、reflectutil)
package com.kd.nm.util; import java.util.arraylist; import java.util.list; import java.util.map; import org.apache.poi.hssf.usermodel.*; /** * description : excel相关工具类 * * @author: 小辰哥哥 * */ public class excelutil { /** * 生成excel表格 * @param heads 表头内容 * @param data 数据内容 * @return */ public static hssfworkbook createxcel(map<string, string> heads, list data) { // 声明一个工作薄 hssfworkbook workbook = new hssfworkbook(); // 生成一个表格 hssfsheet sheet = workbook.createsheet(); // 生成标题行样式 hssfcellstyle headstyle = creatstyle(workbook, (short) 14); // 生成表格内容样式 hssfcellstyle bodystyle = creatstyle(workbook, (short) 10); // 标题元素 list<string> keys = new arraylist<string>(heads.keyset()); // 像素单位 short px = 1000; // 设置列宽 for (int columnindex = 0; columnindex < keys.size(); columnindex++) { sheet.setcolumnwidth(columnindex, 6 * px); } // 生成表格 for (int rownum = 0; rownum <= data.size(); rownum++) { // 创建行 hssfrow row = sheet.createrow(rownum); for (int cellnum = 0; cellnum < keys.size(); cellnum++) { // 创建列 hssfcell cell = row.createcell(cellnum); // 标题 if (rownum == 0) { cell.setcellstyle(headstyle); cell.setcellvalue(heads.get(keys.get(cellnum))); } else { // 内容 cell.setcellstyle(bodystyle); // 通过反射获取 cell.setcellvalue(reflectutil.getvalue(keys.get(cellnum), data.get(rownum - 1))); } } } return workbook; } /** * 生成样式 * @param workbook * @param size * @return */ public static hssfcellstyle creatstyle(hssfworkbook workbook, short size) { hssfcellstyle style = workbook.createcellstyle(); style.setalignment((hssfcellstyle.align_center)); style.setverticalalignment((hssfcellstyle.vertical_center)); hssffont font = workbook.createfont(); font.setfontheightinpoints(size); font.setfontname("微软雅黑"); style.setfont(font); style.setborderbottom(hssfcellstyle.border_thin); style.setbordertop(hssfcellstyle.border_thin); style.setborderright(hssfcellstyle.border_thin); style.setborderleft(hssfcellstyle.border_thin); return style; } }
package com.kd.nm.util; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.util.reflectionutils; import java.beans.propertydescriptor; import java.lang.reflect.method; /** * 反射工具包 * * @author: 小辰哥哥 */ public class reflectutil { private static final logger logger = loggerfactory.getlogger(reflectutil.class); public static string getvalue(string key, object obj) { string value = ""; try { // 获取当前属性 propertydescriptor pd = new propertydescriptor(key, obj.getclass()); // 获取get方法 method getmd = pd.getreadmethod(); value = getmd.invoke(obj).tostring(); } catch (exception e) { logger.error("获取内容失败!"); e.printstacktrace(); } return value; } public static void setvalue(string key, string value, object obj) { try { // 获取当前属性 propertydescriptor pd = new propertydescriptor(key, obj.getclass()); // 获取set方法 method writemd = pd.getwritemethod(); writemd.invoke(obj, value); } catch (exception e) { logger.error("设置内容失败!"); e.printstacktrace(); } } }
4.后端控制器代码
@requestmapping(value = "/exportexcel",method = requestmethod.get,produces = "application/json") public void exportexcel(httpservletresponse httpservletresponse) throws ioexception { // 表头内容(可在前端设置,通过参数传递进来) key是实体类的属性值,value是表头的lable map<string,string> head = new hashmap<>(); head.put("userno","用户编号"); head.put("username","用户名称"); head.put("age","年龄"); // 表格数据内容,模拟数据库查询出来的数据 list<user> data = new arraylist<>(); data.add(new user("1","小辰哥哥","18")); data.add(new user("2","小猪妹妹","18")); data.add(new user("3","大猪哥哥","18")); // 生成工作薄 hssfworkbook hssfworkbook = excelutil.createxcel(head, data); // 定义文件名 string filename = "导出excel表格"; httpservletresponse.setheader("cache-control", "max-age=0"); httpservletresponse.setcontenttype("application/vnd.ms-excel"); httpservletresponse.addheader("content-disposition", "attachment;filename=" + new string(filename.getbytes("gb2312"), "iso-8859-1") + ".xls"); outputstream outputstream = httpservletresponse.getoutputstream(); hssfworkbook.write(outputstream); outputstream.flush(); outputstream.close(); }
5.访问映射地址
接口访问:
http://localhost:9090/faulttreatment/api/standard/exportexcel
到此这篇关于java使用apache.poi中hssfworkbook导出到excel的实现方法的文章就介绍到这了,更多相关apache.poi中hssfworkbook导出到excel内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!