繁体   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

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

文件输出

这是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文件,但不会覆盖它。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM