excel-formula - 带有 Apache POI 的 excel 文件表到带有 iText 的 PDF - 具有值的单元格在 pdf 中没有公式
问题描述
我用 Apache POI 创建了一个 excel 文件表。现在我需要将其转换为 PDF。我想使用 iText,但我想将值而不是公式复制到 pdf。我怎样才能避免在我的 pdf 文件中使用该公式。
public class pdfExcelTest {
public static void main(String[] args) throws IOException {
// source file whit formula in cells
String file = "c:/my-excel-sheet.xls";
try {
if (file != null) {
FileInputStream fileInputStream = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
/*
* ==================================================================
* Iterating
* over all the rows and columns in a Sheet (Multiple ways)
* ==================================================================
*/
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// landscape format
Document document = new Document(PageSize.A4.rotate());
// portrait format
// Document document = new Document(PageSize.A4);
try {
PdfWriter.getInstance(document, new FileOutputStream("z:/excel2pdfTest.pdf"));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.open();
//the table has 26 columns
PdfPTable table = new PdfPTable(26);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
}
String cellValue = dataFormatter.formatCellValue(cell);
// 我想我必须为 CellTypes 字符串、值和公式添加不同的案例
// 然后为我的 cell1 构建不同的构造函数
System.out.print(cell + "\t");
Phrase cell1 = Phrase.getInstance(cellValue);
table.addCell(cell1);
}
System.out.println();
}
try {
document.add(table);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 文档也可以有一个或多个页面 - 取决于我的 excel 文件中有多少张,所以我必须定义另一个表
document.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
解决方案
推荐阅读
- asp.net-web-api - 自托管 asp.net webapi - 如何获得活动目录身份验证?
- laravel - 如何将 Laravel 5.4 中生成的密码用于 Laravel 5.6
- python - 在张量中操作标量 (Tensorflow)
- sql - 如何 SELECT 一行 TABLE 然后 INSERT 到另一个
- php - 通过 DOMDocument 获取链接标签
- python - 损坏的 virtualenv 但没有成功与其他解决方案库未加载:@executable_path/../.Python
- python-2.7 - 根据列中的值合并行
- javascript - 全选 | 使用切换按钮取消选择所有复选框
- python - Python selenium 网页抓取
- javascript - Chrome 抓取有时会挂起