[英]I can't print the info in doc file in a sequence when I read the inf from the Excel sheet
The output format should be like this in the doc: 输出格式应在文档中如下所示:
This is the input format of the excel sheet: 这是excel工作表的输入格式:
I'm trying to read an Excel sheet and trying to print the information in a systematical order in the Word document. 我正在尝试阅读Excel工作表,并试图以系统的顺序在Word文档中打印信息。 I can read from the Excel sheet but couldn't print it in the doc file.
我可以从Excel工作表中读取,但无法在doc文件中打印。 What should I do?
我该怎么办?
It's showing the last line of the Excel file ie, its only over-writing the doc file but not appending it. 它显示的是Excel文件的最后一行,即仅覆盖doc文件而不附加文件。 Is there a way to append this file?
有没有办法添加这个文件?
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();
}}
I expect the output to append the doc file but not to over-write it. 我希望输出会附加doc文件,但不会覆盖它。
You create a new XWPFDocument
for each cell found in the Excel
sheet. 您为在
Excel
表中找到的每个单元格创建一个新的XWPFDocument
。 That cannot lead to the result you wanted. 那不会导致您想要的结果。 To get your wanted result, only one
XWPFDocument
is needed which then gets filled with paragraphs according the contents of the found Excel
cells. 为了获得所需的结果,只需要一个
XWPFDocument
,然后根据找到的Excel
单元格的内容用段落填充。
And for the Excel
part, you should use the org.apache.poi.ss.usermodel.*
rather than HSSF
since this is more general and also able reading XSSF
( *.xlsx
) then. 对于
Excel
部分,您应该使用org.apache.poi.ss.usermodel.*
而不是HSSF
因为这更通用,并且还可以读取XSSF
( *.xlsx
)。
And you should not rely on getStringCellValue
since not all Excel
cell contents must always be text. 并且您不应该依赖
getStringCellValue
因为并非所有Excel
单元格内容都必须始终为文本。 Instead you should use DataFormatter
to get the cell contents from the Excel
cells. 相反,您应该使用
DataFormatter
从Excel
单元格中获取单元格内容。
Complete example: 完整的例子:
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.