• Java代码对Excel文件得的处理


    目录

    POI对Excel文件的读写

    EasyExcel对文件的读取


     

    POI对Excel文件的读写

    1.写数据到Excel文件(小数据+大数据)

    按行来写,先确定行,然后对每一列进行赋值

    1. //生成的文档的地址为桌面
    2. String path = "C:\\Users\\Administrator\\Desktop\\";
    3. /**
    4. * 1.小数据
    5. * @throws IOException
    6. */
    7. @Test
    8. public void writePoI07 () throws IOException {
    9. /**
    10. * row --行从0开始0代表第一行,cell --列 0开始,0代表第一列
    11. */
    12. //1.创建工作簿
    13. Workbook wk = new XSSFWorkbook();
    14. //2.创建工作表
    15. Sheet sh = wk.createSheet("周先生统计表");
    16. //3.创建行(1,1)
    17. Row row = sh.createRow(0);
    18. //4.创建单元格
    19. Cell cell = row.createCell(0);
    20. //5.写数据
    21. cell.setCellValue("姓名");
    22. Cell cell1 = row.createCell(1);
    23. cell1.setCellValue("周彬彬");
    24. Row row2=sh.createRow(1);
    25. Cell cells=row2.createCell(0);
    26. cells.setCellValue("注册日期");
    27. Cell cell2 = row2.createCell(1);
    28. String date = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
    29. cell2.setCellValue(date);
    30. //创建文件流,生存一张表
    31. FileOutputStream fos = new FileOutputStream(path + "666.xlsx");
    32. //把文件写入工作簿中
    33. wk.write(fos);
    34. //关闭流
    35. fos.close();
    36. System.out.println("输出完毕!!!!!!");
    37. }
    38. /**
    39. * 大数据输出
    40. * @throws Exception
    41. */
    42. @Test
    43. public void writeXls07d () throws Exception {
    44. //0.获得写操作开始时间
    45. long start=System.currentTimeMillis();
    46. //1.创建文档对象
    47. Workbook wk =new XSSFWorkbook();
    48. //2.通过文档对象创建工作簿
    49. Sheet sheet = wk.createSheet("07大数据写");
    50. //3.通过工作谱创建行和单元
    51. for (int rowNum = 0; rowNum <100000 ; rowNum++) {
    52. Row row = sheet.createRow(rowNum);
    53. for (int cellNum = 0; cellNum <10 ; cellNum++) {
    54. Cell cell = row.createCell(cellNum);
    55. cell.setCellValue(cellNum);
    56. }
    57. }
    58. //通过流输出文档对象
    59. FileOutputStream fos = new FileOutputStream(path+"testS.xls");
    60. //向文档对象传入流对象进行写操作
    61. wk.write(fos);
    62. //获得写操作结束的时间
    63. long end = System.currentTimeMillis();
    64. System.out.println("批量写操作成功!!!");
    65. System.out.println("批量写操作总共用时:"+(double)(end-start)/1000+"秒");
    66. //关闭流
    67. fos.close();
    68. }

    2.读取Excel文件

    1. package com.wyh.logstash;
    2. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
    3. import org.apache.poi.ss.usermodel.Cell;
    4. import org.apache.poi.ss.usermodel.Row;
    5. import org.apache.poi.hssf.usermodel.HSSFCell;
    6. import org.apache.poi.xssf.usermodel.XSSFRow;
    7. import org.apache.poi.xssf.usermodel.XSSFSheet;
    8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    9. import org.joda.time.DateTime;
    10. import org.junit.Test;
    11. import java.io.FileInputStream;
    12. import java.io.FileNotFoundException;
    13. import java.io.IOException;
    14. import java.util.Date;
    15. public class POIReadTest {
    16. String path = "C:\\Users\\Administrator\\Desktop\\666.xlsx";
    17. /**
    18. * 07版本读取excel文件
    19. */
    20. @Test
    21. public void readPOI07() throws IOException {
    22. //1.获取文件流
    23. FileInputStream fis = new FileInputStream(path);
    24. //2.通过文档对象——>创建工作谱
    25. XSSFWorkbook workbook = new XSSFWorkbook(fis);
    26. XSSFSheet sheet = workbook.getSheetAt(0);
    27. //3.获取第一行(标题)内容
    28. Row rowTitle = sheet.getRow(0);
    29. if (rowTitle != null) {
    30. //3.1拿到列数
    31. int cellCount = rowTitle.getPhysicalNumberOfCells();
    32. //3.2遍历
    33. for (int cellNum = 0; cellNum < cellCount; cellNum++) {
    34. //3.3得到单元格的值
    35. Cell cell = rowTitle.getCell(cellNum);
    36. if (cell != null) {
    37. int cellType = cell.getCellType();
    38. String cellValue = cell.getStringCellValue();
    39. System.out.println(cellValue + " | ");
    40. }
    41. }
    42. System.out.println();
    43. }
    44. //4.表中的内容
    45. int rows = sheet.getPhysicalNumberOfRows();
    46. //4.1遍历每一行
    47. for (int rowNum = 1; rowNum < rows; rowNum++) {
    48. //4.2得到当前行
    49. Row rowData = sheet.getRow(rowNum);
    50. if (rowData != null) {
    51. //4.3读取每行的每一列,得到单元格
    52. int cellCount = rowTitle.getPhysicalNumberOfCells();
    53. for (int cellNum = 0; cellNum < cellCount; cellNum++) {
    54. System.out.println("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]");
    55. Cell cell = rowData.getCell(cellNum);
    56. //5.根据单元格cell匹配数据
    57. if (cell != null) {
    58. int cellType = cell.getCellType();
    59. String cellValue = "";
    60. switch (cellType) {
    61. case HSSFCell.CELL_TYPE_STRING://字符串
    62. System.out.println("[String]");
    63. cellValue = cell.getStringCellValue();
    64. break;
    65. case HSSFCell.CELL_TYPE_BOOLEAN: // 布尔
    66. System.out.print("【BOOLEAN】");
    67. cellValue = String.valueOf(cell.getBooleanCellValue());
    68. break;
    69. case HSSFCell.CELL_TYPE_BLANK: // 空
    70. System.out.print("【BLANK】");
    71. break;
    72. case HSSFCell.CELL_TYPE_NUMERIC: // 数字(日期、普通数字)
    73. System.out.print("【NUMERIC】");
    74. if (HSSFDateUtil.isCellDateFormatted(cell)) { // 日期
    75. System.out.print("【日期】");
    76. Date date = cell.getDateCellValue();
    77. cellValue = new DateTime(date).toString("yyyy-MM-dd");
    78. } else {
    79. // 不是日期格式,防止数字过长!
    80. System.out.print("【转换为字符串输出】");
    81. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    82. cellValue = cell.toString();
    83. }
    84. break;
    85. case HSSFCell.CELL_TYPE_ERROR:
    86. System.out.print("【数据类型错误】");
    87. break;
    88. }
    89. System.out.println(cellValue);
    90. }
    91. }
    92. }
    93. }
    94. }
    95. }

    EasyExcel对文件的读取

    读取数据主要靠的是监听器

    (50条消息) EasyExcel综合课程实战_Fairy要carry的博客-CSDN博客

     

  • 相关阅读:
    Spring | 依赖注入详解(DI)
    为什么要在电影院装监控?有什么作用?
    单片机简介
    【软件测试用例篇】
    微信小程序获取用户头像昵称组件封装(最新版)
    ESP32 开发板上的超低功耗优化
    侯捷 C++ STL标准库和泛型编程 —— 4 分配器 + 5 迭代器
    【狂神说Java】SpringSecurity+shiro
    机器学习笔记 - 简单了解模式识别
    Soot之 遍历Control Flow Graph
  • 原文地址:https://blog.csdn.net/weixin_57128596/article/details/127598069