基于Java编写简单的Excel工具类
作者:老李笔记
这篇文章主要为大家详细介绍了如何基于Java编写简单的Excel工具类,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
Excel工具类
MyExcelUtil.java
package com.asia.common; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; import java.util.ArrayList; import java.util.List; public class MyExcelUtil { // 简单测试 public static void main(String[] args) { List<List<String>> lists = readExcel("D:\\hao\\工作簿1.xlsx", 0); List<List<String>> dataList = new ArrayList<>(); for (int i = 0; i < lists.size(); i++) { dataList.add(lists.get(i)); System.out.println(lists.get(i)); } } public static List<List<String>> readExcel(String path, int sheetIndex) { List<List<String>> excelDataList = new ArrayList<List<String>>(); try { InputStream inputStream = new FileInputStream(path); File file; // 通过文件流工厂方式来处理,用来防止NotOLE2FileException Workbook workbook = WorkbookFactory.create(inputStream); if (inputStream == null || workbook == null) { return excelDataList; } // 读取Sheet Sheet sheet = workbook.getSheetAt(sheetIndex); if (sheet == null) { return excelDataList; } // 循环处理每一行,会读取到第一行 int rows = sheet.getPhysicalNumberOfRows(); int minCells = 0; int maxCells = 0; // 获取最小列数和最大列数,以第一行为准 if (rows >= 1) { minCells = sheet.getRow(0).getFirstCellNum(); maxCells = sheet.getRow(0).getLastCellNum(); } for (int i = 0; i < rows; i++) { // 得到当前行 Row row = sheet.getRow(i); if(null==row) { break; } List<String> rowList = new ArrayList<String>(); for (int j = minCells; j < maxCells; j++) { // 每一个单元格 Cell cell = row.getCell(j); if (cell == null) { rowList.add(""); } else { rowList.add(cell.toString()); } } excelDataList.add(rowList); } inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return excelDataList; } public static boolean writeExcel(List<String> title, List<List<String>> data, String sheetName, String filePath){ if (filePath == null || !filePath.contains(".")) { return false; } String suffix = filePath.substring(filePath.lastIndexOf(".") + 1); Workbook workbook = null; switch (suffix) { case "xls": workbook = new HSSFWorkbook(); break; case "xlsx": workbook = new XSSFWorkbook(); break; default: return false; } // 在workbook中创建一个sheet对应excel中的sheet Sheet sheet = workbook.createSheet(sheetName); // 在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制 Row row = sheet.createRow(0); // 创建单元格,设置表头 int titleSize = title.size(); for (int i = 0; i < titleSize; i++) { Cell cell = row.createCell(i); cell.setCellValue(title.get(i)); } // 写入数据 int dataSize = data.size(); for (int i = 0; i < dataSize; i++) { Row row1 = sheet.createRow(i + 1); List<String> rowData = data.get(i); // 创建单元格设值 for (int i1 = 0; i1 < rowData.size(); i1++) { row1.createCell(i1).setCellValue(rowData.get(i1)); } } File file = new File(filePath); if (file.exists()) { file.delete(); } try { file.createNewFile(); workbook.write(new FileOutputStream(file)); workbook.close(); return true; } catch (IOException e) { e.printStackTrace(); } return false; } }
pom.xml
<dependencies> <!--poi相关依赖--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> <!--poi相关依赖end--> </dependencies>
到此这篇关于基于Java编写简单的Excel工具类的文章就介绍到这了,更多相关Java Excel工具类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!