Apache POI
POI包结构
HSSF:读写Microsoft Excel XLS
XSSF:读写Microsoft Excel XLSX
HWPF:读写Microsoft Word DOC
HSLF:读写 Microsoft PowerPoint
操作Excel文件区分版本:
2003版本(包含2003)以前的扩展名为.xls需要用HSSFWorkbook类操作
2007版本(包含2007)以后的扩展名为.xlsx需要用XSSFWorkbook类操作
需要导入的依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
|
通过POI向Excel文件写入内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public class POITest {
public static void write() throws Exception{ XSSFWorkbook excel = new XSSFWorkbook(); XSSFSheet sheet = excel.createSheet("info"); XSSFRow row = sheet.createRow(0); row.createCell(1).setCellValue("姓名"); row.createCell(2).setCellValue("城市");
row = sheet.createRow(1); row.createCell(1).setCellValue("张三"); row.createCell(2).setCellValue("北京");
row = sheet.createRow(2); row.createCell(1).setCellValue("李四"); row.createCell(2).setCellValue("南京");
FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx")); excel.write(out);
out.close(); excel.close(); }
|
通过POI向Excel文件读取内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public static void read() throws Exception{ InputStream in = new FileInputStream(new File("D:\\info.xlsx"));
XSSFWorkbook excel = new XSSFWorkbook(in); XSSFSheet sheet = excel.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i <= lastRowNum ; i++) { XSSFRow row = sheet.getRow(i); String cellValue1 = row.getCell(1).getStringCellValue(); String cellValue2 = row.getCell(2).getStringCellValue(); System.out.println(cellValue1 + " " + cellValue2); }
in.close(); excel.close(); }0 public static void main(String[] args) throws Exception { read(); } }
|
导出运营数据报表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
public void exportBusinessData(HttpServletResponse response) { LocalDate dateBegin = LocalDate.now().minusDays(30); LocalDate dateEnd = LocalDate.now().minusDays(1);
BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));
InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
try { XSSFWorkbook excel = new XSSFWorkbook(in);
XSSFSheet sheet = excel.getSheet("Sheet1");
sheet.getRow(1).getCell(1).setCellValue("时间:" + dateBegin + "至" + dateEnd);
XSSFRow row = sheet.getRow(3); row.getCell(2).setCellValue(businessDataVO.getTurnover()); row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate()); row.getCell(6).setCellValue(businessDataVO.getNewUsers());
row = sheet.getRow(4); row.getCell(2).setCellValue(businessDataVO.getValidOrderCount()); row.getCell(4).setCellValue(businessDataVO.getUnitPrice());
for (int i = 0; i < 30; i++) { LocalDate date = dateBegin.plusDays(i); BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
row = sheet.getRow(7 + i); row.getCell(1).setCellValue(date.toString()); row.getCell(2).setCellValue(businessData.getTurnover()); row.getCell(3).setCellValue(businessData.getValidOrderCount()); row.getCell(4).setCellValue(businessData.getOrderCompletionRate()); row.getCell(5).setCellValue(businessData.getUnitPrice()); row.getCell(6).setCellValue(businessData.getNewUsers()); }
ServletOutputStream out = response.getOutputStream(); excel.write(out);
out.close(); excel.close(); } catch (IOException e) { e.printStackTrace(); }
} }
|