poi的使用以及详解,poi的maven依赖 |
您所在的位置:网站首页 › poi接口什么意思 › poi的使用以及详解,poi的maven依赖 |
POI
常用场景 将用户信息导出为excel表格(导出数据…)将Excel表中的信息录入到网站数据库(习题上传…)大大减轻网站的录入量!开发中经常会涉及到excel的处理,如导出Excel,导入Excel到数据库中! 操作Excel目前比较流行的就是 Apache POI和阿里巴巴的 easyExcel!只要学不死,就往死里学 Apache POI Apache POI官网 :https://poi.apache.org/ 基本功能 结构: HSSF--提供读写MicrosoftExcel格式档案的功能。 xls XSSF--提供读写MicrosoftExcel OOXML格式档案的功能。 xlsx SXSSF HWPF--提供读写MicrosoftExcel Word格式档案的功能。easyExcel easyExcel 官网地址:https://github.com/alibaba/easyexcel 官方文档:https://www.yuque.com/easyexcel/doc/easyexcel POI-Excel写创建项目 建立一个空项目 Csdn-金飞鹏,创建普通的Maven的Moudel jin-poi引入pom依赖 org.apache.poi poi 3.9 org.apache.poi poi-ooxml 3.9 joda-time joda-time 2.10.1 junit junit 4.1203 | 07 版本的写,就是对象不同,方法是一样的! 需要注意:2003版本和2007版本存在兼容性问题!03最多只有65535行! 1、工作簿; 2、工作表; 3、行; 4、列 String path="C:\\Users\\Administrator\\IdeaProjects\\POI\\jin-poi";03版本 HSSF //创建一个工作簿03 Workbook workbook=new HSSFWorkbook(); //创建一个工作表 Sheet sheet = workbook.createSheet("金飞鹏"); //创建第一行 Row row1 = sheet.createRow(0); //创建第11单元格 Cell cell11 = row1.createCell(0); //写入数据 cell11.setCellValue(666); //创建第12单元格 Cell cell12 = row1.createCell(1); //写入数据 cell12.setCellValue(999); //创建第二行 Row row2=sheet.createRow(1); String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss"); //创建第21单元格 Cell cell21 = row2.createCell(0); //写入数据 cell21.setCellValue(time); //创建第22单元格 Cell cell22 = row2.createCell(1); //写入数据 cell22.setCellValue(time); //创建一个工作表 03版本使用xls结尾! FileOutputStream fileOutputStream=new FileOutputStream(path+"金飞鹏03.xls"); workbook.write(fileOutputStream); //关闭流 fileOutputStream.close(); System.out.println("excel表生成完毕");07版本 XSSF //创建一个工作簿07 Workbook workbook=new XSSFWorkbook(); //创建一个工作表 Sheet sheet = workbook.createSheet("金飞鹏"); //创建第一行 Row row1 = sheet.createRow(0); //创建第11单元格 Cell cell11 = row1.createCell(0); //写入数据 cell11.setCellValue(666); //创建第12单元格 Cell cell12 = row1.createCell(1); //写入数据 cell12.setCellValue(999); //创建第二行 Row row2=sheet.createRow(1); String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss"); //创建第21单元格 Cell cell21 = row2.createCell(0); //写入数据 cell21.setCellValue(time); //创建第22单元格 Cell cell22 = row2.createCell(1); //写入数据 cell22.setCellValue(time); //创建一个工作表 07版本使用xlsx结尾! FileOutputStream fileOutputStream=new FileOutputStream(path+"金飞鹏07.xlsx"); workbook.write(fileOutputStream); //关闭流 fileOutputStream.close(); System.out.println("excel表生成完毕");注意对象的一个区别,文件的后缀! 数据批量导入!大文件写HSSF 缺点:最多只能处理65536行,否则会抛出异常 java.lang.IllegalArgumentException: Invalid row number (65536) outside allowable range (0..65535)优点:过程中写入缓存 不操作磁盘,最后一次性写入磁盘,速度快 public void testWrite03BigData() throws Exception { //时间 long begin=System.currentTimeMillis(); //创建一个工作簿 Workbook workbook=new HSSFWorkbook(); //创建一个表 Sheet sheet = workbook.createSheet(); //写入数据 for (int rowNum = 0; rowNum //创建单元格 Cell cell = row.createCell(cellNum); cell.setCellValue(cellNum); } } System.out.println("over"); //创建一个工作簿 FileOutputStream fileOutputStream=new FileOutputStream(path+"金飞鹏.xlsx"); workbook.write(fileOutputStream); //关闭流 fileOutputStream.close(); System.out.println("创建工作簿成功"); long end=System.currentTimeMillis(); System.out.println("总耗时:"+(double)(end-begin)/1000); }大文件写XSSF 缺点:写数据时速度非常慢,非常耗内存,也会发生内存溢出,如100万条 优点:可以写较大的数据量,如20万条 public void testWrite07BigData() throws Exception { //时间 long begin=System.currentTimeMillis(); //创建一个工作簿 Workbook workbook=new XSSFWorkbook(); //创建一个表 Sheet sheet = workbook.createSheet(); //写入数据 for (int rowNum = 0; rowNum < 100000 ; rowNum++) { //创建行 Row row = sheet.createRow(rowNum); for (int cellNum = 0; cellNum < 10; cellNum++) { //创建单元格 Cell cell = row.createCell(cellNum); cell.setCellValue(cellNum); } } System.out.println("over"); //创建一个工作簿 FileOutputStream fileOutputStream=new FileOutputStream(path+"金飞鹏.xlsx"); workbook.write(fileOutputStream); //关闭流 fileOutputStream.close(); System.out.println("创建工作簿成功"); long end=System.currentTimeMillis(); System.out.println("总耗时:"+(double)(end-begin)/1000); }大文件写SXSSF 优点:可以写入非常大的数据量,如100万条甚至跟多条,写数据速度快,占用更少的内存 注意: 过程中会产生临时文件,需要清理临时文件 默认有100条记录被保存在内存中,如果超过这数量,则最前面的数据被写入临时文件 如果想自定义内存中数据的数量,可以使用 new SXSSFWorkbook(数量) public void testWrite03BigDataS() throws Exception { //时间 long begin=System.currentTimeMillis(); //创建一个工作簿 Workbook workbook=new SXSSFWorkbook(); //创建一个表 Sheet sheet = workbook.createSheet(); //写入数据 for (int rowNum = 0; rowNum //创建单元格 Cell cell = row.createCell(cellNum); cell.setCellValue(cellNum); } } System.out.println("over"); //创建一个工作簿 FileOutputStream fileOutputStream=new FileOutputStream(path+"金飞鹏S.xlsx"); workbook.write(fileOutputStream); //关闭流 fileOutputStream.close(); System.out.println("创建工作簿成功"); long end=System.currentTimeMillis(); System.out.println("总耗时:"+(double)(end-begin)/1000); }SXSSFWorkbook -来自官方的解释:实现”BIgGridDemo“策略的流式XSSFWorkbook版本。这允许写入非常大的文件而不会耗尽内存,因为任何时候只有可配置的行部分被保存在内存中。 请注意,仍然可能会消耗大量内存,这些内存基于您正在使用的功能,例如合并区域,注释…仍然至储存在内存中,因此如果广泛使用,可能需要大量内存。 POI-Excel读03 | 07 路径=String path=“C:\Users\Administrator\IdeaProjects\POI\jin-poi\”; 1、03版本 public void ExcelReadtest() throws Exception { FileInputStream fileInputStream=new FileInputStream(path+"jin-poi金飞鹏03.xls"); //获取工作簿 Workbook workbook=new HSSFWorkbook(fileInputStream); //获取第一张表 Sheet sheetAt = workbook.getSheetAt(0); //获取第一行 Row row = sheetAt.getRow(0); //读取第一行的第一个单元格 Cell cell = row.getCell(6); //读取数据 System.out.println(cell.getNumericCellValue()); //关闭流 fileInputStream.close(); } 2、07版本 public void ExcelReadtest() throws Exception { FileInputStream fileInputStream=new FileInputStream(path+"jin-poi金飞鹏07.xlsx"); //获取工作簿 Workbook workbook=new XSSFWorkbook(fileInputStream); //获取第一张表 Sheet sheetAt = workbook.getSheetAt(0); //获取第一行 Row row = sheetAt.getRow(0); //读取第一行的第一个单元格 Cell cell = row.getCell(6); //读取数据 System.out.println(cell.getNumericCellValue()); //关闭流 fileInputStream.close(); }读取不同的数据类型 public void ExcelReadNoTypetest() throws Exception { FileInputStream fileInputStream=new FileInputStream(path+"会员消费商品明细表.xls"); //创建一个工作簿。使用excel能操作的这边他都可以操作! Workbook workbook=new HSSFWorkbook(fileInputStream); //读取第一个表 Sheet sheetAt = workbook.getSheetAt(0); //获取标题内容 Row rowTitle = sheetAt.getRow(0); if (rowTitle!=null){ //一定要掌握 //获取一行里面有多少列 int cells = rowTitle.getPhysicalNumberOfCells(); for (int cellNum = 0; cellNum //获取从第一行开始以后的数据 Row row = sheetAt.getRow(rowNum); if (row!=null){ //获取每一行总共有多少列 int cells = row.getPhysicalNumberOfCells(); for (int cellNum = 0; cellNum //获取每一个单元格的类型 int cellType = cell.getCellType(); String cellValue=""; switch (cellType){ case Cell.CELL_TYPE_STRING: //字符串 System.out.print("[String]"); cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: //布尔 System.out.print("[boolean]"); cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_BLANK: //空 System.out.print("[空]"); break; case Cell.CELL_TYPE_NUMERIC: //数字(日期、普通数字) if (DateUtil.isCellDateFormatted(cell)){ System.out.print("[日期]"); Date date = cell.getDateCellValue(); cellValue = new DateTime(date).toString("yyyy-MM-dd"); }else { //不是日期格式,防止数字过长。 System.out.print("[转换为字符串]"); cell.setCellType(Cell.CELL_TYPE_STRING); cellValue=cell.toString(); } break; case Cell.CELL_TYPE_ERROR:// System.out.print("数据类型错误"); break; } System.out.println(cellValue); } } System.out.println(); } } }注意:转换问题 计算公式 public void ExcelGongShi() throws Exception { FileInputStream fileInputStream=new FileInputStream(path+"计算公式.xls"); Workbook workbook=new HSSFWorkbook(fileInputStream); //获取第一个表 Sheet sheetAt = workbook.getSheetAt(0); //获取第四行的第一个单元格 Cell cell = sheetAt.getRow(4).getCell(0); //拿到计算公式 FormulaEvaluator formulaEvaluator=new HSSFFormulaEvaluator((HSSFWorkbook) workbook); //输出单元格的公式 int cellType = cell.getCellType(); switch (cellType){ case Cell.CELL_TYPE_FORMULA: //公式 String cellFormula = cell.getCellFormula(); System.out.println(cellFormula); //计算 CellValue evaluate = formulaEvaluator.evaluate(cell); String s = evaluate.formatAsString(); System.out.println(s); break; } } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |