gpt4 book ai didi

java - Java 中的 PDF 到 Excel

转载 作者:行者123 更新时间:2023-11-30 07:47:34 26 4
gpt4 key购买 nike

<分区>

我正在将数据从 PDF 转换为 excel。 PDF 包含表格。我使用 Itext-pdf 读取数据,并在 apache poi 的帮助下将其转换为 excel。但所有内容都转换为文本,甚至表格行和列。我喜欢将表格写成行和列,就像在 PDF 中一样,所以请帮助我阅读 PDF 中的表格以写入 Excel。

这是我的代码:

PdfReader reader;
try {
reader = new PdfReader("D:/JDEV_WORK/MANOJ/ItemPriceReport.pdf");
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
TextExtractionStrategy strategy;
String line = null;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
strategy = parser.processContent(i,new SimpleTextExtractionStrategy());
line = strategy.getResultantText();
System.out.println("line --- "+line);
}

//conversion starts here....

HSSFRow myRow = null;
HSSFCell myCell = null;
CreationHelper helper = myWorkBook.getCreationHelper();
List<String> lines = IOUtils.readLines(new StringReader(line));

for (int i = 0; i < lines.size(); i++) {
String str[] = lines.get(i).split(",");
myRow = mySheet.createRow((short) i);
for (int j = 0; j < str.length; j++) {
myRow.createCell(j).setCellValue(helper.createRichTextString(str[j]));
}
}



FileOutputStream fileOut;
try {
fileOut = new FileOutputStream("D:/JDEV_WORK/MANOJ/ItemPriceExcel.xls");
myWorkBook.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
System.out.println("FILE NOT FOUND");
}
reader.close();
} catch (IOException e) {
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com