itext 把xlsx文件解析为html,java - 使用java,itext和POI API将excel文件转换为pdf并保留设置 - 堆栈内存溢出...

我有一个Excel文件,其中有5列包含很少的合并单元格,空白单元格,日期和其他文本信息(普通的Excel文件)。

我正在使用Java中的POI API读取此文件。 我可以使用iText jar将文件转换为pdf表。

但是,整个格式不会复制到pdf中。 (例如,合并的单元格进入一列,其他格式或设置都消失了)。

创建一个简单的pdf表。

我如何保留与excel相同的格式? (我想要PDF格式的excel表格的精确副本)

这是我正在使用的代码

//First we read the Excel file in binary format into FileInputStream

FileInputStream input_document = new FileInputStream(new File("K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\" + SONo.trim() + "\\" + SONo.trim() + " - Checklist.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 rowIterator = my_worksheet.iterator();

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

com.itextpdf.text.Document iText_xls_2_pdf = new com.itextpdf.text.Document();

PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("K:\\DCIN_TER\\DCIN_EPU2\\CIRCUIT FROM BRANCH\\RAINBOW ORDERS\\" + SONo.trim() + "\\" + SONo.trim() + " - Checklist.pdf"));

iText_xls_2_pdf.open();

//we have 5 columns in the Excel sheet, so we create a PDF table with 5 columns; Note: There are ways to make this dynamic in nature, if you want to.

PdfPTable my_table = new PdfPTable(5);

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

PdfPCell table_cell;

//Loop through rows.

while(rowIterator.hasNext())

{

Row rowi = rowIterator.next();

Iterator cellIterator = rowi.cellIterator();

while(cellIterator.hasNext())

{

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

switch(celli.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(celli.getStringCellValue()));

//move the code below to suit to your needs

my_table.addCell(table_cell);

break;

case Cell.CELL_TYPE_NUMERIC:

//Push the data from Excel to PDF Cell

table_cell = new PdfPCell(new Phrase("" + celli.getNumericCellValue()));

//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

我已将excel文件附加为图像

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9ncHR5TC5wbmc=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值