|
@@ -0,0 +1,158 @@
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * excel工具类
|
|
|
+ *
|
|
|
+ * @author alvin
|
|
|
+ * @date 2023/07/28
|
|
|
+ */
|
|
|
+public class ExcelUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将数据写入Excel文件的指定工作表中
|
|
|
+ *
|
|
|
+ * @param filePath Excel文件路径
|
|
|
+ * @param sheetName 工作表名称
|
|
|
+ * @param rowBeginIndex 写入数据开始的行索引
|
|
|
+ * @param header 表头
|
|
|
+ * @param col 列数据列表,每个Map表示一列数据,key为行索引,value为对应单元格的值
|
|
|
+ * @return 写入是否成功
|
|
|
+ */
|
|
|
+ public static boolean writeExcel(String filePath, String sheetName, int rowBeginIndex, List<String> header, List<Map<String, Object>> col) {
|
|
|
+ Workbook workbook;
|
|
|
+ if (filePath.endsWith(".xlsx")) {
|
|
|
+ workbook = new XSSFWorkbook();
|
|
|
+ } else if (filePath.endsWith(".xls")) {
|
|
|
+ workbook = new HSSFWorkbook();
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("Invalid file extension");
|
|
|
+ }
|
|
|
+
|
|
|
+ Sheet sheet = workbook.createSheet(sheetName);
|
|
|
+
|
|
|
+ int rowIndex = rowBeginIndex;
|
|
|
+ Row headerRow = sheet.createRow(rowIndex++);
|
|
|
+ for (int i = 0; i < header.size(); i++) {
|
|
|
+ Cell cell = headerRow.createCell(i);
|
|
|
+ cell.setCellValue(header.get(i));
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Map<String, Object> rowData : col) {
|
|
|
+ Row dataRow = sheet.createRow(rowIndex++);
|
|
|
+ int colIndex = 0;
|
|
|
+ for (String key : header) {
|
|
|
+ Cell cell = dataRow.createCell(colIndex++);
|
|
|
+ Object value = rowData.get(key);
|
|
|
+ if (value instanceof String) {
|
|
|
+ cell.setCellValue((String) value);
|
|
|
+ } else if (value instanceof Double) {
|
|
|
+ cell.setCellValue((Double) value);
|
|
|
+ } else if (value instanceof Integer) {
|
|
|
+ cell.setCellValue((Integer) value);
|
|
|
+ } else if (value instanceof Boolean) {
|
|
|
+ cell.setCellValue((Boolean) value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
|
|
|
+ workbook.write(outputStream);
|
|
|
+ return true;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从Excel文件的指定工作表中读取数据,并返回一个包含每行数据的LinkedList
|
|
|
+ *
|
|
|
+ * @param filePath Excel文件路径
|
|
|
+ * @param sheetName 工作表名称
|
|
|
+ * @param rowBeginIndex 读取数据开始的行索引
|
|
|
+ * @param colBeginIndex 读取数据开始的列索引
|
|
|
+ * @return 包含每行数据的LinkedList,每行数据以Map的形式表示,key为列索引的字符串表示,value为对应单元格的值
|
|
|
+ */
|
|
|
+ public static LinkedList<Map<String, Object>> readExcel(String filePath, String sheetName, int rowBeginIndex, int colBeginIndex) {
|
|
|
+ LinkedList<Map<String, Object>> result = new LinkedList<>();
|
|
|
+
|
|
|
+ try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
|
|
|
+ Workbook workbook;
|
|
|
+ if (filePath.endsWith(".xlsx")) {
|
|
|
+ workbook = new XSSFWorkbook(fileInputStream);
|
|
|
+ } else if (filePath.endsWith(".xls")) {
|
|
|
+ workbook = new HSSFWorkbook(fileInputStream);
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("Invalid file extension");
|
|
|
+ }
|
|
|
+
|
|
|
+ Sheet sheet = workbook.getSheet(sheetName);
|
|
|
+ if (sheet == null) {
|
|
|
+ throw new IllegalArgumentException("Sheet not found");
|
|
|
+ }
|
|
|
+
|
|
|
+ Row headerRow = sheet.getRow(rowBeginIndex);
|
|
|
+ if (headerRow == null) {
|
|
|
+ throw new IllegalArgumentException("Header row not found");
|
|
|
+ }
|
|
|
+
|
|
|
+ int lastRowNum = sheet.getLastRowNum();
|
|
|
+ for (int i = rowBeginIndex + 1; i <= lastRowNum; i++) {
|
|
|
+ Row dataRow = sheet.getRow(i);
|
|
|
+ if (dataRow == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> rowData = new LinkedHashMap<>();
|
|
|
+ for (int j = colBeginIndex; j < headerRow.getLastCellNum(); j++) {
|
|
|
+ Cell cell = dataRow.getCell(j);
|
|
|
+ if (cell == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String key = headerRow.getCell(j).getStringCellValue();
|
|
|
+ Object value = getCellValue(cell);
|
|
|
+ rowData.put(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ result.add(rowData);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object getCellValue(Cell cell) {
|
|
|
+ switch (cell.getCellType()) {
|
|
|
+ case STRING:
|
|
|
+ return cell.getStringCellValue();
|
|
|
+ case NUMERIC:
|
|
|
+ if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
+ return cell.getDateCellValue();
|
|
|
+ } else {
|
|
|
+ return cell.getNumericCellValue();
|
|
|
+ }
|
|
|
+ case BOOLEAN:
|
|
|
+ return cell.getBooleanCellValue();
|
|
|
+ case FORMULA:
|
|
|
+ return cell.getCellFormula();
|
|
|
+ default:
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ LinkedList<Map<String, Object>> detailedListOfUserNumbers = readExcel("/Users/alvin/Downloads/授用信各资方家数表.xlsx", "用信家数明细表", 0, 0);
|
|
|
+ System.out.println(detailedListOfUserNumbers.size());
|
|
|
+ }
|
|
|
+}
|