数据导出excel简单案例

  1. 需要的依赖坐标

    1. <!--POI-->
    2. <dependency>
    3. <groupId>org.apache.poi</groupId>
    4. <artifactId>poi</artifactId>
    5. <version>4.0.1</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>org.apache.poi</groupId>
    9. <artifactId>poi-ooxml</artifactId>
    10. <version>4.0.1</version>
    11. </dependency>
    12. <dependency>
    13. <groupId>org.apache.poi</groupId>
    14. <artifactId>poi-ooxml-schemas</artifactId>
    15. <version>4.0.1</version>
    16. </dependency>
  2. Java代码演示 ```java package com.xiaoha.excel.excel_eximport;

import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

/**

  • 导出excel文件 *
  • @author HausenLee
  • @date 2021/04/09 */ public class ExceExport { public static void main(String[] args) {

    1. //1.获取到对应的Excel文件,工作簿文件
    2. Workbook wb = new XSSFWorkbook();
    3. //2.创建工作表
    4. Sheet sheet = wb.createSheet();
    5. wb.createSheet("这是啥呀");
    6. //3.创建工作表中的行对象
    7. Row row = sheet.createRow(1);
    8. //4.创建工作表中行中的列对象
    9. Cell cell = row.createCell(1);
    10. //5.在列中写数据(设置数据)
    11. cell.setCellValue("测试一下单元格");
    12. //创建一个文件对象,作为excel文件内容的输出文件
    13. File f = new File("test.xlsx");//文件在当前项目下
    14. //输出时通过流的形式对外输出,包装对应的目标文件
    15. OutputStream os = null;
    16. try {
    17. os = new FileOutputStream(f);
    18. wb.write(os);
    19. } catch (Exception e) {
    20. e.printStackTrace();
    21. } finally {
    22. //将内存中的workbook数据写入到流中
    23. try {
    24. wb.close();
    25. if(os!=null){
    26. os.close();
    27. }
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. }
    31. }

    } } ```

excel导入数据简单案例

  1. Java代码演示 ```java package com.xiaoha.excel.excelImport;

import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;

/**

  • 导入excel文件 *
  • @author HausenLee
  • @date 2021/04/09 */ public class ExcelImport { public static void main(String[] args) {

    1. Workbook wb = null;
    2. try {
    3. //1.获取要读取的文件工作簿对象
    4. wb = new XSSFWorkbook("test.xlsx");
    5. //2.获取工作表
    6. Sheet s = wb.getSheetAt(0);
    7. //3.获取行
    8. Row row = s.getRow(1);
    9. //4.获取列
    10. Cell cell = row.getCell(1);
    11. //5.根据数据的类型获取数据
    12. String data = cell.getStringCellValue();
    13. //double data = cell.getNumericCellValue(); 获取数值类型
    14. //boolean data = cell.getBooleanCellValue(); 获取布尔类型的
    15. System.out.println(data);
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. } finally {
    19. try {
    20. if(wb!=null){
    21. wb.close();
    22. }
    23. } catch (IOException e) {
    24. e.printStackTrace();
    25. }
    26. }

    } } ```

