Java 将pdf打印到excel中

Java 将pdf打印到excel中,java,Java,我需要将PDF打印到excel工作表中,并将PDF中的数字作为第一列的值,第二列将放置PDF中的文本。我有可以读取PDF的代码,但我很难将其保存到excel中(只有.txt适合我)。多谢各位 公共类saveData{ 公共静态void main(字符串[]args)引发异常{ File File=新文件(“C:/Users/jurkeda1/Desktop/TestERV/layout1_3333445.pdf”); PDDocument文件=null; 试一试{ document=PDDocu

我需要将PDF打印到excel工作表中,并将PDF中的数字作为第一列的值,第二列将放置PDF中的文本。我有可以读取PDF的代码,但我很难将其保存到excel中(只有.txt适合我)。多谢各位

公共类saveData{
公共静态void main(字符串[]args)引发异常{
File File=新文件(“C:/Users/jurkeda1/Desktop/TestERV/layout1_3333445.pdf”);
PDDocument文件=null;
试一试{
document=PDDocument.load(文件);
}捕获(IOE异常){
e、 printStackTrace();
}
如果(!document.isEncrypted()){
PDFTextStripper-stripper=null;
试一试{
剥离器=新的PDFTextStripper();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
String text=stripper.getText(文档);
//创建表示磁盘文件的文件对象。
PrintStream o=新的PrintStream(新文件(“C:/Users/jurkeda1/Desktop/TestERV/OUT.txt”);
//在指定新值之前存储当前System.out
PrintStream控制台=System.out;
//将o分配给输出流
系统放样(o);
System.out.println(文本);
}捕获(IOE异常){
e、 printStackTrace();
}

基本上,您可以编写如下工作簿:

// create a new workbook instance
XSSFWorkbook workbook = new XSSFWorkbook();

// ...
// write data into the workbook and format the data
// data may be written by adding and configuring instances of XSSFSheet, XSSFRow and XSSFCell
// ...

// then write the workbook into a file (until here, it is in the RAM)
try {
    // create a new FileOutputStream with the path to the file (don't forget the extension)
    FileOutputStream fos = new FileOutputStream("Y:\\our\\path\\to\\the\\file.xlsx");
    // then write the workbook using that stream
    workbook.write(fos);
    // force the stream to write everything out of the buffers
    fos.flush();
    // and close it afterwards
    fos.close();
    // same for the workbook resource
    workbook.close();
} catch (FileNotFoundException e) {
    // inform about the special exception, maybe
    e.printStackTrace();
} catch (IOException e) {
    // inform about the special exception, maybe
    e.printStackTrace();
}

您需要了解如何添加新工作表以及如何向工作表中添加新行和单元格。这只是如何将
XSSFWorkbook
的实例写入文件系统的示例。

首先,您需要在pom.xml文件中添加
Apache POI
依赖项:

</dependencies>
    <dependency>
        <groupId>org.apache-extras.beanshell</groupId>
        <artifactId>bsh</artifactId>
        <version>2.0b6</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.1</version> 
    </dependency>
</dependencies>
现在您需要创建工作簿和工作表:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>Sample</artifactId>
    <version>1.0.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache-extras.beanshell</groupId>
            <artifactId>bsh</artifactId>
            <version>2.0b6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>
</project>
private void exportAsExcel()
{
   try
   {
      // Create dump data for this example
      List<CustomPDFValue> customPdfValueList = new ArrayList<>();
      customPdfValueList.add(new CustomPDFValue("1", "text 1"));
      customPdfValueList.add(new CustomPDFValue("2", "text 2"));
      customPdfValueList.add(new CustomPDFValue("3", "text 3"));
      customPdfValueList.add(new CustomPDFValue("4", "text 4"));
      customPdfValueList.add(new CustomPDFValue("5", "text 5"));
      customPdfValueList.add(new CustomPDFValue("6", "text 6"));
      Workbook workbook = new HSSFWorkbook();
      Sheet spreadsheet = workbook.createSheet("sample");
      int rowIndex = 0;
      int columnIdexNumber = 0;
      int columnIdexPDFText = 1;
      // Create a row at index 0
      Row row = spreadsheet.createRow(rowIndex);
      // Add a title to the fist cell in the 'row'
      row.createCell(columnIdexNumber).setCellValue("Numbers");
      // Add a title to the second cell in the 'row'
      row.createCell(columnIdexPDFText).setCellValue("PDF text");
      rowIndex++;
      // Now fill the rows with the values taken from the PDF
      for(CustomPDFValue customPDFValue: customPdfValueList)
      {
         // Create a new row and add a number and a text
         row = spreadsheet.createRow(rowIndex);
         row.createCell(columnIdexNumber).setCellValue(customPDFValue.getNumber());
         row.createCell(columnIdexPDFText).setCellValue(customPDFValue.getPDFText());
         rowIndex++;
      }
      // Optional: you can autosize the columns
      for(int i = 0; i < customPdfValueList.size(); i++)
      {
        spreadsheet.autoSizeColumn(i);
      }
      String pathFile = "sample.xls";
      // Save your excel
      FileOutputStream fileOut = new FileOutputStream(pathFile);
      workbook.write(fileOut);
      fileOut.close();
      workbook.close();
   } 
   catch (IOException e) 
   {
     e.printStackTrace();
   }
}
private void exportAsExcel()
{
尝试
{
//为此示例创建转储数据
List customPdfValueList=新建ArrayList();
添加(新的CustomPDFValue(“1”,“文本1”);
添加(新的CustomPDFValue(“2”,“文本2”));
添加(新的CustomPDFValue(“3”,“文本3”);
添加(新的CustomPDFValue(“4”,“文本4”);
添加(新的CustomPDFValue(“5”,“文本5”);
添加(新的CustomPDFValue(“6”,“文本6”);
工作簿=新的HSSF工作簿();
工作表电子表格=工作簿.createSheet(“示例”);
int rowIndex=0;
int columnIdexNumber=0;
int columnIdexPDFText=1;
//在索引0处创建一行
行=电子表格.createRow(行索引);
//向“行”中的第一个单元格添加标题
row.createCell(columnIdexNumber).setCellValue(“数字”);
//向“行”中的第二个单元格添加标题
createCell(columnIdexPDFText).setCellValue(“PDF文本”);
rowIndex++;
//现在用从PDF中获取的值填充行
for(CustomPDFValue CustomPDFValue:CustomPDFValue列表)
{
//创建新行并添加数字和文本
行=电子表格.createRow(行索引);
row.createCell(columnIdexNumber).setCellValue(customPDFValue.getNumber());
row.createCell(columnIdexPDFText).setCellValue(customPDFValue.getPDFText());
rowIndex++;
}
//可选:可以自动调整列的大小
对于(int i=0;i

您的输出应该是这样的:

您需要使用外部库来创建Excel文件,如.xlsx。查看并签出类,如
XSSFWorkbook
,例如。File fileX=new File(“C:/Users/jurkeda1/Desktop/TestERV/template_VMV.xlsx”);FileInputStream fip=new FileInputStream(fileX);XSSFWorkbook工作簿=新XSSFWorkbook(fip);但文本仍未保存在Excel中。您需要
文件输入流
如果要读取现有的
XSSFWorkbook
,请尝试使用
文件输出流
。。。
private void exportAsExcel()
{
   try
   {
      // Create dump data for this example
      List<CustomPDFValue> customPdfValueList = new ArrayList<>();
      customPdfValueList.add(new CustomPDFValue("1", "text 1"));
      customPdfValueList.add(new CustomPDFValue("2", "text 2"));
      customPdfValueList.add(new CustomPDFValue("3", "text 3"));
      customPdfValueList.add(new CustomPDFValue("4", "text 4"));
      customPdfValueList.add(new CustomPDFValue("5", "text 5"));
      customPdfValueList.add(new CustomPDFValue("6", "text 6"));
      Workbook workbook = new HSSFWorkbook();
      Sheet spreadsheet = workbook.createSheet("sample");
      int rowIndex = 0;
      int columnIdexNumber = 0;
      int columnIdexPDFText = 1;
      // Create a row at index 0
      Row row = spreadsheet.createRow(rowIndex);
      // Add a title to the fist cell in the 'row'
      row.createCell(columnIdexNumber).setCellValue("Numbers");
      // Add a title to the second cell in the 'row'
      row.createCell(columnIdexPDFText).setCellValue("PDF text");
      rowIndex++;
      // Now fill the rows with the values taken from the PDF
      for(CustomPDFValue customPDFValue: customPdfValueList)
      {
         // Create a new row and add a number and a text
         row = spreadsheet.createRow(rowIndex);
         row.createCell(columnIdexNumber).setCellValue(customPDFValue.getNumber());
         row.createCell(columnIdexPDFText).setCellValue(customPDFValue.getPDFText());
         rowIndex++;
      }
      // Optional: you can autosize the columns
      for(int i = 0; i < customPdfValueList.size(); i++)
      {
        spreadsheet.autoSizeColumn(i);
      }
      String pathFile = "sample.xls";
      // Save your excel
      FileOutputStream fileOut = new FileOutputStream(pathFile);
      workbook.write(fileOut);
      fileOut.close();
      workbook.close();
   } 
   catch (IOException e) 
   {
     e.printStackTrace();
   }
}