main
黄海 8 months ago
parent 0980d4259f
commit 4596cd263f

@ -68,8 +68,9 @@ public class C1 {
ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument doc = new XWPFDocument(is);
//图表
List<XWPFChart> charts = doc.getCharts();
//排序后的图表
List<XWPFChart> charts = ExcelKit.getSortListForXWPFChart(doc.getCharts());
//1号模板数据在图表5和28中
XSSFWorkbook workbook = charts.get(4).getWorkbook();
List<List<String>> source1 = ExcelKit.readSheet(workbook);
@ -78,7 +79,6 @@ public class C1 {
List<List<String>> source2 = ExcelKit.readSheet(workbook2);
//遍历source1
for (int i = 0; i < source1.size(); i++) {
List<String> r = source1.get(i);
rowIndex++;
@ -104,7 +104,14 @@ public class C1 {
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("总入园数");//总量分类
outRow.createCell(2).setCellValue("");//区域分类
outRow.createCell(3).setCellValue(r.get(4));//总量数值
if (4 < r.size()) {
outRow.createCell(3).setCellValue(r.get(4));//总量数值
} else {
System.out.println(cityName);
outRow.createCell(3).setCellValue("");//总量数值
}
outRow.createCell(4).setCellValue("");//区域数值
outRow.createCell(5).setCellValue(cityName);//行政区划
outRow.createCell(6).setCellValue("云南省");//上级行政区划
@ -120,7 +127,13 @@ public class C1 {
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("2022年基数");//基数
outRow.createCell(2).setCellValue("");//区域分类
outRow.createCell(3).setCellValue(source2.getFirst().get(4));//总量数值
if (4 < source2.getFirst().size()) {
outRow.createCell(3).setCellValue(source2.getFirst().get(4));//总量数值
} else {
outRow.createCell(3).setCellValue("");//总量数值
}
outRow.createCell(4).setCellValue("");//区域数值
outRow.createCell(5).setCellValue(cityName);//行政区划
outRow.createCell(6).setCellValue("云南省");//上级行政区划
@ -161,7 +174,12 @@ public class C1 {
outRow.createCell(1).setCellValue("");//基数
outRow.createCell(2).setCellValue(areaName);//区域分类
outRow.createCell(3).setCellValue("");//总量数值
outRow.createCell(4).setCellValue(r.get(k + 1));//区域数值
if (k + 1 < r.size()) {
outRow.createCell(4).setCellValue(r.get(k + 1));//区域数值
} else {
outRow.createCell(4).setCellValue("");//区域数值
}
outRow.createCell(5).setCellValue(cityName);//行政区划
outRow.createCell(6).setCellValue("云南省");//上级行政区划
//应用一下样式

@ -7,6 +7,7 @@ import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import java.io.File;
import java.io.IOException;
@ -116,6 +117,7 @@ public class ExcelKit {
//遍历输出sheet的内容
int rowIndex = 0;
// 遍历工作表中的所有行
if(sheet==null) return array;
for (Row row : sheet) {
rowIndex++;
if (rowIndex == 1) continue;//第一行为表头
@ -199,4 +201,48 @@ public class ExcelKit {
outRow.getCell(i).setCellStyle(dataStyle);
}
}
/**
* List
* @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()){
// 从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 ){
chartsNum += name.charAt(i);
flag = true;
}else if ( flag && name.charAt(i)>=48 && name.charAt(i)<=57){
chartsNum += name.charAt(i);
flag = true;
}else {
flag = false;
}
}
//对比图表序号数字
int thisChartNum = Integer.parseInt(chartsNum);
if (thisChartNum == itNumber+1){ //如果相等则加入返回list,且itNumber++
charts.add(affCharts.get(oldNumber));
itNumber++;
}
//入参计数器+1 如果达到最大值则重置为0
if (oldNumber == affCharts.size()-1){
oldNumber = 0;
}else{
oldNumber++;
}
}
return charts;
}
}

Loading…
Cancel
Save