数据导出excel复杂案例

  1. Java代码演示 ```java package com.xiaoha.excel.export_header;

import com.xiaoha.excel.pojo.Question; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Test;

import java.io.*; import java.util.ArrayList; import java.util.List;

/**

  • 导出表头 *
  • @author HausenLee
  • @date 2021/04/09 */ public class ExportHeader { public static void main(String[] args) {

    1. //1.获取到对应的Excel文件,工作簿文件
    2. Workbook wb = new XSSFWorkbook();
    3. //2.创建工作表
    4. Sheet s = wb.createSheet("题目数据文件");
    5. //设置通用配置
    6. //s.setColumnWidth(4,100);
    7. //制作标题 addMergedRegion方法是合并单元格的方法
    8. //参数为指定行列的对象CellRangeAddress
    9. //它的构造方法参数为第一行,最后一行,第一列,最后一列
    10. s.addMergedRegion(new CellRangeAddress(1,1,1,12));
    11. //写入标题
    12. Row row_1 = s.createRow(1);
    13. Cell cell_1_1 = row_1.createCell(1);
    14. cell_1_1.setCellValue("在线试题导出信息");
    15. //创建一个样式
    16. CellStyle cs_title = wb.createCellStyle();
    17. //水平对齐
    18. cs_title.setAlignment(HorizontalAlignment.CENTER);
    19. //垂直对齐
    20. cs_title.setVerticalAlignment(VerticalAlignment.CENTER);
    21. cell_1_1.setCellStyle(cs_title);
    22. //制作表头
    23. String[] fields = {"题目ID","所属公司ID","所属目录ID","题目简介","题干描述","题干配图","题目分析","题目类型","题目难度","是否经典题","题目状态","审核状态"};
    24. Row row_2 = s.createRow(2);
    25. for (int i = 0; i < fields.length; i++) {
    26. Cell cell_2_temp = row_2.createCell(1 + i);
    27. //++
    28. cell_2_temp.setCellValue(fields[i]);
    29. //++
    30. CellStyle cs_field = wb.createCellStyle();
    31. cs_field.setAlignment(HorizontalAlignment.CENTER);
    32. cell_2_temp.setCellStyle(cs_field);
    33. }
    34. //制作数据区
    35. // 创建一个文件对象,作为excel文件内容的输出文件
    36. File f = new File("test.xlsx");
    37. //输出时通过流的形式对外输出,包装对应的目标文件
    38. OutputStream os = null;
    39. try {
    40. os = new FileOutputStream(f);
    41. //将内存中的workbook数据写入到流中
    42. wb.write(os);
    43. } catch (Exception e) {
    44. e.printStackTrace();
    45. } finally {
    46. try {
    47. wb.close();
    48. if(os!=null){
    49. os.close();
    50. }
    51. } catch (IOException e) {
    52. e.printStackTrace();
    53. }
    54. }
  1. }
  2. @Test
  3. public void testProjectPoi() throws IOException {
  4. //1.获取到对应的Excel文件,工作簿文件
  5. Workbook wb = new XSSFWorkbook();
  6. //2.创建工作表
  7. Sheet s = wb.createSheet("题目数据文件");
  8. //制作数据区
  9. List<Question> questionList = new ArrayList<>();
  10. Question qq = new Question();
  11. qq.setId("1");
  12. qq.setPicture("12");
  13. qq.setReviewStatus("13");
  14. qq.setAnalysis("14");
  15. qq.setCatalogId("15");
  16. qq.setCompanyId("16");
  17. qq.setDifficulty("17");
  18. qq.setIsClassic("18");
  19. qq.setRemark("19");
  20. qq.setState("21");
  21. qq.setSubject("31");
  22. qq.setType("41");
  23. questionList.add(qq);
  24. Question qqq = new Question();
  25. qqq.setId("1");
  26. qqq.setPicture("12");
  27. qqq.setReviewStatus("13");
  28. qqq.setAnalysis("14");
  29. qqq.setCatalogId("15");
  30. qqq.setCompanyId("16");
  31. qqq.setDifficulty("17");
  32. qqq.setIsClassic("18");
  33. qqq.setRemark("19");
  34. qqq.setState("21");
  35. qqq.setSubject("31");
  36. qqq.setType("41");
  37. questionList.add(qqq);
  38. //遍历集合
  39. /*for (int i = 0; i < questionList.size(); i++) {
  40. Row row = s.createRow(i + 3);
  41. for (int j = 0; j < 12; j++) {
  42. Cell cell = row.createCell(j + 1);
  43. }
  44. }*/
  45. int rowIndex= 0;
  46. for (Question question : questionList) {
  47. int cellIndex = 0;
  48. Row row = s.createRow( 3+rowIndex++);
  49. Cell cell_1 = row.createCell(1+cellIndex++);
  50. cell_1.setCellValue(question.getId());
  51. Cell cell_2 = row.createCell(2+cellIndex++);
  52. cell_2.setCellValue(question.getPicture());
  53. Cell cell_3 = row.createCell(3+cellIndex++);
  54. cell_3.setCellValue(question.getReviewStatus());
  55. Cell cell_4 = row.createCell(4+cellIndex++);
  56. cell_4.setCellValue(question.getAnalysis());
  57. Cell cell_5 = row.createCell(5+cellIndex++);
  58. cell_5.setCellValue(question.getCatalogId());
  59. Cell cell_6 = row.createCell(6+cellIndex++);
  60. cell_6.setCellValue(question.getCompanyId());
  61. Cell cell_7 = row.createCell(7+cellIndex++);
  62. cell_7.setCellValue(question.getDifficulty());
  63. Cell cell_8 = row.createCell(8+cellIndex++);
  64. cell_8.setCellValue(question.getIsClassic());
  65. Cell cell_9 = row.createCell(9+cellIndex++);
  66. cell_9.setCellValue(question.getRemark());
  67. Cell cell_10 = row.createCell(10+cellIndex++);
  68. cell_10.setCellValue(question.getState());
  69. Cell cell_11 = row.createCell(11+cellIndex++);
  70. cell_11.setCellValue(question.getSubject());
  71. Cell cell_12 = row.createCell(12+cellIndex++);
  72. cell_12.setCellValue(question.getType());
  73. }
  74. //创建一个文件对象,作为excel文件内容的输出文件
  75. File f = new File("test.xlsx");//文件在当前项目下
  76. //输出时通过流的形式对外输出,包装对应的目标文件
  77. OutputStream os = null;
  78. try {
  79. os = new FileOutputStream(f);
  80. wb.write(os);
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. } finally {
  84. //将内存中的workbook数据写入到流中
  85. try {
  86. wb.close();
  87. if(os!=null){
  88. os.close();
  89. }
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. }

} ```