main
黄海 8 months ago
parent 2535d9b769
commit 83fda850fc

@ -3,14 +3,39 @@
源文件中第一列开始查找 市州 名称,一般是最后一行不空的行,
学前第I列
学校总数:
招生总数:
在校生总数:
教职工总数:
专任教师数:
小学第S列
初中第AD列
普通高中第AN列
中职第AU列
学前:
学校总数: I
招生总数: O
在校生总数P
教职工总数M
专任教师数N
小学:
学校总数: S
招生总数: Z
在校生总数AA
教职工总数X
专任教师数Y
初中:
学校总数:
招生总数: AD
在校生总数AK
教职工总数AH
专任教师数AI
高中:
学校总数: AN
招生总数: AQ
在校生总数AR
教职工总数AO
专任教师数AP
中职:
学校总数: AU
招生总数: AX
在校生总数AY
教职工总数AV
专任教师数AW

@ -2,6 +2,7 @@ package com.dsideal.base.Tools.FillData.City;
import cn.hutool.core.io.FileUtil;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelUtil;
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@ -27,7 +28,8 @@ public class C7_Test {
static String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\概览.xlsx";
//源文件
String sourceExcel="D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\(昭通市)汇总表.xls";
static String sourceExcel = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\(昭通市)汇总表.xls";
public static void main(String[] args) throws IOException, InvalidFormatException {
//初始化数据库连接
LocalMysqlConnectUtil.Init();
@ -49,6 +51,55 @@ public class C7_Test {
//拷贝文件头
ExcelKit.CopyHead(sampleExcelPath, outSheet, headerStyle);
//1、用POI打开指定文件EXCEL获取它的最后一个有效数据行就是这个城市的整体数据汇总
List<List<String>> sheetList = ExcelKit.readSheet(sourceExcel);
System.out.println(sheetList);
/*2
I
O
P
M
N
S
Z
AA
X
Y
AD
AK
AH
AI
AN
AQ
AR
AO
AP
AU
AX
AY
AV
AW
*/
List<String> stageList = new ArrayList<>();
stageList.add("学前");
stageList.add("小学");
stageList.add("初中");
stageList.add("高中");
stageList.add("中职");
// for(String stage:stageList){
// //按规则获取当前学段中的学校总数,招生总数,在校生总数,教职工总数,专任教师数
// String schoolCount=ExcelKit.transLetter2Num("I");
//
// }
//保存文件
ExcelKit.saveExcel(excelPath, outWorkbook);

@ -9,10 +9,7 @@ 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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@ -109,10 +106,19 @@ public class ExcelKit {
}
/**
* @param workbook
* @return
* @throws IOException
*/
public static List<List<String>> readSheet(String sourceExcel) throws IOException {
//如果文件是xls格式则转换为xlsx格式
if (sourceExcel.endsWith(".xls")) {
ExcelUtil.xlsChangeXlsx(sourceExcel, sourceExcel + "x");
sourceExcel = sourceExcel + "x";
//删除旧的文件
delExcel(sourceExcel);
}
return readSheet(new XSSFWorkbook(new FileInputStream(sourceExcel)));
}
public static List<List<String>> readSheet(XSSFWorkbook workbook) throws IOException {
XSSFSheet sheet = workbook.getSheet("Sheet1");
List<List<String>> array = new ArrayList<>();
@ -217,7 +223,7 @@ public class ExcelKit {
outRow.createCell(i).setCellValue(stringList.get(i));
}
//应用一下样式
applyStyle(outRow, 0, stringList.size()-1, dataStyle);
applyStyle(outRow, 0, stringList.size() - 1, dataStyle);
}
/**
@ -266,6 +272,7 @@ public class ExcelKit {
/**
*
*
* @param excelPath
* @param outWorkbook
* @throws IOException
@ -275,6 +282,7 @@ public class ExcelKit {
outWorkbook.write(fileOut);
outWorkbook.close();
}
/**
* excel
*
@ -304,4 +312,6 @@ public class ExcelKit {
}
return --rlt;
}
}

@ -0,0 +1,239 @@
package com.dsideal.base.Tools.FillData.ExcelKit;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ExcelUtil {
/**
* xlsxlsx
*
* @param inputFilePath Excel
* @param outputFilePath Excel
* @throws IOException
*/
public static void xlsChangeXlsx(String inputFilePath, String outputFilePath) throws IOException {
// 使用 try-with-resources 自动管理资源
try (FileInputStream fis = new FileInputStream(inputFilePath)) {
// 获得xls模板
HSSFWorkbook wb = new HSSFWorkbook(fis);
XSSFWorkbook swb = new XSSFWorkbook();
for (int i = 0; i < wb.getNumberOfSheets(); ++i) {
HSSFSheet sheet = wb.getSheetAt(i);
// 复制sheet合并栏和冻结窗格之类
Sheet sheet1 = copySheet(wb, swb, i);
// 写入xls模板
ExcelUtil builder = new ExcelUtil(wb);
int rowNum = sheet.getLastRowNum();
// 复制单元格值与样式
builder.copyRows(swb, sheet, sheet1, 0, rowNum + 1, 0);
}
try (FileOutputStream fos = new FileOutputStream(outputFilePath)) {
swb.write(fos);
fos.flush();
System.out.println("转换成功,文件已保存至:" + outputFilePath);
}
}
}
private Workbook template = null;
private final Map<Integer, Font> fonts = new HashMap<>();
private final Map<Integer, CellStyle> styles = new HashMap<>();
public ExcelUtil(Workbook template) {
this.template = template;
}
public void switchStyles(Workbook dstWorkbook, CellStyle[] styles) {
for (int i = 0; i < styles.length; i++) {
styles[i] = getStyle(dstWorkbook, styles[i]);
}
}
private Font getFont(Workbook dstWorkbook, Font font) {
return fonts.computeIfAbsent(font.hashCode(), k -> cloneFont(dstWorkbook, font));
}
private CellStyle getStyle(Workbook dstWorkbook, CellStyle style) {
Font font = getFont(dstWorkbook, template.getFontAt(style.getFontIndexAsInt()));
return styles.computeIfAbsent(style.hashCode(), k -> cloneStyle(dstWorkbook, style, dstWorkbook.createDataFormat(), font));
}
public void copyRows(Workbook dstWorkbook, Sheet srcSheet, Sheet dstSheet, int from, int to, int offset) {
for (int r = from; r < to; r++) {
Row srcRow = srcSheet.getRow(r);
if (srcRow != null) {
CellStyle style = srcRow.getRowStyle();
Row dstRow = dstSheet.createRow(r + offset);
dstRow.setHeight(srcRow.getHeight());
if (style != null) {
dstRow.setRowStyle(getStyle(dstWorkbook, style));
}
for (int c = 0; c < srcRow.getLastCellNum(); c++) {
Cell srcCell = srcRow.getCell(c);
if (srcCell != null) {
CellType type = getCellType(srcCell);
Object value = getCellValue(srcCell);
style = srcCell.getCellStyle();
Cell newCell = dstRow.createCell(c, type);
setCellValue(newCell, value, type);
newCell.setCellStyle(getStyle(dstWorkbook, style));
}
}
}
}
}
public static Sheet copySheet(Workbook srcWorkbook, Workbook dstWorkbook, int sheetIndex) {
Sheet srcSheet = srcWorkbook.getSheetAt(sheetIndex);
Sheet dstSheet = dstWorkbook.createSheet(srcSheet.getSheetName());
dstSheet.setDisplayFormulas(srcSheet.isDisplayFormulas());
dstSheet.setDisplayGridlines(srcSheet.isDisplayGridlines());
dstSheet.setDisplayGuts(srcSheet.getDisplayGuts());
dstSheet.setDisplayRowColHeadings(srcSheet.isDisplayRowColHeadings());
dstSheet.setDisplayZeros(srcSheet.isDisplayZeros());
dstSheet.setFitToPage(srcSheet.getFitToPage());
dstSheet.setForceFormulaRecalculation(srcSheet.getForceFormulaRecalculation());
dstSheet.setHorizontallyCenter(srcSheet.getHorizontallyCenter());
dstSheet.setMargin(Sheet.BottomMargin, srcSheet.getMargin(Sheet.BottomMargin));
dstSheet.setMargin(Sheet.FooterMargin, srcSheet.getMargin(Sheet.FooterMargin));
dstSheet.setMargin(Sheet.HeaderMargin, srcSheet.getMargin(Sheet.HeaderMargin));
dstSheet.setMargin(Sheet.LeftMargin, srcSheet.getMargin(Sheet.LeftMargin));
dstSheet.setMargin(Sheet.RightMargin, srcSheet.getMargin(Sheet.RightMargin));
dstSheet.setMargin(Sheet.TopMargin, srcSheet.getMargin(Sheet.TopMargin));
dstSheet.setPrintGridlines(srcSheet.isPrintGridlines());
dstSheet.setRightToLeft(srcSheet.isRightToLeft());
dstSheet.setRowSumsBelow(srcSheet.getRowSumsBelow());
dstSheet.setRowSumsRight(srcSheet.getRowSumsRight());
dstSheet.setVerticallyCenter(srcSheet.getVerticallyCenter());
for (int i = 0; i < 20; i++) {
dstSheet.setColumnWidth(i, srcSheet.getColumnWidth(i));
dstSheet.setColumnHidden(i, srcSheet.isColumnHidden(i));
}
srcSheet.getMergedRegions().forEach(dstSheet::addMergedRegion);
Drawing<?> d1 = srcSheet.getDrawingPatriarch();
if (d1 != null) {
Drawing<?> d2 = dstSheet.getDrawingPatriarch();
if (d2 == null) {
d2 = dstSheet.createDrawingPatriarch();
}
for (Shape shape : d1) {
if (shape instanceof Picture) {
Picture p = (Picture) shape;
ClientAnchor a1 = p.getClientAnchor();
int pictureId = dstWorkbook.addPicture(p.getPictureData().getData(), p.getPictureData().getPictureType());
ClientAnchor a2 = d2.createAnchor(a1.getDx1(), a1.getDy1(), a1.getDx2(), a1.getDy2(), a1.getCol1(), a1.getRow1(), a1.getCol2(), a1.getRow2());
d2.createPicture(a2, pictureId);
}
}
}
return dstSheet;
}
public static Font cloneFont(Workbook dstWorkbook, Font font) {
Font clone = dstWorkbook.createFont();
clone.setBold(font.getBold());
clone.setCharSet(font.getCharSet());
clone.setColor(font.getColor());
clone.setFontHeight(font.getFontHeight());
clone.setFontName(font.getFontName());
clone.setItalic(font.getItalic());
clone.setStrikeout(font.getStrikeout());
clone.setTypeOffset(font.getTypeOffset());
clone.setUnderline(font.getUnderline());
return clone;
}
public static CellStyle cloneStyle(Workbook dstWorkbook, CellStyle style, DataFormat formatter, Font font) {
CellStyle clone = dstWorkbook.createCellStyle();
clone.setAlignment(style.getAlignment());
clone.setBorderBottom(style.getBorderBottom());
clone.setBorderLeft(style.getBorderLeft());
clone.setBorderRight(style.getBorderRight());
clone.setBorderTop(style.getBorderTop());
// 复制数据格式
String formatString = style.getDataFormatString();
DataFormat targetDataFormat = dstWorkbook.createDataFormat();
short targetFormatIndex = targetDataFormat.getFormat(formatString);
clone.setDataFormat(targetFormatIndex);
clone.setDataFormat(formatter.getFormat(style.getDataFormatString()));
clone.setFillBackgroundColor(style.getFillBackgroundColor());
clone.setFillForegroundColor(style.getFillForegroundColor());
clone.setFillPattern(style.getFillPattern());
clone.setFont(font);
clone.setHidden(style.getHidden());
clone.setIndention(style.getIndention());
clone.setLocked(style.getLocked());
clone.setVerticalAlignment(style.getVerticalAlignment());
clone.setWrapText(style.getWrapText());
return clone;
}
protected static CellType getCellType(Cell cell) {
CellType cellType = cell.getCellType();
if (cellType == CellType.FORMULA) {
cellType = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator()
.evaluateFormulaCell(cell);
}
return cellType;
}
protected static Object getCellValue(Cell cell) {
switch (getCellType(cell)) {
case BLANK:
case STRING:
return cell.getStringCellValue();
case BOOLEAN:
return cell.getBooleanCellValue();
case ERROR:
return cell.getErrorCellValue();
case NUMERIC:
return cell.getNumericCellValue();
}
return null;
}
protected static void setCellValue(Cell cell, Object value, CellType type) {
switch (type) {
case BLANK:
return;
case STRING:
cell.setCellValue((String) value);
return;
case BOOLEAN:
cell.setCellValue((Boolean) value);
return;
case ERROR:
cell.setCellErrorValue((Byte) value);
return;
case NUMERIC:
if (value instanceof Double) {
Double d = (Double) value;
// 判断是否为整数,如果是整数,转换为 long否则保留小数
if (d == Math.floor(d)) {
cell.setCellValue(String.valueOf(d.longValue())); // 整数时去掉 .0
} else {
cell.setCellValue(d); // 保留小数
}
}
return;
default:
break;
}
}
}

@ -0,0 +1,14 @@
package com.dsideal.base.Tools.FillData.Test;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelUtil;
import java.io.IOException;
public class ConvertXls {
public static void main(String[] args) throws IOException {
String inputFile = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\(昭通市)汇总表.xls";
String outputFile = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\(昭通市)汇总表.xlsx";
ExcelUtil.xlsChangeXlsx(inputFile, outputFile);
}
}
Loading…
Cancel
Save