解决方案 »
- java循环的小问题,在线等
- [急救]编译后bin目录下class文件都不见了现在怎么编译都不行了
- 是不是不能监听主菜单Menu对象
- 还望各位Java大虾能够给点帮忙!小弟初次求助,拜托拜托
- 请教怎么找出数组中出现次数最多的元素
- jdk1.3中怎么根据已有文件构造Image对象?
- 应该如何进行MS SQL Server 2000的jdbc配置
- 谁能告诉我JAVA怎样快速的入门?
- 初学java可以用哪种工具好?visual age for java 怎么样?
- 一个图形显示的问题,困扰了我很长时间
- swt 如何给OleClientSite添加滚动条
- access denied(java.util.PropertyPermission java.awt.im.style write)
(1)poi的HWPF是不能新建word文档的,只能先有一个文档,再生成一个新文档。这个我试验过很多次了,但是也可能是错的,仅供大家参考哈~~关键代码如下:
FileInputStream fileInputStream = new FileInputStream( soureFile);
POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );
HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument objectOutputStream output = new FileOutputStream( targetFile );
hwpf.write(output);// write to the target file
output.close();(2)再word中插入表格。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();
} 以上程序希望能帮到大家~~有什么问题欢迎讨论啊~~