我有一個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_TERDCIN_EPU2CIRCUIT FROM BRANCHRAINBOW 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_TERDCIN_EPU2CIRCUIT FROM BRANCHRAINBOW 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檔案作為圖像附加