简体   繁体   English

在Aspose单元中阅读Apache POI XSSFWorkbook

[英]Read Apache POI XSSFWorkbook in Aspose cells

I'm trying to create excel file with watermark in JAVA. 我正在尝试在JAVA中创建带有水印的excel文件。

I'm using below approach: 1. using Apache POI api to create excel workbook 2. consume the poi workbook in aspose cells api for adding watermar. 我使用以下方法:1.使用Apache POI api创建excel工作簿2.在aspose单元api中使用poi工作簿以添加watermar。

When i'm trying to consume POI workbook in aspose cell i'm getting error- workbook not expected. 当我尝试在aspose单元中使用POI工作簿时,出现错误-预期不到工作簿。 Please help, as i'm new to spring/JAVA 请帮忙,因为我是Spring / JAVA的新手

PFB my code: 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();
    }
}

Please assist me on how can i consume POI workbook in aspose cell workbook. 请协助我如何在Aspose Cell工作簿中使用POI工作簿。

Below code statement is not working, rather throwing type mismatch error: 下面的代码语句不起作用,而是引发类型不匹配错误:

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

Thanks 谢谢

Well, Aspose.Cells and POI XSSF are different APIs with diverse architectures, both have different objects and attributes. 好吧,Aspose.Cells和POI XSSF是具有不同体系结构的不同API,它们都有不同的对象和属性。 I am not sure one can easily parse one's object in other APIs or may be he cannot do that. 我不确定一个人可以轻松地在其他API中解析其对象,或者可能是他不能做到这一点。 Aspose.Cells will read and parse valid Excel workbooks (which should follow MS Excel standards and specifications). Aspose.Cells将读取和解析有效的Excel工作簿(应遵循MS Excel标准和规范)。

I think you may try to save your workbook to Excel file using POI XSSF APIs and then use Aspose.Cells to read that file. 我认为您可以尝试使用POI XSSF API将工作簿保存到Excel文件,然后使用Aspose.Cells读取该文件。 If the output file (by POI XSSF) follows MS Excel standards and specifications then it should be opened fine into MS Excel too. 如果输出文件(通过POI XSSF)符合MS Excel标准和规范,则也应将其正确打开到MS Excel中。 If it is opened fine into MS Excel then surely Aspose.Cells should also load the file fine. 如果它可以很好地在MS Excel中打开,那么Aspose.Cells当然也应该加载该文件。 If you find any issue where Aspose.Cells could not read the final file, then it is an issue with Aspose.Cells. 如果您发现Aspose.Cells无法读取最终文件的任何问题,则与Aspose.Cells有关。 Otherwise I do not think it is an issue with Aspose.Cells. 否则,我认为Aspose.Cells不会有问题。 In short, you can simply save the Excel Workbook (by POI XSSF) to disk (Excel file) or streams first then use Aspose.Cells APIs to load it from disk or streams, it should work fine. 简而言之,您可以简单地先将Excel工作簿(通过POI XSSF保存)到磁盘(Excel文件)或流中,然后使用Aspose.Cells API从磁盘或流中加载它,它应该可以正常工作。

I am working as Support developer/ Evangelist at Aspose. 我在Aspose担任技术支持/开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM