java - 当我从 Excel 表中读取 inf 时,我无法按顺序打印 doc 文件中的信息
问题描述
文档中的输出格式应该是这样的:
这是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 文件,但不会覆盖它。
解决方案
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();
}
}
推荐阅读
- azure - Microsoft Azure 文本翻译 API 翻译错误
- typescript - 如果两个元组使用 Typescript 相等,你如何比较?
- excel - 如果之前打印过东西,有什么方法可以在excel中记录吗?
- javascript - 正则表达式在编辑器中查找具有单个参数的函数
- mongodb - 如何在pymongo中查询dict里面是列表?
- qt - 如何在 MainWindow 中创建 QPushButton?
- jquery - Django POST 将所有内容存储在键中而不是键值对中
- python - 带有 where 条件的 Pyodbc SQL 插入查询
- javascript - 这里 API Maps 设置标记
- ibm-integration-bus - 如何在 ESQL 中检查变量的类型