亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 Excel 文件 (xls,xlsx) 轉換為 PDF

將 Excel 文件 (xls,xlsx) 轉換為 PDF

Helenr 2022-05-25 15:49:43
我必須將 Excel 文件(xls,xlsx)轉換為 PDF,但是我正在尋找更好的方法,但是我不確定以下示例是否最適合我:https://www.grapecity.com/en/blogs/use-excel-api-to-convert-spreadsheets-to-pdfs-in-java我在這里沒有找到好的帖子和答案,有人有更好的例子嗎?Java將xls文件保存為PDF以一種簡單的方式,我只需要以更好、更簡單的方式將 excel 轉換為 java 中的 pdf,而無需閱讀整個 excel。我找到了這個例子,這正是我需要的,但是由于許可證而不能使用它:https://kbdeveloper.qoppa.com/sample-java-code-to-convert-excel-to-pdf-using-jofficeconvert/提前致謝 !
查看完整描述

2 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

經過這么多測試,我說服他們我使用的第一個解決方案是正確的:


import java.io.FileInputStream;

    import java.io.*;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    import org.apache.poi.hssf.usermodel.HSSFSheet;

    import org.apache.poi.ss.usermodel.*;

    import java.util.Iterator;

   import com.itextpdf.text.*;

    import com.itextpdf.text.pdf.*;


    public class excel2pdf {  

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


                    FileInputStream input_document = new FileInputStream(new File("C:\\excel_to_pdf.xls"));

                    // Read workbook into HSSFWorkbook

                    HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 

                    // Read worksheet into HSSFSheet

                    HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0); 

                    // To iterate over the rows

                    Iterator<Row> rowIterator = my_worksheet.iterator();

                    //We will create output PDF document objects at this point

                    Document iText_xls_2_pdf = new Document();

                    PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("Excel2PDF_Output.pdf"));

                    iText_xls_2_pdf.open();

                    //we have two columns in the Excel sheet, so we create a PDF table with two columns

                    //Note: There are ways to make this dynamic in nature, if you want to.

                    PdfPTable my_table = new PdfPTable(2);

                    //We will use the object below to dynamically add new data to the table

                    PdfPCell table_cell;

                    //Loop through rows.

                    while(rowIterator.hasNext()) {

                            Row row = rowIterator.next(); 

                            Iterator<Cell> cellIterator = row.cellIterator();

                                    while(cellIterator.hasNext()) {

                                            Cell cell = cellIterator.next(); //Fetch CELL

                                            switch(cell.getCellType()) { //Identify CELL type

                                                    //you need to add more code here based on

                                                    //your requirement / transformations

                                            case Cell.CELL_TYPE_STRING:

                                                    //Push the data from Excel to PDF Cell

                                                     table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));

                                                     //feel free to move the code below to suit to your needs

                                                     my_table.addCell(table_cell);

                                                    break;

                                            }

                                            //next line

                                    }


                    }

                    //Finally add the table to PDF document

                    iText_xls_2_pdf.add(my_table);                       

                    iText_xls_2_pdf.close();                

                    //we created our pdf file..

                    input_document.close(); //close xls

            }

    }

非常感謝您的幫助 !


查看完整回答
反對 回復 2022-05-25
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

要轉換 xls:

  1. 將 xls 輸入流轉換為 html 輸入流

  2. html inputstream 轉換為 pdf outstream

  3. 這個 pdf outstream 可以寫入所需的 pdf 文件,或者可以返回以供進一步使用


public byte[] convertMsXlsBytesToPdfBytes() {

byte[] pdfBytes = null;

try {

    ByteArrayOutputStream pdfOutStream = new ByteArrayOutputStream();

    String fileName = "Doc Naming convention.xls";

    InputStream inputStream = new FileInputStream(fileName);


    //convert xls stream to html - w3 document

    org.w3c.dom.Document w3Document = ExcelToHtmlConverter.process(inputStream);


    //convert above w3 document to html input stream

    ByteArrayOutputStream htmlOutputStream = new ByteArrayOutputStream();

    Source xmlSource = new DOMSource(w3Document);

    Result outputTarget = new StreamResult(htmlOutputStream);

    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);

    InputStream htmlInputStream = new ByteArrayInputStream(htmlOutputStream.toByteArray());


    //convert html inputstream to pdf out stream

    ConverterProperties converterProperties = new ConverterProperties();

    HtmlConverter.convertToPdf(htmlInputStream, pdfOutStream, converterProperties);


    pdfBytes = pdfOutStream.toByteArray();


    //write to physical pdf file

    OutputStream out = new FileOutputStream(fileName.substring(0, fileName.indexOf(".")) + ".pdf");

    out.write(pdfBytes);

    out.close();

} catch (Exception e) {

    // log exception details and throw custom exception

}

return pdfBytes; //bytes array I'm returning as per my requirement

}


查看完整回答
反對 回復 2022-05-25
  • 2 回答
  • 0 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號