Java 将excel文件(xls、xlsx)转换为PDF
我必须将excel文件(xls,xlsx)转换为PDF,但是我正在寻找更好的方法,但是我不确定以下示例是否最适合我: 我在这里没有找到好的帖子和答案,有人有更好的例子吗 用一种简单的方式,我只需要以更好、更简单的方式将excel转换为java中的pdf,而不必阅读整个excel 我发现了这个例子,它正是我所需要的,但是由于许可证的原因,无法使用它:Java 将excel文件(xls、xlsx)转换为PDF,java,excel,Java,Excel,我必须将excel文件(xls,xlsx)转换为PDF,但是我正在寻找更好的方法,但是我不确定以下示例是否最适合我: 我在这里没有找到好的帖子和答案,有人有更好的例子吗 用一种简单的方式,我只需要以更好、更简单的方式将excel转换为java中的pdf,而不必阅读整个excel 我发现了这个例子,它正是我所需要的,但是由于许可证的原因,无法使用它: 提前谢谢 经过这么多的测试,我让他们相信我使用的第一个解决方案是正确的: import java.io.FileInputStream;
提前谢谢 经过这么多的测试,我让他们相信我使用的第一个解决方案是正确的:
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
}
}
import java.io.FileInputStream;
导入java.io.*;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.ss.usermodel.*;
导入java.util.Iterator;
导入com.itextpdf.text.*;
导入com.itextpdf.text.pdf.*;
公共类excel2pdf{
公共静态void main(字符串[]args)引发异常{
FileInputStream input\u document=新的FileInputStream(新文件(“C:\\excel\u to_pdf.xls”);
//将工作簿读入HSSF工作手册
HSSFWorkbook my_xls_工作簿=新的HSSFWorkbook(输入文档);
//将工作表读入HSSF表
HSSFSheet my_sheet=my_xls_工作簿。getSheetAt(0);
//在行上迭代
迭代器rowIterator=我的工作表。迭代器();
//此时,我们将创建输出PDF文档对象
Document iText_xls_2_pdf=新文档();
getInstance(iText_xls_2_pdf,新的FileOutputStream(“Excel2PDF_Output.pdf”);
iText_xls_2_pdf.open();
//Excel工作表中有两列,因此我们创建了一个包含两列的PDF表
//注意:如果你愿意的话,有一些方法可以使这个过程在本质上是动态的。
PdfPTable my_table=新PdfPTable(2);
//我们将使用下面的对象向表中动态添加新数据
PdfPCell表_单元;
//在行中循环。
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell Cell=cellIterator.next();//获取单元格
开关(cell.getCellType()){//标识单元格类型
//您需要在此处根据添加更多代码
//您的需求/转换
case Cell.Cell\u类型\u字符串:
//将数据从Excel推送到PDF单元格
table_cell=newpdfpcell(新短语(cell.getStringCellValue());
//请随意移动下面的代码以满足您的需要
my_table.addCell(table_cell);
打破
}
//下一行
}
}
//最后将表添加到PDF文档中
iText_xls_2_pdf.add(我的表格);
iText_xls_2_pdf.close();
//我们创建了我们的pdf文件。。
输入_document.close();//关闭xls
}
}
非常感谢你的帮助 要转换xls:
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
}
请访问:好的,但是他们不希望excel被“迭代”,就像你发送给打印机并将其保存为pdf时的函数一样,我以前做过,但是现在我无法创建虚拟打印机。你可以使用:easy和Apache 2许可证非常感谢,我将测试这个!使用这个解决方案,我可以得到合并的单元格和完整的所有格式。希望能有帮助。