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

标签 java excel apache-poi documentation

文档中的输出格式应如下所示:

Document Output

这是Excel工作表的输入格式:

Excel Output

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

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

我希望输出附加文档文件,但不要覆盖它。

最佳答案

您为 Excel 工作表中的每个单元格创建一个新的 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();
 }
}

关于java - 当我从 Excel 工作表读取 inf 时,无法按顺序打印 doc 文件中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57899780/

相关文章:

java - 在 XWPFDocument 的任意位置插入 XWPFParagraph

java - 如何避免将此方法和所有其他变量设为静态?

java - 在 Applet On Event 中使用 PaintComponent 进行绘图

java - 线程间消息传递实现

excel - Apache POI 和颜色

vba - Excel VBA 每行循环条件格式

python - 检查Excel文件是否包含公式

java - Apache POI : Sort rows by date

java - 如何使用java api合并两个具有图表和表格的pptx文件?

java - 如何比较列表中的对象并删除重复项