|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|