HSSF EXCEL 导出

1.所需依赖jar包

 1 <dependency>
 2             <groupId>org.apache.poi</groupId>
 3             <artifactId>poi</artifactId>
 4             <version>3.9</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.apache.poi</groupId>
 8             <artifactId>poi-ooxml</artifactId>
 9             <version>3.9</version>
10 </dependency>
View Code

2.样式

 1 public static HSSFCellStyle getGroomHeaderStyle(HSSFWorkbook wb) {
 2         // 设置边框
 3         HSSFCellStyle style = wb.createCellStyle();
 4         style.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
 5         style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
 6         style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
 7         style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
 8 
 9         // 单元格字体
10         HSSFFont f = wb.createFont();
11         f.setFontHeightInPoints((short) 10);// 字号
12         f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 加粗
13         style.setFont(f);
14         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
15         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
16         // style.setRotation(short rotation);//单元格内容的旋转的角度
17 
18         // 设置背景和字体颜色
19 
20         style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
21         style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
22 
23         // 设置自动换行
24         // style.setWrapText(true);
25         return style;
26     }
View Code

3.获取数据源并生成excel

 1   @RequestMapping(value = "/exportSysUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
 2     public void exportSysUserInfo(HttpServletRequest request, HttpServletResponse response, Integer[] userIds, String usernameTx, String loginTx) throws UnsupportedEncodingException {
 3         String filePath = request.getSession().getServletContext()
 4                 .getRealPath("/").replace("\\", "/")
 5                 + "/";
 6         filePath = filePath + ExcelExportUtil.TEMP_DOWN_EXCEL_PATH + "/"
 7                 + ".xls";
 8 
 9         SysUser sysUser = (SysUser) request.getSession().getAttribute("userSession");
10         Integer isAdmin = sysUser.getIsAdmin();
11         Integer comId = sysUser.getComId();
12 
13         try {
14             if (null != usernameTx && !"".equals(usernameTx)) {
15                 if (usernameTx.equals(new String(usernameTx.getBytes("iso8859-1"), "iso8859-1"))) {
16                     usernameTx = new String(usernameTx.getBytes("iso8859-1"), "utf-8");
17                 }
18             }
19 
20             if (null != loginTx && !"".equals(loginTx)) {
21                 if (loginTx.equals(new String(loginTx.getBytes("iso8859-1"), "iso8859-1"))) {
22                     loginTx = new String(loginTx.getBytes("iso8859-1"), "utf-8");
23                 }
24             }
25 
26             List<SysUserExport> sysUserExportList = sysUserService.getListExportSysUser(userIds, usernameTx, loginTx, isAdmin, comId);
27 
28             HSSFWorkbook wb = new HSSFWorkbook();
29             // 工作簿
30             HSSFSheet sheet = wb.createSheet("科技人员信息");
31 
32             // 单元格样式
33             HSSFCellStyle cellStyle = ExcelExportUtil.getGroomCellStyle(wb);
34             // 工作簿 头部 标题样式
35             HSSFCellStyle headStyle = ExcelExportUtil.getGroomHeaderStyle(wb);
36             // 创建表格头部
37             createHeaderRow(sheet, headStyle);
38 
39             HSSFRow row = null;
40             // 添加数据
41             for (int i = 0; i < sysUserExportList.size(); i++) {
42                 SysUserExport sysUserExport = sysUserExportList.get(i);
43 
44                 //出生日期
45                 sysUserExport.setBirthday(sysUserExport.getBirthday().substring(0, 10));
46 
47                 row = sheet.createRow(i + 1);
48                 ExcelExportUtil.createCell(row, 0, cellStyle, sysUserExport.getUsername());//用户名
49                 ExcelExportUtil.createCell(row, 1, cellStyle, sysUserExport.getGender());//工号(登录名)
50                 ExcelExportUtil.createCell(row, 2, cellStyle, sysUserExport.getBirthday());//工号(登录名)
51                 ExcelExportUtil.createCell(row, 3, cellStyle, sysUserExport.getTelePhone());//电话
52                 ExcelExportUtil.createCell(row, 4, cellStyle, sysUserExport.getPosition());//岗位
53                 ExcelExportUtil.createCell(row, 5, cellStyle, sysUserExport.getNote());//工作职责
54                 ExcelExportUtil.createCell(row, 6, cellStyle, sysUserExport.getComName());//所属组织
55                 ExcelExportUtil.createCell(row, 7, cellStyle, sysUserExport.getDepName());//所属部门
56                 //新增字段
57                 ExcelExportUtil.createCell(row, 8, cellStyle, sysUserExport.getIsComputer());//计算机相关专业
58                 ExcelExportUtil.createCell(row, 9, cellStyle, sysUserExport.getMajorName());//专业名称
59                 ExcelExportUtil.createCell(row, 10, cellStyle, sysUserExport.getEducation());//学历
60                 ExcelExportUtil.createCell(row, 11, cellStyle, sysUserExport.getWorkingYears());//从事银行工作年限
61                 ExcelExportUtil.createCell(row, 12, cellStyle, sysUserExport.getIsBadRecord());//不良记录信息
62                 ExcelExportUtil.createCell(row, 13, cellStyle, sysUserExport.getIsSignAgreement());//签订保密协议
63                 ExcelExportUtil.createCell(row, 14, cellStyle, sysUserExport.getEducationFileName());//学历证书
64                 ExcelExportUtil.createCell(row, 15, cellStyle, sysUserExport.getJobFileName());//工作简历
65 
66             }
67 
68             FileOutputStream fos = new FileOutputStream(filePath);
69             wb.write(fos);
70             fos.close();
71         } catch (FileNotFoundException e) {
72             log.info("导出找不到文件!");
73         } catch (IOException e) {
74             log.info("文件流异常!");
75         }
76         // 下载文件
77         ExcelExportUtil.downLoadExcel(request, response, filePath, "科技人员信息");
78     }
View Code

4.生成excel表头

 1     private void createHeaderRow(HSSFSheet sheet, HSSFCellStyle style) {
 2         // 工作表的第0行,表头部分
 3         HSSFRow row = sheet.createRow(0);
 4 
 5         // 设置列宽度,第一个参数代表列id(从0开始),第2个参数代表宽度值 参考 :"2012-08-10"的宽度为2500
 6         sheet.setColumnWidth(0, 3766);
 7         sheet.setColumnWidth(1, 3766);
 8         sheet.setColumnWidth(2, 3766);
 9         sheet.setColumnWidth(3, 4500);
10         sheet.setColumnWidth(4, 3766);
11         sheet.setColumnWidth(5, 10000);
12         sheet.setColumnWidth(6, 6000);
13         sheet.setColumnWidth(7, 6000);
14         //新增字段
15         sheet.setColumnWidth(8, 3766);
16         sheet.setColumnWidth(9, 6000);
17         sheet.setColumnWidth(10, 3766);
18         sheet.setColumnWidth(11, 6000);
19         sheet.setColumnWidth(12, 3766);
20         sheet.setColumnWidth(13, 3766);
21         sheet.setColumnWidth(14, 6000);
22         sheet.setColumnWidth(15, 6000);
23 
24         // 设置行高
25         row.setHeight((short) 500);
26         //人员基本信息
27         ExcelExportUtil.createCell(row, 0, style, "用户名");
28         ExcelExportUtil.createCell(row, 1, style, "性别");
29         ExcelExportUtil.createCell(row, 2, style, "出生日期");
30         ExcelExportUtil.createCell(row, 3, style, "电话");
31         ExcelExportUtil.createCell(row, 4, style, "岗位");
32         ExcelExportUtil.createCell(row, 5, style, "工作职责");
33         ExcelExportUtil.createCell(row, 6, style, "所属组织");
34         ExcelExportUtil.createCell(row, 7, style, "所属部门");
35         //人员详细信息
36         ExcelExportUtil.createCell(row, 8, style, "计算机相关专业");
37         ExcelExportUtil.createCell(row, 9, style, "专业名称");
38         ExcelExportUtil.createCell(row, 10, style, "学历");
39         ExcelExportUtil.createCell(row, 11, style, "从事银行工作年限");
40         ExcelExportUtil.createCell(row, 12, style, "不良记录信息");
41         ExcelExportUtil.createCell(row, 13, style, "签订保密协议");
42         ExcelExportUtil.createCell(row, 14, style, "学历证书");
43         ExcelExportUtil.createCell(row, 15, style, "工作简历");
44     }
View Code

转载于:https://www.cnblogs.com/HotDogHome/p/9468434.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java Excel 导出工具类的示例: ```java import java.io.FileOutputStream; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelUtil { /** * 导出Excel * @param title 表格标题 * @param headers 表头 * @param dataset 数据集合 * @param out 输出流 */ public static void exportExcel(String title, String[] headers, List<Object[]> dataset, FileOutputStream out) { try { // 创建工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作表 HSSFSheet sheet = workbook.createSheet(title); // 设置表格默认列宽度为15个字符 sheet.setDefaultColumnWidth(15); // 创建标题行 HSSFRow row = sheet.createRow(0); HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建标题单元格 HSSFCell cell; for (int i = 0; i < headers.length; i++) { cell = row.createCell(i); cell.setCellValue(headers[i]); cell.setCellStyle(style); } // 填充数据 for (int i = 0; i < dataset.size(); i++) { Object[] objArr = dataset.get(i); row = sheet.createRow(i + 1); for (int j = 0; j < objArr.length; j++) { // 创建数据单元格 row.createCell(j).setCellValue(String.valueOf(objArr[j])); } } // 输出Excel文件 workbook.write(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 使用示例: ```java public class TestExcelUtil { public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream("D:/test.xls"); String[] headers = { "姓名", "年龄", "性别" }; List<Object[]> dataset = new ArrayList<Object[]>(); dataset.add(new Object[] { "张三", 20, "男" }); dataset.add(new Object[] { "李四", 22, "女" }); dataset.add(new Object[] { "王五", 25, "男" }); ExcelUtil.exportExcel("学生信息表", headers, dataset, out); System.out.println("导出成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 该示例使用 Apache POI 库来实现 Excel 导出功能,并且支持设置表格标题、表头、数据集合以及输出流等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值