1. 怎么用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();
}
2. poi中,如何向Word文档里添加表格
//创建一个表格
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("字段四:");
3. 怎么用java poi生成word表格
rt java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class XwpfTUtil {
/*String filePath = "/sta.docx";
InputStream is;
XWPFDocument doc;
Mapparams = new HashMap();
{
params.put("${name}", "xxx");
params.put("${sex}", "男");
params.put("${political}", "共青团员");
params.put("${place}", "sssss");
params.put("${classes}", "3102");
params.put("${id}", "213123123");
params.put("${qq}", "213123");
params.put("${tel}", "312313213");
params.put("${oldJob}", "sadasd");
params.put("${swap}", "是");
params.put("${first}", "asdasd");
params.put("${second}", "综合事务部");
params.put("${award}", "asda");
params.put("${achievement}", "完成科协网站的开发");
params.put("${advice}", "没有建议");
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的单元格
添加区域,合并单元格
[c-sharp] view plaincopyprint?
1.Region region = new Region((short)rowFrom,(short)columnFrom,(short)rowTo
2.(short)columnTo);//合并从第rowFrom行columnFrom列
3.sheet.addMergedRegion(region);// 到rowTo行columnTo的区域
4.//得到所有区域
5.sheet.getNumMergedRegions()
转载请注明出处51数据库 » poiword表格增加行