2011-09-28 94 views
0

我需要將xlsx文檔轉換爲pdf格式。 我知道iText可以保存pdf文檔和Docx4j可以讀寫xslx。實際上,我們的應用程序既用於構建報告。 但是我們有非常困難的模板,所以我不能只讀取xslx(docx4j)並將其寫入pdf(iText)。格式化會丟失,所以我需要另一個轉換庫。 我也聽說過像商業圖書館(Jxcell)但想使用開源解決方案。java excel至pdf轉換

任何人都可以幫助我嗎?

回答

0

它必須在Java中完成嗎? 如果是的話,也許看看Apache POI

否則,爲什麼不有一個小的本機應用程序,利用PDF打印機,並調用相關的API直接打印到PDF文件?當然Java的是不是真的做這方面的工作做......

例如,以下是針對非Java框架:PDF Class Library from Solidworks

+0

爲據我所知,POI無法使用xslx,我不需要創建新文檔,而是轉換現有的文檔。只允許Java((( – Nick

1

我已經利用iText和Apache POI:

FileInputStream filecontent = new FileInputStream(new File(sourcepath)); 
    FileOutputStream out = new FileOutputStream(new File(destinationPath)); 
    HSSFWorkbook my_xls_workbook = null; 
    HSSFSheet my_worksheet = null; 
    XSSFWorkbook my_xlsx_workbook = null; 
    XSSFSheet my_worksheet_xlsx = null; 
    Document document = new Document(PageSize.ARCH_E, 0, 0, 0, 0); 
    PdfWriter writer = PdfWriter.getInstance(document, out); 
    document.open(); 
    PdfDestination magnify = null; 
    float magnifyOpt = (float) 70.0; 
    magnify = new PdfDestination(PdfDestination.XYZ, -1, -1, magnifyOpt/100); 
    int pageNumberToOpenTo = 1; 
    PdfAction zoom = PdfAction.gotoLocalPage(pageNumberToOpenTo, magnify, writer); 
    writer.setOpenAction(zoom); 
    document.addDocListener(writer); 

    Iterator<Row> rowIterator = null; 
    int maxColumn = 0; 
    if (fileType.equals(".xls")) { 
     try { 
      my_xls_workbook = new HSSFWorkbook(filecontent); 
      my_worksheet = my_xls_workbook.getSheetAt(0); 
      rowIterator = my_worksheet.iterator(); 
      maxColumn = my_worksheet.getRow(0).getLastCellNum(); 
     } catch (IOException ex) { 
      Logger.getLogger(PdfConversion.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    if (fileType.equals(".xlsx")) { 
     try { 
      my_xlsx_workbook = new XSSFWorkbook(filecontent); 
      my_worksheet_xlsx = my_xlsx_workbook.getSheetAt(0); 
      rowIterator = my_worksheet_xlsx.iterator(); 
      maxColumn = my_worksheet_xlsx.getRow(0).getLastCellNum(); 
     } catch (IOException ex) { 
      Logger.getLogger(PdfConversion.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
    PdfPTable my_table = new PdfPTable(maxColumn); 
    my_table.setHorizontalAlignment(Element.ALIGN_CENTER); 
    my_table.setWidthPercentage(100); 
    my_table.setSpacingBefore(0f); 
    my_table.setSpacingAfter(0f); 
    PdfPCell table_cell; 
    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 
       case Cell.CELL_TYPE_STRING: 
        //Push the data from Excel to PDF Cell 
        table_cell = new PdfPCell(new Phrase(cell.getStringCellValue())); 
        if (row.getRowNum() == 0) { 
         table_cell.setBackgroundColor(BaseColor.LIGHT_GRAY); 
         table_cell.setBorderColor(BaseColor.BLACK); 
        } 
        my_table.addCell(table_cell); 
        break; 
      } 
     } 
    } 
    document.add(my_table); 
    document.close(); 
    System.out.println("Excel file converted to PDF successfully");