|
|
|
@ -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 {
|
|
|
|
|
/**
|
|
|
|
|
* 将xls的转换为xlsx,数据样式和格式一同转换,并输出为文件流
|
|
|
|
|
*
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|