简体   繁体   English

当我从Excel工作表中读取inf时,无法按顺序在doc文件中打印信息

[英]I can't print the info in doc file in a sequence when I read the inf from the Excel sheet

The output format should be like this in the doc: 输出格式应在文档中如下所示:

文件输出

This is the input format of the excel sheet: 这是excel工作表的输入格式:

Excel输出

I'm trying to read an Excel sheet and trying to print the information in a systematical order in the Word document. 我正在尝试阅读Excel工作表,并试图以系统的顺序在Word文档中打印信息。 I can read from the Excel sheet but couldn't print it in the doc file. 我可以从Excel工作表中读取,但无法在doc文件中打印。 What should I do? 我该怎么办?

It's showing the last line of the Excel file ie, its only over-writing the doc file but not appending it. 它显示的是Excel文件的最后一行,即仅覆盖doc文件而不附加文件。 Is there a way to append this file? 有没有办法添加这个文件?

public static void main(String[] args) throws Exception {
    File src = new File("convertion java.xls");
    FileInputStream fis = new FileInputStream(src);
    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet sheet1 = wb.getSheetAt(0);
    String[] header = new String[4];
    for (int k = 0; k < 4; k++) {
        header[k] = sheet1.getRow(0).getCell(k).getStringCellValue();
    }
    FileOutputStream out = new FileOutputStream(new File("output.docx"));
    for (int j = 1; j < 5; j++) {

        for (int i = 0; i < 4; i++) {

            result = sheet1.getRow(j).getCell(i).getStringCellValue();
            doc = header[i] + " = " + result;
            System.out.println(doc);
            XWPFDocument document = new XWPFDocument();
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun paragraphOneRunOne = paragraph.createRun();
            paragraphOneRunOne.setText(doc);
            document.write(out);
        }
        System.out.println();
    }
    wb.close();
    out.close();
}}  

I expect the output to append the doc file but not to over-write it. 我希望输出会附加doc文件,但不会覆盖它。

You create a new XWPFDocument for each cell found in the Excel sheet. 您为在Excel表中找到的每个单元格创建一个新的XWPFDocument That cannot lead to the result you wanted. 那不会导致您想要的结果。 To get your wanted result, only one XWPFDocument is needed which then gets filled with paragraphs according the contents of the found Excel cells. 为了获得所需的结果,只需要一个 XWPFDocument ,然后根据找到的Excel单元格的内容用段落填充。

And for the Excel part, you should use the org.apache.poi.ss.usermodel.* rather than HSSF since this is more general and also able reading XSSF ( *.xlsx ) then. 对于Excel部分,您应该使用org.apache.poi.ss.usermodel.*而不是HSSF因为这更通用,并且还可以读取XSSF*.xlsx )。

And you should not rely on getStringCellValue since not all Excel cell contents must always be text. 并且您不应该依赖getStringCellValue因为并非所有Excel单元格内容都必须始终为文本。 Instead you should use DataFormatter to get the cell contents from the Excel cells. 相反,您应该使用DataFormatterExcel单元格中获取单元格内容。

Complete example: 完整的例子:

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xwpf.usermodel.*;

class ReadExcelWriteWord {

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

  DataFormatter formatter = new DataFormatter();

  Workbook workbook = WorkbookFactory.create(new FileInputStream("convertion java.xls"));
  FileOutputStream out = new FileOutputStream("output.docx");
  XWPFDocument document = new XWPFDocument();

  Sheet sheet = workbook.getSheetAt(0);
  Row row = null;
  Cell cell = null;
  XWPFParagraph paragraph = null;
  XWPFRun run = null;

  String[] header = new String[4];
  row = sheet.getRow(0);
  if (row != null) {
   for (int k = 0; k < 4; k++) {
    cell = row.getCell(k);
    header[k] = formatter.formatCellValue(cell);
   }
  }

  String result = "";
  String doc = "";
  for (int j = 1; j < 5; j++) {
   row = sheet.getRow(j);
   if (row != null) {
    for (int i = 0; i < 4; i++) {
     cell = row.getCell(i);
     result = formatter.formatCellValue(cell);
     doc = header[i] + " = " + result;
     System.out.println(doc);
     paragraph = document.createParagraph();
     run = paragraph.createRun();
     run.setText(doc);
    }
   }
   paragraph = document.createParagraph();
   System.out.println();
  }

  workbook.close();
  document.write(out);
  out.close();
  document.close();
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法从 json 文件中读取和打印值 - I can't read and print values from json file 使用apache POI读取Excel文件后,如何在txt文件中打印工作表? - Once I read a excel file with apache POI how do I print the sheet in a txt file? 我试图从文件中读取字符串并将其存储在Excel工作表中 - I tried to read string from file and store it in Excel sheet 当我尝试从excel文件读取时出现ClassNotFoundException - i get ClassNotFoundException when i try to read from excel file 我对从文件中读取的数组进行了排序,但是无法打印 - I have an sorted an array that was read from a file, but it won't print 我想从具有Java库Apache Poi的excel .xlsx文件中读取,但是我无法修复错误,代码无法编译 - I want to read from excel .xlsx file with java's library Apache Poi, but I can't fix an error, code does not compile 如何用Java读取Excel文件? - How can I read Excel file in Java? 为什么我无法在类路径中使用getResourceAsStream读取属性文件(在WEB-INF / lib下) - Why I cant read properties file using getResourceAsStream when it is in classpath (under WEB-INF/lib) 如何使用JavaSound(Java,Java Sound)从.wave文件读取信息 - How can I read info from .wave file using JavaSound (Java, Java Sound) 如何使用 Java 将 CSV 文件加载到 Excel 工作表中 - How can I load CSV file into Excel sheet using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM