1

请问,当我将 xls 文件保存为 pdf 格式时,是否可以进行(调用)或执行相同操作的程序。在 java 程序中使用 JAVA API Jxl 或其他东西

我找到一个例子

import officetools.OfficeFile; // from officetools.jar

FileInputStream fis = new FileInputStream(new File("test.doc")); 
FileOutputStream fos = new FileOutputStream(new File("test.pdf"));

OfficeFile f = new OfficeFile(fis,"localhost","8100", false);

f.convert(fos,"pdf");

但它需要 openOffice 是否有其他东西,比如 Excel 上的 PDF crator 会在我的程序上自动更改扩展名

4

1 回答 1

1

在此处查看此示例将使用 iText 和 apache poi的隐藏文档、excel、文本和图像转换为 PDF

从http://itextpdf.com/下载 iText

从http://poi.apache.org/download.html下载 apache poi

您还可以使用下面的示例将 Microsoft Office Word 文件转换为 PDF

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;


import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class DocToPDF{

public static void main(String[] args) {

    POIFSFileSystem fs = null;  
    Document document = new Document();

     try {  

         fs = new POIFSFileSystem(new FileInputStream("D:/test.doc"));  

         HWPFDocument doc = new HWPFDocument(fs);  
         WordExtractor we = new WordExtractor(doc);  

         OutputStream file = new FileOutputStream(new File("D:/test.pdf")); 

         PdfWriter writer = PdfWriter.getInstance(document, file);  

         Range range = doc.getRange();
         document.open();  
         writer.setPageEmpty(true);  
         document.newPage();  
         writer.setPageEmpty(true);  

         String[] paragraphs = we.getParagraphText();  
         for (int i = 0; i < paragraphs.length; i++) {  

             org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);

             paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");  
         System.out.println("Length:" + paragraphs[i].length());  
         System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());  

         // add the paragraph to the document  
         document.add(new Paragraph(paragraphs[i]));  
         }  

         System.out.println("Finished");  
     } catch (Exception e) {  
         e.printStackTrace();  
     } finally {  
                     // close the document  
        document.close();  
                 }  
     }  
}
于 2013-05-08T15:12:06.753 回答