业主投票数据统计,格式化工具
数据格式为
1幢2402室2幢902室3幢1101室4幢3104室
类似的N行一列表格。
格式转换后形成一个任意行任意列的的矩形表格,方便查看。转化效果如下图。

2 3 import org.apache.commons.lang3.StringUtils; 4 import org.apache.poi.ss.usermodel.*; 5 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 6 7 import java.io.*; 8 import java.util.HashMap; 9 import java.util.Map; 10 11 /** 12 * 帮我处理一下Excel表格,目前表格格式是一列数据,数据格式为1幢2402室。 现在我不需要把它变成一幢一个表格,每个表格是33行, 13 * 列是4列的表格。比如1幢2402室, 就是在一号表格里,第24行第二列的格子变成红色。 14 */ 15 public class ExcelProcessor { 16 17 public static void main(String[] args) { 18 String inputFilePath = "D:\\output\\weijielong.xlsx"; 19 String outputDirectory = "D:\\output\\lou"; 20 21 try { 22 processExcelFile(inputFilePath, outputDirectory); 23 System.out.println("Excel处理完成!"); 24 } catch (IOException e) { 25 System.err.println("处理Excel文件时出错: " + e.getMessage()); 26 e.printStackTrace(); 27 } 28 } 29 30 public static void processExcelFile(String inputFilePath, String outputDirectory) throws IOException { 31 // 创建输出目录 32 File dir = new File(outputDirectory); 33 if (!dir.exists()) { 34 dir.mkdirs(); 35 } 36 37 // 读取输入Excel文件 38 try (FileInputStream fis = new FileInputStream(inputFilePath); Workbook workbook = new XSSFWorkbook(fis)) { 39 40 Sheet inputSheet = workbook.getSheetAt(0); 41 Map<String, Workbook> buildingWorkbooks = new HashMap<>(); 42 Map<String, Sheet> buildingSheets = new HashMap<>(); 43 44 // 创建红色样式 45 46 // Font font = workbook.createFont(); 47 // font.setFontHeightInPoints((short) 10); 48 49 // CellStyle redStyle = workbook.createCellStyle(); 50 // redStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); 51 // redStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); 52 // redStyle.setFont(font); 53 54 55 // 遍历输入表格中的每一行 56 for (int i = 0; i <= inputSheet.getLastRowNum(); i++) { 57 Row row = inputSheet.getRow(i); 58 if (row == null) 59 continue; 60 61 Cell cell = row.getCell(0); 62 if (cell == null) 63 continue; 64 65 String roomInfo = cell.getStringCellValue(); 66 if (roomInfo == null || roomInfo.trim().isEmpty()) 67 continue; 68 69 // 解析房号信息 70 ExcelProcessor excelProcessor=new ExcelProcessor(); 71 Room room = excelProcessor.parseRoomInfo(roomInfo); 72 if (room == null) 73 continue; 74 75 // 获取或创建对应幢的工作簿和工作表 76 Workbook buildingWorkbook = buildingWorkbooks.computeIfAbsent(room.building, k -> new XSSFWorkbook()); 77 Sheet buildingSheet = buildingSheets.computeIfAbsent(room.building, k -> { 78 Sheet sheet = buildingWorkbook.createSheet(k + "幢"); 79 // 创建表头 80 Row headerRow = sheet.createRow(0); 81 for (int j = 1; j < 5; j++) { 82 Cell headerCell = headerRow.createCell(j); 83 headerCell.setCellValue("0" + j); 84 } 85 // 创建行头 86 for (int j = 1; j < 34; j++) { 87 Row lineRow = sheet.createRow(j); 88 Cell headerCell = lineRow.createCell(0); 89 headerCell.setCellValue(j + "楼"); 90 } 91 return sheet; 92 }); 93 94 // 计算目标单元格位置 95 int targetRowNum = room.floor; 96 int targetColNum = room.room; 97 98 // 确保行存在 99 Row targetRow = buildingSheet.getRow(targetRowNum); 100 if (targetRow == null) { 101 targetRow = buildingSheet.createRow(targetRowNum); 102 } 103 104 // 确保单元格存在并设置值 105 Cell targetCell = targetRow.getCell(targetColNum); 106 if (targetCell == null) { 107 targetCell = targetRow.createCell(targetColNum); 108 } 109 String val = StringUtils.remove(roomInfo, room.building); 110 val = StringUtils.remove(roomInfo, room.building); 111 targetCell.setCellValue(StringUtils.remove(val, "室")); 112 // targetCell.setCellStyle(redStyle); 113 } 114 115 // 保存所有幢的工作簿 116 for (Map.Entry<String, Workbook> entry : buildingWorkbooks.entrySet()) { 117 String buildingNumber = entry.getKey(); 118 Workbook buildingWorkbook = entry.getValue(); 119 120 String outputFilePath = outputDirectory + "/" + buildingNumber + ".xlsx"; 121 try (FileOutputStream fos = new FileOutputStream(outputFilePath)) { 122 buildingWorkbook.write(fos); 123 } 124 } 125 126 // 关闭所有工作簿 127 for (Workbook wb : buildingWorkbooks.values()) { 128 wb.close(); 129 } 130 } 131 } 132 133 private Room parseRoomInfo(String roomInfo) { 134 try { 135 136 String building =""; 137 String huhao=""; 138 if(StringUtils.contains(roomInfo, "元")) { 139 building = StringUtils.substringBefore(roomInfo, "元")+"元"; 140 huhao=StringUtils.substringBetween(roomInfo,"元","室"); 141 }else { 142 building = StringUtils.substringBefore(roomInfo, "幢")+"幢"; 143 huhao=StringUtils.substringBetween(roomInfo,"幢","室"); 144 } 145 146 System.err.println("building: " + building); 147 148 int floor =0; 149 if(huhao.length()==4) { 150 floor = Integer.parseInt(huhao.substring(0,2)); 151 }else { 152 floor = Integer.parseInt(huhao.substring(0, 1)); 153 } 154 155 System.err.println("floor: " + floor); 156 157 int room =0; 158 if(huhao.length()==4) { 159 room = Integer.parseInt(huhao.substring(2,4)); 160 }else { 161 room = Integer.parseInt(huhao.substring(2, 3)); 162 } 163 164 System.err.println("room: " + room); 165 166 return new Room(building, floor, room); 167 } catch (Exception e) { 168 System.err.println("解析房号信息失败: " + roomInfo + ", 错误: " + e.getMessage()); 169 return null; 170 } 171 } 172 173 class Room { 174 String building; 175 int floor; 176 int room; 177 178 public Room(String building, int floor, int room) { 179 this.building = building; 180 this.floor = floor; 181 this.room = room; 182 } 183 } 184 }

浙公网安备 33010602011771号