diff --git a/ExtendJar/aspose-cells-23.4.jar b/ExtendJar/aspose-cells-23.4.jar new file mode 100644 index 00000000..b51ea697 Binary files /dev/null and b/ExtendJar/aspose-cells-23.4.jar differ diff --git a/src/main/java/UnitTest/ImportExcel/AsposeAddColumn.java b/src/main/java/UnitTest/ImportExcel/AsposeAddColumn.java new file mode 100644 index 00000000..880eeac3 --- /dev/null +++ b/src/main/java/UnitTest/ImportExcel/AsposeAddColumn.java @@ -0,0 +1,109 @@ +package UnitTest.ImportExcel; + +import com.aspose.cells.*; + +import java.io.IOException; +import java.io.InputStream; + +public class AsposeAddColumn { + /** + * 功能: AsposeCells破解办法 + * + * @return + */ + public static void getLicense() throws IOException { + InputStream is = com.aspose.cells.License.class.getResourceAsStream("/com.aspose.cells.lic_2999.xml"); + License asposeLicense = new License(); + asposeLicense.setLicense(is); + is.close(); + } + + public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\"; + + /** + * 功能:扩展汇总表的前两列 + * + * @param source + * @param target + * @param colorString + * @throws Exception + */ + public static void expandColumn(String source, String target, String colorString, int headFirstRow, int headTotalRows) throws Exception { + Workbook wb = new Workbook(source); + Worksheet sheet = wb.getWorksheets().get(0); + //添加列 + Cells cells = sheet.getCells(); + //在第一列前增加两列,一列是序号,另一列是单位名称 + int columnIndex = 0, totalColumns = 2;//写死 + // 表头所在行,共几行 + cells.insertColumns(columnIndex, totalColumns); + // 设置列宽 + int colWidth = 120; + cells.setColumnWidthPixel(columnIndex, colWidth); + cells.setColumnWidthPixel(columnIndex + 1, colWidth); + + //第一行表头的前三个单元格合并 + // 取消指定范围的单元格合并 + int columnCount = cells.getMaxDataColumn() + 1; + cells.unMerge(0, 0, 1, columnCount); // 取消单元格合并 + cells.merge(0, 0, 1, columnCount + 2); // 合并第一行的前三个单元格 + // 设置水平和垂直居中 + Style style = cells.get(0, 0).getStyle(); + + //居中 + style.setHorizontalAlignment(TextAlignmentType.CENTER); + style.setVerticalAlignment(TextAlignmentType.CENTER); + cells.get(0, 0).setStyle(style); + + // 第一列:序号,合并表头所在所有行的第一列 + totalColumns = 1; //上下多行合并,就是一列 + for (int firstColumn = 0; firstColumn <= 1; firstColumn++) { + sheet.getCells().merge(headFirstRow, firstColumn, headTotalRows, totalColumns); + } + //表头 + cells.get(headFirstRow, 0).setValue("序号"); + cells.get(headFirstRow, 1).setValue("单位"); + // 设置背景色为给定颜色 + style = wb.createStyle(); + // 设置字体 + style.getFont().setBold(true); + style.getFont().setName("宋体"); + style.getFont().setSize(12); + int red = Integer.parseInt(colorString.substring(1, 3), 16); + int green = Integer.parseInt(colorString.substring(3, 5), 16); + int blue = Integer.parseInt(colorString.substring(5, 7), 16); + Color color = Color.fromArgb(red, green, blue); + style.setForegroundColor(color); + style.setPattern(BackgroundType.SOLID); + // 设置居中 + style.setHorizontalAlignment(TextAlignmentType.CENTER); // 设置水平居中 + style.setVerticalAlignment(TextAlignmentType.CENTER); // 设置垂直居中 + + // 获取合并后的单元格 + style.setBorder(BorderType.TOP_BORDER, CellBorderType.THIN, Color.getBlack()); + style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THIN, Color.getBlack()); + style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THIN, Color.getBlack()); + style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THIN, Color.getBlack()); + + for (int i = headFirstRow; i < headFirstRow + headTotalRows; i++) { + Row row = sheet.getCells().getRow(i); + Cell cell = row.get(0); + cell.setStyle(style); + cell = row.get(1); + cell.setStyle(style); + } + //保存 + wb.save(target); + } + + public static void main(String[] args) throws Exception { + //获取授权 + getLicense(); + + //给EXCEL增加两列 + String source = path + "1.xlsx"; + String target = path + "2.xlsx"; + int headFirstRow = 2, headTotalRows = 2; + expandColumn(source, target, "#DAEEF3", headFirstRow, headTotalRows); + } +} diff --git a/src/main/java/UnitTest/ImportExcel/TestTitle.java b/src/main/java/UnitTest/ImportExcel/TestTitle.java index fd12e4ad..08ef375e 100644 --- a/src/main/java/UnitTest/ImportExcel/TestTitle.java +++ b/src/main/java/UnitTest/ImportExcel/TestTitle.java @@ -135,12 +135,23 @@ public class TestTitle { } com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(source); Worksheet sheet = wb.getWorksheets().get(0); + //添加列 Cells cells = sheet.getCells(); cells.insertColumns(0, 2);//在第一列前增加两列,一列是序号,另一列是单位名称 - // 合并单元格:第三行、第四行的第一列 sheet.getCells().merge(2, 0, 2, 1); + // 设置背景色为黄色 + Style style = wb.createStyle(); + style.setForegroundColor(com.aspose.cells.Color.getYellow()); + style.setPattern(BackgroundType.SOLID); + + // 获取合并后的单元格 + com.aspose.cells.Row row = sheet.getCells().getRow(2); + com.aspose.cells.Cell cell = row.getFirstCell(); + + //应用样式 + cell.setStyle(style); //保存 wb.save(target); } diff --git a/src/main/resource/Excel/1.xlsx b/src/main/resource/Excel/1.xlsx index f6a0be9d..797dc3dc 100644 Binary files a/src/main/resource/Excel/1.xlsx and b/src/main/resource/Excel/1.xlsx differ diff --git a/src/main/resource/Excel/2.xlsx b/src/main/resource/Excel/2.xlsx index f7d79e02..72549156 100644 Binary files a/src/main/resource/Excel/2.xlsx and b/src/main/resource/Excel/2.xlsx differ