栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

你知道什么是POI吗?

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

你知道什么是POI吗?

java中的POI是什么?

POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊实现”

是用Java编写的免费开源的跨平台的 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

简而言之,就是"进行MsOffice进行读写的这样一个东西“

POI下包结构说明:

HSSF - 提供读写Microsoft Excel格式档案的功能。

XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。

HWPF - 提供读写Microsoft Word格式档案的功能。

HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

HDGF - 提供读写Microsoft Visio格式档案的功能。

本息主要讲我遇到的 Excel操作:

POI提供了HSSF、XSSF以及SXSSF三种方式操作Excel。

(1)HSSF:Excel97-2003版本,扩展名为.xls。一个sheet最大行数65536,最大列数256。

(2)XSSF:Excel2007版本开始,扩展名为.xlsx。一个sheet最大行数1048576,最大列数16384。

(3)SXSSF:是在XSSF基础上,POI3.8版本开始提供的支持低内存占用的操作方式,扩展名为.xlsx。

Excel版本兼容性是向下兼容。

三种类的接口和方法如下:

HSSF:HSSFWorkbook、HSSFSheet、HSSFRow、HSSFCell……

XSSF:XSSFWorkbook、XSSFSheet、XSSFRow、XSSFCell……

SXSSF:SXSSFWorkbook、Sheet、Row、Cell……

很重要的一点,SXSSF之所以是一种低内存操作方式,是因为他的构造方法:

System.out.println(System.getProperty("java.io.tmpdir"));

案例代码:

// TODO: 2021/10/12 导出
@Auth(value = "exportExcel:ohhazard", code = "__YYYYYYY005", description = "导出", disabled = true)
@RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
@ApiOperation(value = "导出Excel", notes = "")
public void exportExcel(@ModelAttribute OhHazard pojo, HttpServletResponse res) throws IOException {
   exportExcel(res,pojo);
}
private void exportExcel(HttpServletResponse res, OhHazard ohHazard) throws IOException {
   List list = service.selectPageList(ohHazard, 1, Integer.MAX_VALUE).getList();
   Workbook wb = new XSSFWorkbook();//创建一个新的工作簿    
   Sheet sheet = wb.createSheet("导出数据");//创建第一个Sheet页,括号内是工作簿的名字. 
   String fileName = "职业危害因素" + DateUtil.formatDate(new Date()) + ".xlsx";//给导出的文件取名字
   genOhhazardExcel(list, sheet,wb);
   sheet.setDisplayGridlines(false);
   WebUtils.writeExcel(wb, fileName,res);
}

// TODO: 2021/10/12  导出
private void genOhhazardExcel(List list, Sheet sheet,Workbook wb) throws IOException {
   // 设置第一行表头
   Row rowHead = sheet.createRow(0);
   //给表头行创建列(这里创建了8列)
   rowHead.createCell(0).setCellValue("编码");
   rowHead.createCell(1).setCellValue("危害因素名称");
   rowHead.createCell(2).setCellValue("所属公司");
   rowHead.createCell(3).setCellValue("所属部门");
   rowHead.createCell(4).setCellValue("化学毒物危害指数THI");
   rowHead.createCell(5).setCellValue("类型");
   rowHead.createCell(6).setCellValue("工作场所化学物的职业接触比值的权重数(WB)");
   rowHead.createCell(7).setCellValue("化学物的危害程度的权重数(WD)");

   CellStyle cellStyleHead = ExcelUtils.headCellStyle(wb);//给单元格设置表头格式
   Map idPojoMap = organizationService.getIdPojoMap();
   for (int i = 0; i <8; i++) {
      rowHead.getCell(i).setCellStyle(cellStyleHead);
   }
    //当列表不为空的时候,就进行遍历
   if(!CollectionUtils.isEmpty(list)){
      CellStyle style = ExcelUtils.cellStyle(wb);
      CellStyle borderCellStyle = ExcelUtils.cellBorder(wb.createCellStyle());
      for(int rownum = 0; rownum < list.size(); rownum++){
         Row row = sheet.createRow(rownum+1);
         row.setHeight((short) 360);
         OhHazard ohHazard=list.get(rownum);
         row.createCell(0).setCellValue(ohHazard.getCode());//编码
         if(!StringUtils.isEmpty(ohHazard.getName())){
            row.createCell(1).setCellValue(ohHazard.getName());//危害因素名称
         }
         if(!StringUtils.isEmpty(ohHazard.getCompId())){
            row.createCell(2).setCellValue(idPojoMap.get(ohHazard.getCompId()).getName());//所属公司
         }
         if(!StringUtils.isEmpty(ohHazard.getOrgId())){
            row.createCell(3).setCellValue(idPojoMap.get(ohHazard.getOrgId()).getName());//所属部门
         }
         if(!StringUtils.isEmpty(ohHazard.getThi())){
            row.createCell(4).setCellValue(ohHazard.getThi().toString());//化学毒物危害指数THI
         }
         if(!StringUtils.isEmpty(ohHazard.getType())){
            if(ohHazard.getType()==1){
               row.createCell(5).setCellValue("化学毒物");//类型
            }if(ohHazard.getType()==2){
               row.createCell(5).setCellValue("粉层");

            }
         }
         if(!StringUtils.isEmpty(ohHazard.getWb())){
            row.createCell(6).setCellValue(ohHazard.getWb().toString());//工作场所化学物的职业接触比值的权重数(WB)
         }
         if(!StringUtils.isEmpty(ohHazard.getWd())){
            row.createCell(7).setCellValue(ohHazard.getWd().toString());//化学物的危害程度的权重数(WD)
         }

         for (int i = 0; i < 8; i++) {
            if (row.getCell(i) != null) {
               row.getCell(i).setCellStyle(style);
            } else {
               row.createCell(i).setCellStyle(borderCellStyle);
            }
         }
      }
      ExcelUtils.autoColumnWidth(sheet, 8);//
   }
   else{
      sheet.createRow(1).createCell(0).setCellValue("查询不到数据");
   }



}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325635.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号