当前位置:   article > 正文

Apache—POI详解、小案例展示

Apache—POI详解、小案例展示

简介:Apache POI 是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是,我们可以使用POI在Java程序中对Miscrosoft Office各种文件进行读写操作。

目录

1、应用场景

2、案例代码

2.1 创建 Excel 文件

2.2 读取 Excel 文件


1、应用场景

图 1-1 Apache POI应用场景

Apache POI应用场景:

  • 银行网银系统导出交易明细
  • 各种业务系统导出Excel报表
  • 批量导入业务数据

2、案例代码

2.1 创建 Excel 文件

  1. package com.sky.test;
  2. import org.apache.poi.xssf.usermodel.XSSFRow;
  3. import org.apache.poi.xssf.usermodel.XSSFSheet;
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. public class POITest {
  10. public static void main(String[] args) throws IOException {
  11. write();
  12. }
  13. /**
  14. * 通过POI创建Excel文件并且写入文件内容
  15. */
  16. public static void write() throws IOException {
  17. //创建一个Excel文件
  18. XSSFWorkbook excel = new XSSFWorkbook();
  19. //创建Excel中的单元测
  20. XSSFSheet sheet = excel.createSheet("info");
  21. //在sheet中创建行对象,rownum编号从零开始
  22. XSSFRow row = sheet.createRow(1);
  23. //创建单元格并且写入文件内容
  24. row.createCell(1).setCellValue("姓名");
  25. row.createCell(2).setCellValue("城市");
  26. //创建一个新行
  27. XSSFRow row1 = sheet.createRow(2);
  28. row1.createCell(1).setCellValue("王宁");
  29. row1.createCell(2).setCellValue("亳州");
  30. //创建一个新行
  31. XSSFRow row2 = sheet.createRow(2);
  32. row2.createCell(1).setCellValue("王宁");
  33. row2.createCell(2).setCellValue("亳州");
  34. //将内存中的Excel表格存储到指定的盘符下面
  35. FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\info.xlsx"));
  36. excel.write(fileOutputStream);
  37. //关闭资源
  38. excel.close();
  39. fileOutputStream.close();
  40. }
  41. }

2.2 读取 Excel 文件

  1. package com.sky.test;
  2. import org.apache.poi.xssf.usermodel.XSSFRow;
  3. import org.apache.poi.xssf.usermodel.XSSFSheet;
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  5. import java.io.*;
  6. public class POITest {
  7. public static void main(String[] args) throws IOException {
  8. read();
  9. }
  10. /**
  11. * 通过POI读取Excel文件
  12. */
  13. public static void read() throws IOException{
  14. FileInputStream fileInputStream = new FileInputStream(new File("D:\\info.xlsx"));
  15. //创建一个Excel对象,用于读取Excle数据
  16. XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
  17. //读取Excel文件中的第一个Sheet页
  18. XSSFSheet sheet = excel.getSheetAt(0);
  19. //获取Sheet页中有数据的最后一行
  20. int lastRowNum = sheet.getLastRowNum();
  21. for (int i = 1; i <= lastRowNum; i++) {
  22. //获取某一行
  23. XSSFRow row = sheet.getRow(i);
  24. //获取当前行的单元格对象
  25. String cellValue1 = row.getCell(1).getStringCellValue();
  26. String cellValue2 = row.getCell(2).getStringCellValue();
  27. System.out.println(cellValue1 + " " + cellValue2);
  28. }
  29. //关闭资源
  30. excel.close();
  31. fileInputStream.close();
  32. }
  33. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/431633
推荐阅读
相关标签
  

闽ICP备14008679号