1.导入pom依赖

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.14</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.14</version>
</dependency>

2. Jakarta POI HSSF API组件

HSSF(用于操作Excel的组件)提供给用户使用的对象在rg.apache.poi.hssf.usermodel包中,主要部分包括Excel对象,样式和格式,还有辅助操作。有以下几种对象:

常用组件:HSSFWorkbook                      excel的文档对象HSSFSheet                         excel的表单HSSFRow                           excel的行HSSFCell                          excel的格子单元HSSFFont                          excel字体HSSFDataFormat                    日期格式HSSFHeader                        sheet头HSSFFooter                        sheet尾(只有打印的时候才能看到效果)样式:HSSFCellStyle                       cell样式辅助操作包括:HSSFDateUtil                        日期HSSFPrintSetup                      打印HSSFErrorConstants                  错误信息表

3. 基本操作步骤

首先,理解一下一个Excel的文件的组织形式,一个Excel文件对应于一个workbook(HSSFWorkbook),一个workbook可以有多个sheet(HSSFSheet)组成,一个sheet是由多个row(HSSFRow)组成,一个row是由多个cell(HSSFCell)组成。

基本操作步骤:

private static final String[] excelHeader = {"产品", "资产前端标识", "客户号", "客户姓名","证件号码","贷款账号","合同号", "借据号","贷款金额","贷款期限(月)","担保方式","还款方式", "放款账号","放款日期","到期日期","电话","地址","利率"};@Autowiredprivate YkLendApplyDao ykLendApplyDao;@Autowiredprivate LenderProductInfoService lenderProductInfoService;@Autowiredprivate CustomerInfoServiceImpl customerInfoService;public HSSFWorkbook export(String proCode, Date beginDate, Date endDate){//表头第一行HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet0 = wb.createSheet("report01");HSSFRow row0 = sheet0.createRow(0);HSSFCellStyle headerStyle = getCellStyle(wb, "宋体", true,14, HSSFCellStyle.ALIGN_CENTER);HSSFCell cell0 = row0.createCell(0);cell0.setCellValue("网贷发放明细报表");cell0.setCellStyle(headerStyle);sheet0.addMergedRegion(new CellRangeAddress(0, 0, 0, 17));//表头第二行 机构+日期行//机构0-9HSSFRow row1 = sheet0.createRow(1);HSSFCell cellOrg = row1.createCell(0);HSSFCellStyle orgRowStyle = getCellStyle(wb, "宋体", false,9, HSSFCellStyle.ALIGN_LEFT);cellOrg.setCellValue("填报机构:银行总行");cellOrg.setCellStyle(orgRowStyle);sheet0.addMergedRegion(new CellRangeAddress(1, 1, 0, 8));//日期10-18HSSFCell cellDate = row1.createCell(9);HSSFCellStyle dateRowStyle = getCellStyle(wb, "宋体", false,9, HSSFCellStyle.ALIGN_RIGHT);DateUtil.formatDate(beginDate,"yyyyMMdd");cellDate.setCellValue("发放日期: " + DateUtil.formatDate(beginDate,"yyyyMMdd") + "至" +DateUtil.formatDate(endDate,"yyyyMMdd"));cellDate.setCellStyle(dateRowStyle);sheet0.addMergedRegion(new CellRangeAddress(1, 1, 9, 17));//表头第三行列名HSSFRow row2 = sheet0.createRow(2);HSSFCellStyle headRowStyle = getCellStyle(wb, "宋体", false, 11, HSSFCellStyle.ALIGN_CENTER);for (int i = 0; i < excelHeader.length; i++) {HSSFCell cell = row2.createCell(i);cell.setCellValue(excelHeader[i]);cell.setCellStyle(headRowStyle);}//表内容// 单元格列宽int[] excelHeaderWidth = { 10, 15, 13, 10, 20, 20, 25, 25, 10, 15, 10, 10,20, 10, 10, 15, 20, 10 };// 设置列宽度(像素)for (int i = 0; i < excelHeaderWidth.length; i++) {sheet0.setColumnWidth(i, 256 * excelHeaderWidth[i]);}List<YkLendApply> lendApplyList = ykLendApplyDao.getByLoanTime(beginDate, endDate);LenderProductInfo productInfo = lenderProductInfoService.findByCode(proCode);String lenderProdNo = productInfo.getLenderProdNo();    //产品编号String contInt = productInfo.getContInt().toString();  //利率String productInfoName = productInfo.getName();//产品名HSSFRow row3;if(lendApplyList != null && lendApplyList.size() > 0){for (int i = 0; i < lendApplyList.size(); i++) {row3 = sheet0.createRow(i + 3);YkLendApply lendApply = lendApplyList.get(i);row3.createCell(0).setCellValue(lenderProdNo);row3.createCell(1).setCellValue(productInfoName);row3.createCell(2).setCellValue(lendApply.getCustNo());row3.createCell(3).setCellValue(lendApply.getCustName());row3.createCell(4).setCellValue(lendApply.getIdNo());row3.createCell(5).setCellValue(lendApply.getLnAcNo());row3.createCell(6).setCellValue(lendApply.getContNo());row3.createCell(7).setCellValue(lendApply.getIouNo());row3.createCell(8).setCellValue(lendApply.getLoanAmt().toString());row3.createCell(9).setCellValue(lendApply.getLoanMonth());row3.createCell(10).setCellValue("信用");row3.createCell(11).setCellValue("等额本息");row3.createCell(12).setCellValue(lendApply.getPayeeAcNo());row3.createCell(13).setCellValue(DateUtil.formatDate(lendApply.getLoanTime(),"yyyyMMdd"));row3.createCell(14).setCellValue(lendApply.getEndLoanDate());row3.createCell(15).setCellValue(lendApply.getMobile());row3.createCell(16).setCellValue(customerInfoService.getByCustNo(lendApply.getCustNo()).getAddress());row3.createCell(17).setCellValue(contInt);row3.setRowStyle(dateRowStyle);}}return wb;}private HSSFCellStyle getCellStyle(HSSFWorkbook wb,String FontName, boolean blod, int HeightInPoints,short alignment) {HSSFCellStyle style = wb.createCellStyle();HSSFFont font = wb.createFont();font.setFontName(FontName);font.setFontHeightInPoints((short)HeightInPoints);font.setBold(blod);style.setAlignment(alignment);style.setFont(font);return style;}

导出结果:

https://blog.csdn.net/ethan_10/article/details/80335350

http://poi.apache.org/apidocs/dev/org/apache/poi/hssf/usermodel/HSSFWorkbook.html

HSSFWorkbook EXCEL导出相关推荐

  1. java:用HSSFWorkbook实现excel导出

    业务:选定需导出的档案字段,然后导出. 数据库档案字段值表为:信息用户id-档案字段id-档案字段值 已有获取档案信息方法:getFieldValueList(List< Integer> ...

  2. C#中用NPOI的excel导出

    //机构表导出 private static List<User2> amininf = new BLL.Bll().GetUser2s(); //定义数据源导出对象 #region 导出 ...

  3. Apache POI操作Excel导出JAVABEAN对象方法

    2019独角兽企业重金招聘Python工程师标准>>> Apache POI操作Excel导出方法说明 Apache的POI组件是Java操作Microsoft Office办公套件 ...

  4. 开发指南专题十六:JEECG微云快速开发平台Excel导出

    开发指南专题十六:JEECG微云快速开发平台Excel导出 14.3.  Excel导出 导出工具类ExcelExportUtil 提供两个函数 //创建多个Sheet public static H ...

  5. Asp.Net 常用工具类之Office—Excel导出(4)

    开发过程中各类报表导入导出防不胜防,网上也是各种解决方法层出不穷,比如Excel,CSV,Word,PDF,HTML等等... 网上各种导出插件也是层出不穷,NPOI,微软Microsoft.Offi ...

  6. struts2 poi excel 导出

    2019独角兽企业重金招聘Python工程师标准>>> 1.struts2配置文件 <!-- 下载专题库资源分类的excel模板 --> <action name= ...

  7. C# Excel导出超出65536行报错 Invalid row number (65536) outside allowable range (0..65535)

    C# Excel导出超出65536行报错 Invalid row number (65536) outside allowable range (0-65535) 一:报错 Invalid row n ...

  8. C# 常用Excel导出的几种常见方式及实现步骤

    目录 常用Excel导出方式 1.使用 Microsoft Office Interop Excel 组件导出 Excel 文件 2.使用 NPOI 组件导出 Excel 文件 3.使用 EPPlus ...

  9. 压缩包里面excel 导出 (跨行跨列)

    压缩包里面excel 导出 (跨行跨列) 创建好 excel模板  直接读取内容即可的方法 1. List content = new ArrayList(); InputStream istream ...

最新文章

  1. mysql 生成数列_PHP生成器的创建和使用
  2. 乐视1s 安装android6,乐视X500(1S) 安卓6.0最终 魅族Flyme6刷机包 最新6.7.12.29R 紫火20180510更新...
  3. mysql把data移走后报错_【mysql案例】Failedtoopenlog--datadir物理迁移报错
  4. hadoop读取mysql数据_Pyspark连接mysql、hive、hdfs 实例展示
  5. Design Patterns之Adapter Pattern总结
  6. httpd设置HTTPS双向认证
  7. fork()调用使子进程先于父进程被调度
  8. Linux | Ubuntu:十年,十个关键时刻
  9. python入门之装饰器
  10. 多个数字数组_九章算法 | 谷歌面试题:多个数组的交集
  11. 1000米感知能力?!图森无人车说这是他们的最新突破
  12. mbot机器人编程课件_mbot机器人教程创客大赛
  13. 2021财院ACM选拔赛题解
  14. 拜登留学新政:美国读博直接拿绿卡,增加H1B签证限额!但未来留美门槛反而变高了?...
  15. ubantu22与windows相互复制粘贴(详细图文)
  16. 三维旋转(根据转轴和角度)的公式。罗德里格旋转公式
  17. Oracle误删除表空间的恢复
  18. Bolzano-Weierstrass 定理
  19. 开源毕设项目《面向桂林旅游的APP软件设计与开发》
  20. 丁磊进退之道《前程密码》

热门文章

  1. C++ 安妮的宠物小屋 练习
  2. java 一周的第一天_Java获取某年某周的第一天
  3. Word 2010 中的 VBA 入门
  4. 悟空浏览器——青龙羊毛
  5. ​PDF怎么转换成Word?试试这几种好用的转换方法
  6. 软件质量测试雨课堂习题
  7. Java面试题总结-2022版
  8. 09组团队项目-Beta冲刺-3/5
  9. 怎么用python统计字数_Python统计字数的思路详解
  10. vue练手小项目--眼镜在线试戴