Aspose.Cells转Pdf正确姿势

单文件单页转换:

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) &
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛-承哥

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值