poi实现excel的导入导出

古城微笑少年丶 2021-09-07 06:09 851阅读 0赞

【简介】

目前操作excel比较流行的就是Apache POI和阿里的easyExcel,poi提供API给java程序对Microsoft Office格式格式档案读和写的功能。本篇文章主要说明poi实现excel的导入导出。

HSSF 操作读写03版的excel格式档案的功能;
03版本文件以.xls结尾;最多可以存储65536行数据;

  • 缺点:最多只能处理65536行数据,否则会抛出异常java.lang.IllegalArgumentException:Invalid row number(65536) outside allowabke range(0..65535);
  • 优点:过程中写入缓存,不操作磁盘,最后一次性写入磁盘,速度快;

XSSF 操作读写07版的excel格式档案的功能;
07版文件以.xlsx结尾;对行数无限制。

  • 缺点:写数据速度慢,含内存,也会存在内存溢出OOM;
  • 优点:可以写入较大的数据量。

    大文件读写:SXSSF

  • 缺点:过程中会产生临时文件,需要清理临时文件。((SXSSFWorkbook)workbook).dispose();//在关闭流之后清楚临时文件。
  • 有点:可以写入非常大的数据量,写入速度非常快,占用更少的内存。
  • 原理:默认100条记录被保存到内存中,如果超过100条数据,则最前面的数据被写入临时文件中。如果自动逸内存中的数量,可以说使用new SXSSFWorkbook(数量)。

【poi】导入依赖

  1. <!--xls(03)-->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi</artifactId>
  5. <version>3.9</version>
  6. </dependency>
  7. <!--xlsx(07)-->
  8. <dependency>
  9. <groupId>org.apache.poi</groupId>
  10. <artifactId>poi-ooxml</artifactId>
  11. <version>3.9</version>
  12. </dependency>

