# -*- coding: utf-8 -*- import io path = r'C:\Users\jcxiong\Documents\Codex\MyProject\trafficAudit\traffic-audit-server\src\main\java\com\trafficaudit\reportexport\service\ReportExportService.java' with io.open(path, encoding='utf-8') as f: content = f.read() has_crlf = '\r\n' in content lines = content.split('\n') def func_span(name): start = next(i for i, l in enumerate(lines) if l.strip().startswith('public byte[] ' + name + '(')) end = len(lines) for i in range(start+1, len(lines)): if lines[i].strip().startswith('public byte[] '): end = i break return start, end def process_rank(func, metric, unit, title_suffix, city_writer, province_writer): fs, fe = func_span(func) i = next(i for i in range(fs, fe) if lines[i].strip() == 'int rowIdx = 3;') j = i while j > fs and 'Row head1 = sheet.createRow(' not in lines[j]: j -= 1 assert 'Row head1' in lines[j], func + ' head1 not found' lines[j:i] = [' rowIdx = writeRankHead(sheet, rowIdx, "%s", "%s");' % (metric, unit)] auto_i = next(m for m in range(j+1, fe) if lines[m].strip() == 'autoWidth(sheet, 18);') insert = [ '', ' // 本月区块(当月标题 + 累计数据,与样例结构一致)', ' sheet.createRow(rowIdx++);', ' Row title2 = sheet.createRow(rowIdx++);', ' title2.createCell(0).setCellValue(year + "年" + month + "月全省分市州累计完成" + "%s");' % title_suffix, ' rowIdx = writeRankHead(sheet, rowIdx, "%s", "%s");' % (metric, unit), ' rowIdx = %s;' % province_writer, ' for (String city : RegionUtil.cityList()) {', ' rowIdx = %s;' % city_writer, ' }', '', ' // 末尾空行(与样例结构对齐)', ' for (int i = 0; i < 4; i++) sheet.createRow(rowIdx++);', '', ] lines[auto_i:auto_i] = insert print('processed', func) process_rank('exportTurnoverRank', '周转量', '万吨公里', '公路货物运输周转量情况', 'writeTurnoverRankRows(sheet, rowIdx, city, cumMap, false)', 'writeTurnoverRankRows(sheet, rowIdx, null, cumMap, true)') process_rank('exportFreightRank', '货运量', '万吨', '公路货运量情况', 'writeRankRows(sheet, rowIdx, city, cumMap, h2032FreightCum, h2032FreightYoy, false)', 'writeRankRows(sheet, rowIdx, null, cumMap, h2032FreightCum, h2032FreightYoy, true)') # Change A: blank row before head2 in exportFreightTurnover blank = [i for i, l in enumerate(lines) if l.strip() == 'sheet.createRow(rowIdx++);'] target = [i for i in blank if i+1 < len(lines) and 'Row head2 = sheet.createRow(rowIdx++);' in lines[i+1]] assert len(target) == 1, target del lines[target[0]] print('removed blank row at', target[0]+1) # helper before writeRankRows anchor = next(i for i, l in enumerate(lines) if l.strip().startswith('private int writeRankRows(')) helper = [ ' /** 排名表双行表头 */', ' private int writeRankHead(Sheet sheet, int rowIdx, String metric, String unit) {', ' Row h1 = sheet.createRow(rowIdx++);', ' h1.createCell(0).setCellValue("市州");', ' h1.createCell(1).setCellValue("规上" + metric);', ' h1.createCell(4).setCellValue("规上增速");', ' h1.createCell(6).setCellValue("规下" + metric);', ' h1.createCell(9).setCellValue("规下增速");', ' h1.createCell(11).setCellValue("合计" + metric);', ' h1.createCell(14).setCellValue("合计增速");', ' h1.createCell(16).setCellValue("分市州规上规下占比");', '', ' Row h2 = sheet.createRow(rowIdx++);', ' h2.createCell(1).setCellValue("累计完成 (万" + unit + ")");', ' h2.createCell(2).setCellValue("排名");', ' h2.createCell(3).setCellValue("占全省比重");', ' h2.createCell(4).setCellValue("同比");', ' h2.createCell(5).setCellValue("增速 排名");', ' h2.createCell(6).setCellValue("累计完成 (万" + unit + ")");', ' h2.createCell(7).setCellValue("排名");', ' h2.createCell(8).setCellValue("占全省比重");', ' h2.createCell(9).setCellValue("同比");', ' h2.createCell(10).setCellValue("增速排名");', ' h2.createCell(11).setCellValue("累计完成 (万" + unit + ")");', ' h2.createCell(12).setCellValue("排名");', ' h2.createCell(13).setCellValue("占全省比重");', ' h2.createCell(14).setCellValue("同比");', ' h2.createCell(15).setCellValue("增速排名");', ' return rowIdx;', ' }', '', '', ] lines[anchor:anchor] = helper print('helper inserted') new_content = '\n'.join(lines) if has_crlf: new_content = new_content.replace('\n', '\r\n') with io.open(path, 'w', encoding='utf-8', newline='') as f: f.write(new_content) print('saved')