1. poi xwpf 操作word表格,怎么合并单元格
for (int j = 0; j < tr.numCells(); j++) {
cell = tr.getCell(j);//取得单元格
if(j < tr.numCells()-1){//获取下一个单元格,用于判断是否合并
nextCell = tr.getCell(j+1);
}else{
nextCell = null;
}
int[] leftEdges = getLeftEdges(table,tr);
if(tempIndex< leftEdges.length-1 && cell.getLeftEdge() != leftEdges[tempIndex+1] ){
htmlTextTbl.append(" colspan='");
//如果不相等,继续循环,直到找着相等的为止
int colspan = 1;
for(int edgeIndex = tempIndex+1; edgeIndex < leftEdges.length; edgeIndex++){
System.out.println(leftEdges[edgeIndex] == nextCell.getLeftEdge());
if(leftEdges[edgeIndex] != nextCell.getLeftEdge() ){
colspan ++;
}else{
break;
}
}
tempIndex += colspan;
htmlTextTbl.append(colspan+"'");
}
}
2. java poi xwpf操作word生成一个表格怎么合并单元格,求大神指导
用PageOffice实现比较简单,代码片段WordDocument doc = new WordDocument();Table table1 = doc.openDataRegion("Text").openTable(1);//打开已存在表格table1.setPreferredWidthType(WdPreferredWidthType.wdPreferredWidthPoints);table1.setPreferredWidth(350);//设置表格宽度table1.openColumn(1).setWidth(150, WdRulerStyle.wdAdjustFirstColumn);//设置第一列宽度//table1.openRow(2).setHeight(50);//设置第二行行高table1.setRowsHeight(50);//设置表格里所有行的行高table1.openCellRC(2, 1).setValue("PageOffice");//单元格填值table1.openCellRC(2, 1).getFont().setSize(15);//下面设置字体table1.openCellRC(2, 1).getFont().setColor(Color.GREEN);table1.openCellRC(2, 1).getFont().setBold(true);table1.openCellRC(2, 1).getFont().setItalic(true);table1.openCellRC(1, 1).mergeTo(1, 5);//第一行的5个单元格合并为一个单元。
3. poi xwpf 操作word表格,怎么合并单元格
for (int j = 0; j < tr.numcells();="" j++)="" {="" cell="tr.getCell(j);//取得单元格" if(j="">< tr.numcells()-1){//获取下一个单元格,用于判断是否合并="" nextcell="tr.getCell(j+1);" }else{="" nextcell="null;" }="" int[]="" leftedges="getLeftEdges(table,tr);">< leftedges.length-1="" &&="" cell.getleftedge()="" !="leftEdges[tempIndex+1]" ){="" htmltexttbl.append("="" colspan='"); //如果不相等,继续循环,直到找着相等的为止 int colspan = 1; for(int edgeIndex = tempIndex+1; edgeIndex < leftEdges.length; edgeIndex++){ System.out.println(leftEdges[edgeIndex] == nextCell.getLeftEdge()); if(leftEdges[edgeIndex] != nextCell.getLeftEdge() ){ colspan ++; }else{ break; } } tempIndex += colspan; htmlTextTbl.append(colspan+"' ");="">
4. 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()两个重载方法。
5. 怎样用poi在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();
}
6. 如何使用apache poi将word转化为html
Java可以使用这个开源框架,对word进行读取合并等操作,Apache POI是一个开源的利用Java读写Excel、WORD等微软OLE2组件文档的项目。最新的3.5版本有很多改进,加入了对采用OOXML格式的Office 2007支持,如xlsx、docx、pptx文档。 示例如下:import org.apache.poi.POITextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
//得到.doc文件提取器
org.apache.poi.hwpf.extractor.WordExtractor doc = new WordExtractor(new FileInputStream(filePath));
//提取.doc正文文本
String text = doc.getText();
//提取.doc批注
String[] comments = doc. getCommentsText();
2007
import org.apache.poi.POITextExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFComment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
//得到.docx文件提取器
org.apache.poi.xwpf.extractor.XWPFWordExtractor docx = new XWPFWordExtractor(POIXMLDocument.openPackage(filePath));
//提取.docx正文文本
String text = docx.getText();
//提取.docx批注
org.apache.poi.xwpf.usermodel.XWPFComment[] comments = docx.getDocument()).getComments();
for(XWPFComment comment:comments){
comment.getId();//提取批注Id
comment.getAuthor();//提取批注修改人
comment.getText();//提取批注内容
}
转载请注明出处51数据库 » poiwordxwpf