main
黄海 8 months ago
parent 2931e48eb3
commit 570f08b135

@ -16,6 +16,7 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class C1 {
@ -45,7 +46,8 @@ public class C1 {
//应用样式
XSSFCellStyle headerStyle = ExcelKit.getHeaderStyle(outWorkbook);
XSSFCellStyle dataStyle = ExcelKit.getDataStyle(outWorkbook);
//拷贝文件头,第一行
//拷贝文件头
ExcelKit.CopyHead(sampleExcelPath, outSheet, headerStyle);
//找到parentPath下一级目录中所有文件
@ -81,25 +83,16 @@ public class C1 {
//遍历source1
for (int i = 0; i < source1.size(); i++) {
List<String> r = source1.get(i);
rowIndex++;
Row outRow = outSheet.createRow(rowIndex);
Row outRow = outSheet.createRow(++rowIndex);
// 需要写死列的对应关系
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("总入园数");//总量分类
outRow.createCell(2).setCellValue("");//区域分类
outRow.createCell(3).setCellValue(r.get(4));//总量数值
outRow.createCell(4).setCellValue("");//区域数值
outRow.createCell(5).setCellValue(cityName);//行政区划
outRow.createCell(6).setCellValue("云南省");//上级行政区划
//应用一下样式
ExcelKit.applyStyle(outRow, 0, 6, dataStyle);
ExcelKit.putData(outRow, new ArrayList<>(Arrays.asList(r.getFirst(), "总入园数", "", r.get(4), "", cityName, "云南省")), dataStyle);
//年份,总量分类,区域分类,总量数值,区域数值,行政区划,上级行政区划
}
//遍历source2
for (int i = 0; i < source2.size(); i++) {
if (i == 0) continue;//跳过2022年这个年份重复了
List<String> r = source2.get(i);
rowIndex++;
Row outRow = outSheet.createRow(rowIndex);
Row outRow = outSheet.createRow(++rowIndex);
// 需要写死列的对应关系
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("总入园数");//总量分类
@ -115,8 +108,7 @@ public class C1 {
//2022年基数:遍历source2
for (int i = 0; i < source2.size(); i++) {
List<String> r = source2.get(i);
rowIndex++;
Row outRow = outSheet.createRow(rowIndex);
Row outRow = outSheet.createRow(++rowIndex);
// 需要写死列的对应关系
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("2022年基数");//基数
@ -140,8 +132,7 @@ public class C1 {
for (int i = 0; i < source1.size(); i++) {
List<String> r = source1.get(i);
rowIndex++;
Row outRow = outSheet.createRow(rowIndex);
Row outRow = outSheet.createRow(++rowIndex);
// 需要写死列的对应关系
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("");//总量分类
@ -156,18 +147,13 @@ public class C1 {
for (int i = 0; i < source2.size(); i++) {
if (i == 0) continue;
List<String> r = source2.get(i);
rowIndex++;
Row outRow = outSheet.createRow(rowIndex);
Row outRow = outSheet.createRow(++rowIndex);
// 需要写死列的对应关系
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("");//基数
outRow.createCell(2).setCellValue(areaName);//区域分类
outRow.createCell(3).setCellValue("");//总量数值
if (k + 1 < r.size()) {
outRow.createCell(4).setCellValue(r.get(k + 1));//区域数值
} else {
outRow.createCell(4).setCellValue("");//区域数值
}
outRow.createCell(4).setCellValue(r.get(k + 1));//区域数值
outRow.createCell(5).setCellValue(cityName);//行政区划
outRow.createCell(6).setCellValue("云南省");//上级行政区划
@ -176,6 +162,7 @@ public class C1 {
}
}
}
System.out.println("正在处理" + cityName + "市州文件...");
}
}

@ -117,14 +117,14 @@ public class ExcelKit {
//遍历输出sheet的内容
int rowIndex = 0;
// 遍历工作表中的所有行
if(sheet==null) return array;
if (sheet == null) return array;
for (Row row : sheet) {
rowIndex++;
if (rowIndex == 1) continue;//第一行为表头
if (row == null) break;
if (row.getCell(0) == null) break;
if (StrKit.isBlank(readCell(row.getCell(0)))){
if (StrKit.isBlank(readCell(row.getCell(0)))) {
break;
}
// 遍历行中的所有单元格
@ -140,6 +140,7 @@ public class ExcelKit {
/**
*
*
* @param cell
*/
public static String readCell(Cell cell) {
@ -188,6 +189,7 @@ public class ExcelKit {
return cell.toString();
}
}
/**
*
*
@ -202,44 +204,58 @@ public class ExcelKit {
}
}
/**
*
*
* @param outRow
* @param stringList
*/
public static void putData(Row outRow, List<String> stringList, XSSFCellStyle dataStyle) {
for (int i = 0; i < stringList.size(); i++) {
outRow.createCell(i).setCellValue(stringList.get(i));
}
//应用一下样式
applyStyle(outRow, 0, stringList.size()-1, dataStyle);
}
/**
* List
* @param affCharts doc.getCharts()
*
* @param affCharts doc.getCharts()
* @return
*/
public static List<XWPFChart> getSortListForXWPFChart(List<XWPFChart> affCharts) {
List<XWPFChart> charts = new ArrayList<>();
int itNumber = 0; //计数器
int oldNumber = 0; //入参计数器
while (itNumber < affCharts.size()){
while (itNumber < affCharts.size()) {
// 从oldCharts.get(0)开始检索,获取排序用图表名
String name = affCharts.get(oldNumber).getPackagePart().getPartName().toString();
// 获取此图表排序
String chartsNum = "";// 图表序号
boolean flag = false; // 上一个是否为数字
for (int i = 0; i < name.length(); i++) {
if (chartsNum.equals("") && name.charAt(i)>=48 && name.charAt(i)<=57 ){
if (chartsNum.equals("") && name.charAt(i) >= 48 && name.charAt(i) <= 57) {
chartsNum += name.charAt(i);
flag = true;
}else if ( flag && name.charAt(i)>=48 && name.charAt(i)<=57){
} else if (flag && name.charAt(i) >= 48 && name.charAt(i) <= 57) {
chartsNum += name.charAt(i);
flag = true;
}else {
} else {
flag = false;
}
}
//对比图表序号数字
int thisChartNum = Integer.parseInt(chartsNum);
if (thisChartNum == itNumber+1){ //如果相等则加入返回list,且itNumber++
if (thisChartNum == itNumber + 1) { //如果相等则加入返回list,且itNumber++
charts.add(affCharts.get(oldNumber));
itNumber++;
}
//入参计数器+1 如果达到最大值则重置为0
if (oldNumber == affCharts.size()-1){
if (oldNumber == affCharts.size() - 1) {
oldNumber = 0;
}else{
} else {
oldNumber++;
}
}

Loading…
Cancel
Save