将PDF打印到Excel中

时间:2019-10-01 06:45:19

标签: java

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

public class saveData {

    public static void main(String[] args) throws Exception {

        File file = new File("C:/Users/jurkeda1/Desktop/TestERV/layout1_3333445.pdf");

        PDDocument document = null;
        try {
            document = PDDocument.load(file);
        } catch (IOException e) {
            e.printStackTrace();
        }


        if (!document.isEncrypted()) {
            PDFTextStripper stripper = null;
            try {
                stripper = new PDFTextStripper();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                String text = stripper.getText(document);

                // Creating a File object that represents the disk file.
                PrintStream o = new PrintStream(new File("C:/Users/jurkeda1/Desktop/TestERV/OUT.txt"));
                // Store current System.out before assigning a new value
                PrintStream console = System.out;
                // Assign o to output stream
                System.setOut(o);
                System.out.println(text);
            } catch (IOException e) {
                e.printStackTrace();
            }

2 个答案:

答案 0 :(得分:0)

基本上,您可以像这样编写工作簿:

System.out.println("Input Please");
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
  int next = in.nextInt();
  System.out.print(next);
  System.out.print(' ');
}

您需要了解如何添加新工作表以及如何向工作表添加新的行和单元格。这只是如何将// 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(); } 实例写入文件系统的示例。

答案 1 :(得分:0)

首先,您需要在您的 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>

pom.xml 的示例:

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

创建一个类来管理PDF数据 [可选]

public class CustomPDFValue 
{
    private String number;
    private String PDFText;

    public CustomPDFValue(String number, String PDFText) 
    {
        this.number = number;
        this.PDFText = PDFText;
    }

    public String getNumber() 
    {
        return number;
    }

    public void setNumber(String number) 
    {
        this.number = number;
    }

    public String getPDFText() 
    {
        return PDFText;
    }

    public void setPDFText(String PDFText) 
    {
        this.PDFText = PDFText;
    }
}

现在,您需要创建一个工作簿和一个表格

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();
   }
}

您的输出应如下所示:The excel file generated.