java poi XWPFTable操作word表格的问题
1.下载
下载3.8beta4版本,请记得一定要下载该版本,其他版本读取word模板并改写内容生成新的文件后,打开新文件时会提示“word无法读取文档,文档可能损坏。”
2.集成到项目
这一步很简单,只要把下载后解压得到的poi-3.8-beta4-20110826.jar和poi-scratchpad-3.8-beta4-20110826.jar两个文件复制到java web项目的lib目录下就行了
3.制作word模板
把需要变动的值全部用代码来代替,例如你需要改变名称的值,则可以在模板中用name来表示。详细见附件中的doc文件。
4.调用接口方法实现对word的读写操作
整个过程就是先读取模板,然后修改内容,再重新生成新的文档保存到本地或者输出文件流提供下载,下面分别是生成新文档和输出文件流两种方式的代码片断,详细的代码请见下列代码中的readwriteWord()两个重载方法。
Java操作word里的表格问题,高分在线等
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExportDocImpl
{
public void testWord(){
try{
FileInputStream in = new FileInputStream("D:\\sinye.doc");//载入文档
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);
Range range = hwpf.getRange();//得到文档的读取范围
TableIterator it = new TableIterator(range);
//迭代文档中的表格
while (it.hasNext()) {
Table tb = (Table) it.next();
//迭代行,默认从0开始
for (int i = 0; i
poi 操作word 2007 (如何删除word中的某一个表格)
关键代码如下:
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)再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();
}
如何使用POI解决word2007中字符替换问题
首先就是要明白解决的方案是:先删后添。
就是利用Paragraph创建run,在创建完毕后循环遍历tableàrowàcell,获得到cell里的值,即cell.getText();在这里注意cell中value的长度,建议打出来看看已在后面于替换后的cell的值做对比。同时我们应该多多关注底层文件和源码,这样可以更加方便的了解他的底层构造。
可以将word的后缀名改为zip然后打开找到word/document.xml文件,查看构造尤其是表的结构(比如说多关注关注-
excel poi 使用HSSFWorkbook 导出的excel 用2007版本的office打开不
2007版本的需要使用XSSFWorkbook来处理,如果不确定读取的excel版本的话可以写的if判断下如果后缀为".xls"创建HSSFWorkbook 否则创建 XSSFWorkbook来对文件进行操作即可.Workbook excelWB = null;Sheet oneSheet = null;Row oneRow = null;Cell oneCell = null;。
..if("xls".equals(excFilSuff)){ excelWB = new HSSFWorkbook(excelFile); }else{ excelWB = new XSSFWorkbook(excelFile); }//获取第0个sheet对象oneSheet = excelWB.getSheetAt(0);//获取每行对象oneRow = oneSheet.getRow(rowNum);// 获取第几列对象oneCell = oneRow.getCell(cellNum);。
poi官网有操作word的文档吗,excel和ppt的都有demo为什么对word介
poi现在对excel的操作能精确到“单元格”,对ppt的操作能精确到“文本框”;但是对word的操作就没有那么精确了。
这也许是原因吧。 现在还有很多关键问题没有解决,如果您有兴趣,欢迎您加入!http://poi.apache.org/guidelines.html 不知道您到底想操作word的什么部分:sections?paragraphs ?character runs? 您研究一下现在的API,看看有不有有关定位到“表格”的函数;这种需求我也没做过,很多操作没有现成的代码,需要自己调查,研究。
现在POI里的HWPF和XWPF处于没有人“领养”的状态,所以你的问题很可能需要自己调查,解决。^_^。
转载请注明出处51数据库 » poiword2007表格位置问题