main
黄海 2 years ago
parent 66be4ac1a2
commit 741f4ce20d

@ -0,0 +1,19 @@
package UnitTest;
import com.dsideal.FengHuang.Util.ExcelCommonUtil;
import java.util.ArrayList;
public class TestMergeExcel {
public static void main(String[] args) {
//这里是xls文件
String[] filePaths = {"c:/Test/7840.xlsx","c:/Test/8210.xlsx"};
ArrayList<String> list = new ArrayList<>();
for (String path : filePaths) {
list.add(path);
}
String path = "c:/Test";
String fileName = "Result.xlsx";
ExcelCommonUtil.mergeExcel(list, path, fileName);
}
}

@ -843,4 +843,169 @@ public class ExcelCommonUtil {
}
sheet.removeMergedRegion(index);//移除合并单元格
}
/**
* #excel
*
* @param fileLists excel
* @param path
* @param fileName
*/
public static void mergeExcel(List<String> fileLists, String path, String fileName) {
// 创建新的excel工作簿
XSSFWorkbook newExcelWorkBook = new XSSFWorkbook();
// 遍历需要合并的excel文件
for (String excelName : fileLists) {
try (InputStream in = new FileInputStream(excelName)) {
// 创建工作簿
XSSFWorkbook tmpWorkBook = new XSSFWorkbook(in);
// 获取工作簿中的Sheet个数
int len = tmpWorkBook.getNumberOfSheets();
if (len <= 1) {
XSSFSheet tmpSheet = tmpWorkBook.getSheetAt(0);
XSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName());
// 复制sheet内容
copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet);
} else {
for (int i = 0; i < len; i++) {
XSSFSheet tmpSheet = tmpWorkBook.getSheetAt(i);
XSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName());
// 复制sheet内容
copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet);
}
}
// 关闭tmpWorkBook工作簿
tmpWorkBook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 新生成的excel文件
if (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls")) {
fileName += ".xlsx";
}
String excelFileName = path + File.separator + fileName;
// 判断文件是否存在
File excelFile = new File(excelFileName);
if (excelFile.exists()) {
// 存在则删除
excelFile.delete();
}
// 使用输出流写出
try (FileOutputStream fos = new FileOutputStream(excelFileName)) {
newExcelWorkBook.write(fos);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
newExcelWorkBook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("excel文件合并成功合并后文件路径" + excelFileName);
}
/**
* #sheetexcel
*
* @param workbook excel簿
* @param tmpSheet sheet
* @param newExcelSheet sheet
*/
public static void copyExcelSheet(XSSFWorkbook workbook, XSSFSheet tmpSheet, XSSFSheet newExcelSheet) {
// 合并单元格
mergeSheetAllRegion(tmpSheet, newExcelSheet);
// 设置单元格列宽度
// 获取最后一个单元格位置
int len = tmpSheet.getRow(tmpSheet.getFirstRowNum()).getLastCellNum();
for (int i = 0; i < len; i++) {
newExcelSheet.setColumnWidth(i, tmpSheet.getColumnWidth(i));
}
// 复制每行内容
Iterator<Row> it = tmpSheet.iterator();
while (it.hasNext()) {
XSSFRow tmpRow = (XSSFRow) it.next();
// 创建新行
XSSFRow newExcelRow = newExcelSheet.createRow(tmpRow.getRowNum());
// 复制行
copyExcelRow(workbook, tmpRow, newExcelRow);
}
}
/**
* #
*
* @param tmpSheet sheet
* @param newExcelSheet sheet
*/
private static void mergeSheetAllRegion(XSSFSheet tmpSheet, XSSFSheet newExcelSheet) {
int num = tmpSheet.getNumMergedRegions();
CellRangeAddress cellRange = null;
for (int i = 0; i < num; i++) {
cellRange = tmpSheet.getMergedRegion(i);
newExcelSheet.addMergedRegion(cellRange);
}
}
/**
* #excelsheet
*
* @param workbook 簿
* @param tmpRow excel
* @param newExcelRow excel
*/
public static void copyExcelRow(XSSFWorkbook workbook, XSSFRow tmpRow, XSSFRow newExcelRow) {
// 设置行高
newExcelRow.setHeight(tmpRow.getHeight());
// 获取所有列
Iterator<Cell> it = tmpRow.cellIterator();
while (it.hasNext()) {
XSSFCell tmpCell = (XSSFCell) it.next();
// 创建单元格
XSSFCell newExcelCell = newExcelRow.createCell(tmpCell.getColumnIndex());
// 复制单元格
copyExcelCell(workbook, tmpCell, newExcelCell);
}
}
/**
* #
*
* @param workbook 簿
* @param tmpCell excel
* @param newExcelCell excel
*/
public static void copyExcelCell(XSSFWorkbook workbook, XSSFCell tmpCell, XSSFCell newExcelCell) {
XSSFCellStyle newExcelStyle = workbook.createCellStyle();
// 复制单元格样式
newExcelStyle.cloneStyleFrom(tmpCell.getCellStyle());
// 单元格样式
newExcelCell.setCellStyle(newExcelStyle);
if (tmpCell.getCellComment() != null) {
newExcelCell.setCellComment(tmpCell.getCellComment());
}
// 不同数据类型处理
CellType tmpCellType = tmpCell.getCellType();
newExcelCell.setCellType(tmpCellType);
if (tmpCellType == CellType.NUMERIC) {
if (DateUtil.isCellDateFormatted(tmpCell)) {
newExcelCell.setCellValue(tmpCell.getDateCellValue());
} else {
newExcelCell.setCellValue(tmpCell.getNumericCellValue());
}
} else if (tmpCellType == CellType.STRING) {
newExcelCell.setCellValue(tmpCell.getRichStringCellValue());
} else if (tmpCellType == CellType.BLANK) {
} else if (tmpCellType == CellType.BOOLEAN) {
newExcelCell.setCellValue(tmpCell.getBooleanCellValue());
} else if (tmpCellType == CellType.ERROR) {
newExcelCell.setCellErrorValue(tmpCell.getErrorCellValue());
} else if (tmpCellType == CellType.FORMULA) {
newExcelCell.setCellFormula(tmpCell.getCellFormula());
} else {
}
}
}

@ -253,7 +253,6 @@ public class YpController extends Controller {
renderJson(CommonUtil.returnMessageJson(false, "上传文件类型错误系统只允许上传jpg格式"));
return;
}
String uuid = UUID.randomUUID().toString();
//判断目录是不是存在
File file = new File(PathKit.getWebRootPath() + "/upload");
@ -269,7 +268,6 @@ public class YpController extends Controller {
kv.set("base64", base64);
kv.set("uuid", uuid);
renderJson(kv);
//path : /FengHuang/upload/sfzh+".jpg"
}

Loading…
Cancel
Save