【问题标题】:Read Apache POI XSSFWorkbook in Aspose cells在 Aspose 单元格中阅读 Apache POI XSSFWorkbook
【发布时间】:2018-07-05 06:42:10
【问题描述】:

我正在尝试在 JAVA 中创建带水印的 excel 文件。

我正在使用以下方法: 1.使用Apache POI api创建excel工作簿 2.使用aspose cells api中的poi工作簿添加watermar。

当我尝试在 aspose 单元格中使用 POI 工作簿时,我收到错误 - 工作簿不是预期的。请帮忙,因为我是 spring/JAVA 的新手

PFB 我的代码:

package com.mudassir.exceltest.testExcel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class TestExcelApplication {

    private static String[] COLUMNs = {"Id", "Name", "Address", "Age"};
    private static List<Customer> customers = Arrays.asList(
            new Customer("1", "Jack Smith", "Massachusetts", 23),
            new Customer("2", "Adam Johnson", "New York", 27),
            new Customer("3", "Katherin Carter", "Washington DC", 26),
            new Customer("4", "Jack London", "Nevada", 33), 
            new Customer("5", "Jason Bourne", "California", 36));

    public static void main(String[] args) throws IOException {

        Workbook workbook = new XSSFWorkbook();
        CreationHelper createHelper = workbook.getCreationHelper();

        Sheet sheet = workbook.createSheet("Customers");

        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setColor(IndexedColors.BLUE.getIndex());

        CellStyle headerCellStyle = workbook.createCellStyle();
        headerCellStyle.setFont(headerFont);

        // Row for Header
        Row headerRow = sheet.createRow(0);

        // Header
        for (int col = 0; col < COLUMNs.length; col++) {
            if(col== 0){
            Cell cell = headerRow.createCell(col);
            cell.setCellValue(COLUMNs[col]);
            cell.setCellStyle(headerCellStyle);
            }
            else{
                Cell cell = headerRow.createCell(col+1);
                cell.setCellValue(COLUMNs[col]);
                cell.setCellStyle(headerCellStyle);
            }

        }

        // CellStyle for Age
        CellStyle ageCellStyle = workbook.createCellStyle();
        ageCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#"));

        int rowIdx = 1;
        for (Customer customer : customers) {
            Row row = sheet.createRow(rowIdx++);

            row.createCell(0).setCellValue(customer.getId());
            row.createCell(2).setCellValue(customer.getName());
            row.createCell(3).setCellValue(customer.getAddress());

            Cell ageCell = row.createCell(4);
            ageCell.setCellValue(customer.getAge());
            ageCell.setCellStyle(ageCellStyle);
        }

        // read the image to the stream
        final FileInputStream stream = new FileInputStream("image.png");
        final CreationHelper helper = workbook.getCreationHelper();
        final Drawing drawing = sheet.createDrawingPatriarch();

        final ClientAnchor anchor = helper.createClientAnchor();
        //anchor.setAnchorType( ClientAnchor.MOVE_AND_RESIZE );


        final int pictureIndex =
                workbook.addPicture(IOUtils.toByteArray(stream), Workbook.PICTURE_TYPE_PNG);


        anchor.setCol1( 0 );
        anchor.setRow1( 9 ); // same row is okay
        anchor.setRow2( 11 );
        anchor.setCol2( 2 );
        final Picture pict = drawing.createPicture( anchor, pictureIndex );
        //pict.resize();

        Header header = sheet.getHeader();
        header.setCenter("&[pict]");
        header.setLeft("Left First Page Header");
        header.setRight("Right First Page Header");

        sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
        sheet.addMergedRegion(new CellRangeAddress(1,1,0,1));
        sheet.addMergedRegion(new CellRangeAddress(2,2,0,1));
        sheet.addMergedRegion(new CellRangeAddress(3,3,0,1));
        sheet.addMergedRegion(new CellRangeAddress(4,4,0,1));

        com.aspose.cells.Workbook workbook1=new com.aspose.cells.Workbook(workbook);


        FileOutputStream fileOut = new FileOutputStream("customerstest.xlsx");
        workbook.write(fileOut);
        fileOut.close();
        workbook.close();
    }
}

请帮助我如何在 aspose 单元工作簿中使用 POI 工作簿。

下面的代码语句不起作用,而是抛出类型不匹配错误:

com.aspose.cells.Workbook workbook1=new com.aspose.cells.Workbook(workbook);

谢谢

【问题讨论】:

    标签: java spring apache-poi aspose-cells


    【解决方案1】:

    嗯,Aspose.Cells 和 POI XSSF 是具有不同架构的不同 API,它们都有不同的对象和属性。我不确定是否可以轻松地解析其他 API 中的对象,或者他可能无法做到这一点。 Aspose.Cells 将读取和解析有效的 Excel 工作簿(应遵循 MS Excel 标准和规范)。

    我认为您可以尝试使用 POI XSSF API 将工作簿保存到 Excel 文件,然后使用 Aspose.Cells 读取该文件。如果输出文件(通过 POI XSSF)遵循 MS Excel 标准和规范,那么它也应该可以在 MS Excel 中正常打开。如果它可以在 MS Excel 中正常打开,那么 Aspose.Cells 肯定也应该正常加载文件。如果您发现 Aspose.Cells 无法读取最终文件的任何问题,那么这是 Aspose.Cells 的问题。否则我不认为这是 Aspose.Cells 的问题。简而言之,您可以简单地将 Excel 工作簿(通过 POI XSSF)保存到磁盘(Excel 文件)或流中,然后使用 Aspose.Cells API 从磁盘或流中加载它,它应该可以正常工作。

    我在 Aspose 担任支持开发人员/宣传员。

    【讨论】:

    • wants adding watermark in Excel workbook。我已经告诉他 Excel 没有内置水印功能。我已经展示了如何在标题中添加图片来模拟水印。 Apache poi 到目前为止,此代码需要使用非常低级的类和方法。现在他可能认为 Aspose.Cells 提供了某种“魔法”来满足水印要求。但是他也应该使用 Aspose Cells 来创建内容,不是吗?
    • @AxelRichter,你是对的。 Aspose.Cells 确实支持在标题中添加图片。因此,如果您“Shaikh Mudassir”想要使用 Aspose.Cells API,您可以使用此链接中的示例代码 --- (docs.aspose.com/display/cellsjava/…)。但在这种情况下,您还将使用 Aspose.Cells 来创建内容。请注意,如果您想使用 Aspose.Cells API,则根本不需要使用 Apache POI XSSF。 ...
    • 您可以使用 Aspose.Cells API 创建所有内容。谢谢你。 ---- 注意:我在 Aspose 担任开发倡导者。
    猜你喜欢
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多