单文件单页转换:
File file = new File("test.xls");
Workbook wb = new Workbook(file.getPath());
PdfSaveOptions opts = new PdfSaveOptions();
opts.setOnePagePerSheet(true);
wb.save(pdfFile.getPath(), opts);
单文件多页转换:
由于很多操作人员,就喜欢没事找事,把一些其他的插件做出来的文件,转换成excel,那么会有很多隐藏的空格啊,或者一些其他乱七八糟隐藏的数据,每次转换的时候都会少一行,特别的加了一行代码。
maxRow = maxRow + 1;
String result = "";
Workbook wb = new Workbook(file.getPath());
// 设置打印区域为null,否则转为pdf时,有些excel数据会丢失。
WorksheetCollection ws = wb.getWorksheets();
for (int i = 0; i < ws.getCount(); i++) {
Worksheet sWorksheet = ws.get(i);
PageSetup pageSetup = sWorksheet.getPageSetup();
// pageSetup.setBlackAndWhite(true); 当前也设置为黑白
int rowNum = sWorksheet.getCells().getMaxRow();
// System.out.println("rowNum:"+rowNum);
int colNum = sWorksheet.getCells().getMaxColumn();
System.out.println("行:" + rowNum + ",列:" + colNum);
// 行数超过,或者列数超过,设置默认值
if (rowNum > MAX_ROW_THRESHOLD || colNum > MAX_COL_THRESHOLD) {
rowNum = MAX_ROW_THRESHOLD;
colNum = MAX_COL_THRESHOLD;
}
System.out.println("行:" + rowNum + ",列:" + colNum);
// 自适应列宽
// sWorksheet.autoFitColumns();
// sWorksheet.autoFitRows();
Cells cells = sWorksheet.getCells();
int maxRow = 0;
int maxCol = 0;
for (int m = rowNum; m >= 0; m--) {
boolean isBlankRow = true;
int maxCurRow = m;
for (int n = colNum; n >= 0; n--) {
com.aspose.cells.Cell cell = cells.get(m, n);
String cellValue = cell.getStringValue();
if ("127510.02".equals(cellValue)) {
System.out.println(cellValue);
System.out.println(cell.getValue());
}
Style style = cells.get(m, n).getStyle();
com.aspose.cells.Font font = style.getFont();
// 字体颜色为白色的时候,为逻辑删除,过滤掉
if (!font.getColor().isEmpty()) {
if (font.getColor().toArgb() == 16777215) {
cell.putValue("");
}
}
int curCol = n;
Range rangeExpend = cell.getMergedRange();
if (rangeExpend != null) {
curCol = rangeExpend.getFirstColumn() + rangeExpend.getColumnCount();
int curRowExpend = rangeExpend.getFirstRow() + rangeExpend.getRowCount();
if (curRowExpend > maxCurRow) {
maxCurRow = curRowExpend;
}
}
if (cellValue != null && !cellValue.equals("")) {
if (curCol > maxCol) {
maxCol = curCol;
}
isBlankRow = false;
}
}
if (!isBlankRow && maxCurRow > maxRow) {
maxRow = maxCurRow;
}
}
// sw------针对excel 存在图片进行收集
PictureCollection pt = sWorksheet.getPictures();
// 赛选图片最大值
int maxPicRow = 0;
int maxPicColumn = 0;
for (int picI = 0; picI < pt.getCount(); picI++) {
Picture pic = pt.get(picI);
int LowerRightRow = pic.getLowerRightRow();
int LowerRightColumn = pic.getLowerRightColumn();
if (maxPicRow < LowerRightRow) {
maxPicRow = LowerRightRow;
}
if (maxPicColumn < LowerRightColumn) {
maxPicColumn = LowerRightColumn;
}
}
// 判断原来的行与列是否超过了 图片
if (maxRow < maxPicRow) {
maxRow = maxPicRow;
}
if (maxCol < maxPicColumn) {
maxCol = maxPicColumn;
}
maxCol = maxCol + 2; // 临时处理+2列
maxRow = maxRow + 1;
log.info(file.getName() + "的第" + (i + 1) &