当我从 Excel sheet 读取信息时,我无法按顺序打印文档文件中的信息

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

文档中的输出格式应该是这样的:

这是excelsheet的输入格式:

我正在尝试阅读 Excel sheet 并尝试在 Word 文档中按系统顺序打印信息。我可以从 Excel sheet 中读取但无法在 doc 文件中打印它。我该怎么办?

它显示了 Excel 文件的最后一行,即它只是覆盖了 doc 文件而不是附加它。有没有办法附加这个文件?

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

我希望输出附加 doc 文件而不是覆盖它。

您为 Excel sheet 中找到的每个单元格创建一个新的 XWPFDocument。那不能导致你想要的结果。要获得您想要的结果,只需要 一个 XWPFDocument 然后根据找到的 Excel 单元格的内容填充段落。

而对于 Excel 部分,您应该使用 org.apache.poi.ss.usermodel.* 而不是 HSSF 因为这更通用并且也可以阅读 XSSF (*.xlsx) 然后。

并且您不应该依赖 getStringCellValue,因为并非所有Excel单元格内容都必须始终是文本。相反,您应该使用 DataFormatterExcel 单元格中获取单元格内容。

完整示例:

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