2.3.2、XSSF方式导入
public class ExcelRead2007Test { public static String PATH = "/Users/hello/Desktop/"; public static void main(String[] args) throws Exception { //获取文件流 FileInputStream inputStream = new FileInputStream(PATH + "用户信息表2007BigData.xlsx"); //1.创建工作簿,使用excel能操作的这边都看看操作 Workbook workbook = new XSSFWorkbook(inputStream); //2.得到表 Sheet sheet = workbook.getSheetAt(0); //3.得到行 Row row = sheet.getRow(0); //4.得到列 Cell cell = row.getCell(0); getValue(cell); inputStream.close(); } public static void getValue(Cell cell){ //匹配类型数据 if (cell != null) { CellType cellType = cell.getCellType(); String cellValue = ""; switch (cellType) { case STRING: //字符串 System.out.print("[String类型]"); cellValue = cell.getStringCellValue(); break; case BOOLEAN: //布尔类型 System.out.print("[boolean类型]"); cellValue = String.valueOf(cell.getBooleanCellValue()); break; case BLANK: //空 System.out.print("[BLANK类型]"); break; case NUMERIC: //数字(日期、普通数字) System.out.print("[NUMERIC类型]"); if (HSSFDateUtil.isCellDateFormatted(cell)) { //日期 System.out.print("[日期]"); Date date = cell.getDateCellValue(); cellValue = new DateTime(date).toString("yyyy-MM-dd"); } else { //不是日期格式,防止数字过长 System.out.print("[转换为字符串输出]"); cell.setCellType(CellType.STRING); cellValue = cell.toString(); } break; case ERROR: System.out.print("[数据类型错误]"); break; } System.out.println(cellValue); } } }
2.3.3、SXSSF方式导入
public class ExcelReadSXSSFTest { public static String PATH = "/Users/hello/Desktop/"; public static void main(String[] args) throws Exception { //获取文件流 //1.创建工作簿,使用excel能操作的这边都看看操作 OPCPackage opcPackage = OPCPackage.open(PATH + "用户信息表2007BigData.xlsx"); XSSFReader xssfReader = new XSSFReader(opcPackage); StylesTable stylesTable = xssfReader.getStylesTable(); ReadOnlySharedStringsTable sharedStringsTable = new ReadOnlySharedStringsTable(opcPackage); // 创建XMLReader,设置ContentHandler XMLReader xmlReader = SAXHelper.newXMLReader(); xmlReader.setContentHandler(new XSSFSheetXMLHandler(stylesTable, sharedStringsTable, new SimpleSheetContentsHandler(), false)); // 解析每个Sheet数据 Iterator<InputStream> sheetsData = xssfReader.getSheetsData(); while (sheetsData.hasNext()) { try (InputStream inputStream = sheetsData.next();) { xmlReader.parse(new InputSource(inputStream)); } } } /** * 内容处理器 */ public static class SimpleSheetContentsHandler implements XSSFSheetXMLHandler.SheetContentsHandler { protected List<String> row; /** * A row with the (zero based) row number has started * * @param rowNum */ @Override public void startRow(int rowNum) { row = new ArrayList<>(); } /** * A row with the (zero based) row number has ended * * @param rowNum */ @Override public void endRow(int rowNum) { if (row.isEmpty()) { return; } // 处理数据 System.out.println(row.stream().collect(Collectors.joining(" "))); } /** * A cell, with the given formatted value (may be null), * and possibly a comment (may be null), was encountered * * @param cellReference * @param formattedValue * @param comment */ @Override public void cell(String cellReference, String formattedValue, XSSFComment comment) { row.add(formattedValue); } /** * A header or footer has been encountered * * @param text * @param isHeader * @param tagName */ @Override public void headerFooter(String text, boolean isHeader, String tagName) { } } }
三、easypoi
以前的以前,有个大佬程序员,跳到一家公司之后就和业务人员聊上了,这些业务员对excel报表有着许许多多的要求,比如想要一个报表,他的表头是一个多行表头,过几天之后,他想要给这些表头添加样式,比如关键的数据标红,再过几天,他想要再末尾添加一条合计的数据,等等!
起初还好,都是copy、copy,之后发现系统中出现大量的重复代码,于是有一天真的忍受不了了,采用注解搞定来搞定这些定制化成程度高的逻辑,将公共化抽离出来,于是诞生了 easypoi!
easypoi 的底层也是基于 apache poi 进行深度开发的,它主要的特点就是将更多重复的工作,全部简单化,避免编写重复的代码!
下面,我们就一起来了解一下这款高大上的开源工具:easypoi
3.1、首先添加依赖包
<dependencies> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>4.1.0</version> </dependency> </dependencies>
3.2、采用注解导出导入
easypoi 最大的亮点就是基于注解实体类来导出、导入excel,使用起来非常简单!
首先,我们创建一个实体类UserEntity
,其中@Excel
注解表示导出文件的头部信息。
public class UserEntity { @Excel(name = "姓名") private String name; @Excel(name = "年龄") private int age; @Excel(name = "操作时间",format="yyyy-MM-dd HH:mm:ss", width = 20.0) private Date time; //set、get省略 }
接着,我们来编写导出服务!
public static void main(String[] args) throws Exception { List<UserEntity> dataList = new ArrayList<>(); for (int i = 0; i < 10; i++) { UserEntity userEntity = new UserEntity(); userEntity.setName("张三" + i); userEntity.setAge(20 + i); userEntity.setTime(new Date(System.currentTimeMillis() + i)); dataList.add(userEntity); } //生成excel文档 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户","用户信息"), UserEntity.class, dataList); FileOutputStream fos = new FileOutputStream("/Users/hello/Documents/easypoi-user1.xls"); workbook.write(fos); fos.close(); }