【poi】写数据

  1. @Test
  2. public void ExportExcel03() {
  3. //创建工作簿
  4. Workbook workbook=new HSSFWorkbook();
  5. //创建工作表
  6. Sheet sheet=workbook.createSheet();
  7. //创建,0代表第一行
  8. Row title=sheet.createRow(0);
  9. //创建列
  10. Cell cell0=title.createCell(0);
  11. Cell cell1=title.createCell(1);
  12. Cell cell2=title.createCell(2);
  13. Cell cell3=title.createCell(3);
  14. Cell cell4=title.createCell(4);
  15. cell0.setCellValue("姓名");
  16. cell1.setCellValue("出生年月日");
  17. cell2.setCellValue("科目");
  18. cell3.setCellValue("成绩");
  19. cell4.setCellValue("排名");
  20. int rowNum=10;
  21. int cellNum=5;
  22. for(int i=1;i<rowNum;i++){
  23. Row row=sheet.createRow(i);
  24. for(int j=0;j<cellNum;j++){
  25. Cell cell=row.createCell(j);
  26. cell.setCellValue("测试"+i+":"+(j+1));
  27. }
  28. }
  29. FileOutputStream fos= null;
  30. try {
  31. fos = new FileOutputStream("C:\\Users\\lijiao8\\Desktop\\导出03.xls");
  32. workbook.write(fos);
  33. fos.close();
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. @Test
  41. public void ExportExcel07() {
  42. //创建工作簿
  43. Workbook workbook=new XSSFWorkbook();
  44. //创建工作表
  45. Sheet sheet=workbook.createSheet();
  46. //创建,0代表第一行
  47. Row title=sheet.createRow(0);
  48. //创建列
  49. Cell cell0=title.createCell(0);
  50. Cell cell1=title.createCell(1);
  51. Cell cell2=title.createCell(2);
  52. Cell cell3=title.createCell(3);
  53. Cell cell4=title.createCell(4);
  54. cell0.setCellValue("姓名");
  55. cell1.setCellValue("出生年月日");
  56. cell2.setCellValue("科目");
  57. cell3.setCellValue("成绩");
  58. cell4.setCellValue("排名");
  59. int rowNum=10;
  60. int cellNum=5;
  61. for(int i=1;i<rowNum;i++){
  62. Row row=sheet.createRow(i);
  63. for(int j=0;j<cellNum;j++){
  64. Cell cell=row.createCell(j);
  65. cell.setCellValue("测试"+i+":"+(j+1));
  66. }
  67. }
  68. FileOutputStream fos= null;
  69. try {
  70. fos = new FileOutputStream("C:\\Users\\lijiao8\\Desktop\\导出07.xlsx");
  71. workbook.write(fos);
  72. fos.close();
  73. } catch (FileNotFoundException e) {
  74. e.printStackTrace();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }

【poi】读数据(该部分就不将03版本和07版本分开展示了)

读取的excel文件内容:

20210208153947209.png

首先创建一个对象,方便导出,

  1. import java.util.Date;
  2. /**
  3. * @author lijiao
  4. * @create 2021-02-08 14:19
  5. */
  6. public class Student {
  7. private String name;
  8. private String birthDay;
  9. private String course;
  10. private Integer score;
  11. private Integer ranking;
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getBirthDay() {
  19. return birthDay;
  20. }
  21. public void setBirthDay(String birthDay) {
  22. this.birthDay = birthDay;
  23. }
  24. public String getCourse() {
  25. return course;
  26. }
  27. public void setCourse(String course) {
  28. this.course = course;
  29. }
  30. public Integer getScore() {
  31. return score;
  32. }
  33. public void setScore(Integer score) {
  34. this.score = score;
  35. }
  36. public Integer getRanking() {
  37. return ranking;
  38. }
  39. public void setRanking(Integer ranking) {
  40. this.ranking = ranking;
  41. }
  42. @Override
  43. public String toString() {
  44. return "Student{" +
  45. "name='" + name + '\'' +
  46. ", birthDay='" + birthDay + '\'' +
  47. ", course='" + course + '\'' +
  48. ", score=" + score +
  49. ", ranking=" + ranking +
  50. '}';
  51. }
  52. }

导入测试:

  1. @Test
  2. public void importExcel03() {
  3. try {
  4. List<Student> list = new ArrayList<Student>();
  5. int limit = 100;
  6. InputStream in = new FileInputStream("C:\\Users\\Desktop\\测试.xls");
  7. //07版本
  8. //InputStream in = new FileInputStream("C:\\Users\\Desktop\\测试.xlsx");
  9. //Workbook workbook = new XSSFWorkbook(in);
  10. Workbook workbook = new HSSFWorkbook(in);
  11. Sheet sheet = workbook.getSheetAt(0);
  12. int rowCount = sheet.getPhysicalNumberOfRows();
  13. //跳过标题
  14. for (int i = 1; i < rowCount; i++) {
  15. Row row = sheet.getRow(i);
  16. if (row != null) {
  17. int cellCount = row.getPhysicalNumberOfCells();
  18. Student stu = new Student();
  19. Cell cell = row.getCell(0);
  20. if (cell != null) {
  21. String name = cell.getStringCellValue();
  22. stu.setName(name);
  23. }
  24. Cell cell1 = row.getCell(1);
  25. if (cell1 != null) {
  26. Date date = cell1.getDateCellValue();
  27. String birthDay = new DateTime(date).toString("yyyy-MM-dd");
  28. stu.setBirthDay(birthDay);
  29. }
  30. Cell cell2 = row.getCell(2);
  31. if (cell2 != null) {
  32. String course = cell2.getStringCellValue();
  33. stu.setCourse(course);
  34. }
  35. Cell cell3 = row.getCell(3);
  36. if (cell3 != null) {
  37. int score = (int) cell3.getNumericCellValue();
  38. stu.setScore(score);
  39. }
  40. Cell cell4 = row.getCell(4);
  41. if (cell4 != null) {
  42. int rank = (int) cell4.getNumericCellValue();
  43. stu.setRanking(rank);
  44. }
  45. System.out.println(stu.toString());
  46. list.add(stu);
  47. }
  48. if (list.size() > limit) {
  49. //提交代码至数据库
  50. }
  51. }
  52. in.close();
  53. } catch (FileNotFoundException e) {
  54. e.printStackTrace();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }

控制台输出:

Student{name=’张三’, birthDay=’2000-12-12’, course=’英语’, score=200, ranking=2}
Student{name=’李四’, birthDay=’1998-10-23’, course=’俄语’, score=198, ranking=4}
Student{name=’王五’, birthDay=’1997-12-01’, course=’韩语’, score=199, ranking=3}
Student{name=’赵六’, birthDay=’1998-10-01’, course=’日语’, score=201, ranking=1}

发表评论

表情:
评论列表 (有 0 条评论,851人围观)

还没有评论,来说两句吧...

相关阅读