簡體   English   中英

在Aspose單元中閱讀Apache POI XSSFWorkbook

[英]Read Apache POI XSSFWorkbook in Aspose cells

我正在嘗試在JAVA中創建帶有水印的excel文件。

我使用以下方法:1.使用Apache POI api創建excel工作簿2.在aspose單元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 Cell工作簿中使用POI工作簿。

下面的代碼語句不起作用,而是引發類型不匹配錯誤:

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

謝謝

好吧,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擔任技術支持/開發人員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM