作者:乔山办公网日期:
返回目录:word文档
document.createTable(rows, numOfColumns); 你自己查查API很多方法啊
关键代码如下:
FileInputStream fileInputStream = new FileInputStream( soureFile);
POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );
HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument object
OutputStream output = new FileOutputStream( targetFile );
hwpf.write(output);// write to the target file
output.close();
(2)再e799bee5baa6e58685e5aeb9366word中插入表格。HWPF的情况:
Table tcDataTable = range.insertTableBefore( (short)column , row);//column and row列数和行数
tcDataTable.getRow(i).getCell(j).getParagraph(0).getCharacterRun(0).insertBefore("插入i行j列的内容" );
XWPF的情况:
String outputFile = "D:\\test.doc";
XWPFDocument document = new XWPFDocument();
XWPFTable tableOne = document.createTable();
XWPFTableRow tableOneRowOne = tableOne.getRow(0);
tableOneRowOne.getCell(0).setText("11");
XWPFTableCell cell12 = tableOneRowOne.createCell();
cell12.setText("12");
// tableOneRowOne.addNewTableCell().setText("第1行第2列");
// tableOneRowOne.addNewTableCell().setText("第1行第3列");
// tableOneRowOne.addNewTableCell().setText("第1行第4列");
XWPFTableRow tableOneRowTwo = tableOne.createRow();
tableOneRowTwo.getCell(0).setText("21");
tableOneRowTwo.getCell(1).setText("22");
// tableOneRowTwo.getCell(2).setText("第2行第3列");
XWPFTableRow tableOneRow3 = tableOne.createRow();
tableOneRow3.addNewTableCell().setText("31");
tableOneRow3.addNewTableCell().setText("32");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(outputFile);
document.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
有个最简单,不用使用任何 poi 或其他第三方类库的抄方法生成 excel ,或 word ,
你把一个生成好的 word 例子用文件另存为 xml , docx 之类,然后你再直接用记事本去打知开这个 docx ,你就会发现是道 xml 格式,
这个时候,你在程序里面就直接用普通代码生成这个 xml 就可以了。
//创建一知个表格
XWPFTable table = doc.createTable(4,2);
table.setCellMargins(50, 0, 50,3000);//top, left, bottom, right
// table.setInsideHBorder(XWPFBorderType.NONE, 0, 0, "");//去除单元道格间的横线
table.getRow(0).getCell(0).setText("字段版一:");
table.getRow(0).getCell(1).setText("字段二:");
table.getRow(1).getCell(0).setText("字段三权:");
table.getRow(1).getCell(1).setText("字段四:");