首页 > 解决方案 > 当我从 Excel 表中读取 inf 时,我无法按顺序打印 doc 文件中的信息

问题描述

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

文件输出

这是excel表的输入格式:

Excel 输出

我正在尝试阅读 Excel 工作表并尝试在 Word 文档中以系统顺序打印信息。我可以从 Excel 表中读取,但无法在 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 文件,但不会覆盖它。

标签: javaexcelapache-poidocumentation

解决方案


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

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

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

完整示例:

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

推荐阅读