很重啊,会不会对主板不好会怎么样

10:34 提问
使用poi操作word时如何在有多个表格的word中定位到其中一个表格。
XWPFTable table1=new XWPFTable(cttbl, docx);
这里的cttbl是什么?如何设置一个表格的cttbl?
按赞数排序
public static void getWordXAndStyle(InputStream in,String fileName,String path,String type) throws Exception {
XWPFDocument docx = (XWPFDocument) getDocument(in,type);
Iterator&IBodyElement& iBody = docx.getBodyElementsIterator();
int curT = 0;//当前操作对象的索引
int curP = 0;//当前操作对象的索引
//htmlText = "&html&&head&&meta http-equiv=\"Content-Type\" content=\"text/ charset=utf-8\" /&&title&&/title&&/head&&body&";
htmlText = "";
while(iBody.hasNext()){
IBodyElement body = iBody.next();
if(BodyElementType.TABLE.equals(body.getElementType())){//处理表格
XWPFTable table = body.getBody().getTableArray(curT);
List&XWPFTable& tables = body.getBody().getTables();
table = tables.get(curT);
if(table != null){
htmlText = htmlText+readTableX(table);
}else if(BodyElementType.PARAGRAPH.equals(body.getElementType())){//处理段落
XWPFParagraph ph = body.getBody().getParagraphArray(curP);
if(ph != null){
htmlText = htmlText+readParagraphX(ph);
//htmlText = htmlText + "&/body&&/html&";
writeFile(htmlText,fileName,path,type);
public static String readTableX(XWPFTable tb) throws Exception {
htmlTextTbl="";
List&XWPFTableRow& rows = tb.getRows();
for(XWPFTableRow row:rows){
//int rowHight = row.getHeight();
String tr = "";
List&XWPFTableCell& cells = row.getTableCells();
for(XWPFTableCell cell:cells){
String text = "";
List&XWPFParagraph& graphs = cell.getParagraphs();
//遍历段落
for(XWPFParagraph pg:graphs){
text = text+pg.getText()+"&br/&";
tr += "&td&"+text+"&/td&";
htmlTextTbl += "&tr&"+tr+"&/tr&";
htmlTextTbl = "&table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"tbl2\"&"+htmlTextTbl+"&/table&&br/&";
return htmlTextT
public static String readParagraphX(XWPFParagraph p) throws Exception {
String tempStr = "";
String text = p.getText();
if(StringUtil.isEmpty(text)){
tempStr = tempStr + "&br/&";
tempStr = tempStr+"&span&"+text+"&/span&&br/&";
return tempS
你是怎么定位到其中一个table的,我并不知道我的table在word中是第几个
操作word中多个表格的话,插件PageOffice也是可以实现的,动态创建表格
DataRegion drTable2= doc.createDataRegion("PO_table2", DataRegionInsertType.After, "PO_table1");是在"PO_table1"后面动态创建一个新的数据区域"PO_table2",用于创建新的一个5行5列的表格table2。如果要定位表格的话,可以直接根据数据区域名称进行定位的。可以了解了解的
其他相似问题java生成带有样式、表格、不定图片的word主要用的技术:freemarkpoijacob简述
现在项目有个需求,要根据选择的内容生成word文档,文档本身带样式,里面有表
格,根据选择的内容图片数量也不定。本人研究的比较浅显,以下所述也只代表个人
观点。poi操作word有两个方面:一、操作word文档,读写,生成word的话用的是
poi规定的一些方法,这种情况下要根据要求生成表格比较麻烦,即使能生成word,
但要生成带一定样式的word也比较费劲;二、可以word文档里面自己做的标记替
换成想要的内容,如将content替换成一段描述,这个替换也可以是图片。
freemark生成word,将word转存成xml,再将xml后缀名直接修改成ftl,然后就像
平常用freemark一样,最后生成word即可(这块网上示例较多,不做啰嗦)。
freemark也可以替换word里面的图片,但不灵活。jacob,本身对word可做的事可
能很多,但这里我只用到了替换图片。本来想的是,将需求方给出的word模板,制
作成ftl模板,利用freemark替换内容生成word,所有该放图片的地方,放入特定标
签(如${iamge1}),再用poi将特定标签换成图片,但最后发现poi打不开第三方生
成的word文档,只好将替换图片这一步换成用jacob来做,下面给出源码。frmmark生成word
自己百度poi替换图片
注意:这套代码只适用于word 2007,并且 poi 版本 3.7
&dependency&
&groupId&org.apache.poi&/groupId&
&artifactId&poi&/artifactId&
&version&3.7&/version&
&scope&compile&/scope&
&/dependency&
&dependency&
&groupId&org.apache.poi&/groupId&
&artifactId&poi-ooxml&/artifactId&
&version&3.7&/version&
&/dependency&
&dependency&
&groupId&org.apache.poi&/groupId&
&artifactId&poi-ooxml-schemas&/artifactId&
&version&3.7&/version&
&/dependency&/** * 适用于word 2007 * poi 版本 3.7 */public class WordUtil {
* 根据指定的参数值、模板,生成 word 文档
* @param param 需要替换的变量
* @param template 模板
public static CustomXWPFDocument generateWord(Map&String, Object& param, String template) {
CustomXWPFDocument doc = null;
OPCPackage pack = POIXMLDocument.openPackage(template);
//HWPFDocument doc =
FileInputStream in=new FileInputStream(new File(template));
doc=new HWPFDocument(in);*/
doc = new CustomXWPFDocument(pack);
if (param != null && param.size() & 0) {
//处理段落
List&XWPFParagraph& paragraphList = doc.getParagraphs();
processParagraphs(paragraphList, param, doc);
//处理表格
Iterator&XWPFTable& it = doc.getTablesIterator();
while (it.hasNext()) {
XWPFTable table = it.next();
List&XWPFTableRow& rows = table.getRows();
for (XWPFTableRow row : rows) {
List&XWPFTableCell& cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
List&XWPFParagraph& paragraphListTable =
cell.getParagraphs();
processParagraphs(paragraphListTable, param, doc);
} catch (Exception e) {
e.printStackTrace();
* 处理段落
* @param paragraphList
public static void processParagraphs(List&XWPFParagraph&
paragraphList,Map&String, Object& param,CustomXWPFDocument doc){
if(paragraphList != null && paragraphList.size() & 0){
for(XWPFParagraph paragraph:paragraphList){
List&XWPFRun& runs = paragraph.getRuns();
for (XWPFRun run : runs) {
String text = run.getText(0);
if(text != null){
boolean isSetText = false;
for (Map.Entry&String, Object& entry : param.entrySet()) {
String key = entry.getKey();
if(text.indexOf(key) != -1){
isSetText = true;
Object value = entry.getValue();
if (value instanceof String) {//文本替换
text = text.replace(key, value.toString());
} else if (value instanceof Map) {//图片替换
text = text.replace(key, "");
Map pic = (Map)
int width = Integer.parseInt(pic.get("width").toString());
int height = Integer.parseInt(pic.get("height").toString());
int picType = getPictureType(pic.get("type").toString());
byte[] byteArray = (byte[]) pic.get("content");
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);
int ind =doc.addPicture(byteInputStream, picType);
doc.createPicture(ind, width , height,paragraph);
} catch (Exception e) {
e.printStackTrace();
if(isSetText){
run.setText(text,0);
* 根据图片类型,取得对应的图片类型代码
* @param picType
* @return int
private static int getPictureType(String picType){
int res = CustomXWPFDocument.PICTURE_TYPE_PICT;
if(picType != null){
if(picType.equalsIgnoreCase("png")){
res = CustomXWPFDocument.PICTURE_TYPE_PNG;
}else if(picType.equalsIgnoreCase("dib")){
res = CustomXWPFDocument.PICTURE_TYPE_DIB;
}else if(picType.equalsIgnoreCase("emf")){
res = CustomXWPFDocument.PICTURE_TYPE_EMF;
}else if(picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")){
res = CustomXWPFDocument.PICTURE_TYPE_JPEG;
}else if(picType.equalsIgnoreCase("wmf")){
res = CustomXWPFDocument.PICTURE_TYPE_WMF;
* 将输入流中的数据写入字节数组
* @param in
public static byte[] inputStream2ByteArray(InputStream in,boolean isClose){
byte[] byteArray = null;
int total = in.available();
byteArray = new byte[total];
in.read(byteArray);
} catch (IOException e) {
e.printStackTrace();
if(isClose){
in.close();
} catch (Exception e2) {
System.out.println("关闭流失败");
return byteA
}}/** * poi把word中的${image1}替换成图片 * @author * @date
*/public class Test {
public static void main(String[] args) throws Exception {
Map&String, Object& param = new HashMap&String, Object&();
Map&String,Object& header = new HashMap&String, Object&();
header.put("width", 512);
header.put("height", 384);
header.put("type", "jpg");
header.put("content", WordUtil.inputStream2ByteArray(new FileInputStream("d:/image1.jpg"), true));
param.put("${image1}",header);
CustomXWPFDocument doc = WordUtil.generateWord(param, "d://image.docx");
FileOutputStream fopts = new FileOutputStream("d:/image2.doc");
doc.write(fopts);
fopts.close();
}}jacob替换图片
&dependency&
&groupId&net.sf.jacob-project&/groupId&
&artifactId&jacob&/artifactId&
&version&1.14.3&/version&
&/dependency&
运行报错时根据提示将dll文件放到java_home/bin下面即可dll文件链接/**
* 用于给指定的word文档中某处字符串做替换
* @param textPath
待替换的word文档
* @param targetWord
待替换的字符串
* @param replaceWord
要替换成的字符串
public static void replaceWord(String textPath, String targetWord,
String replaceWord) {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
app.setProperty("Visible", new Variant(false));// 设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
Dispatch.Method,
new Object[] { textPath, new Variant(false),
new Variant(false) }, new int[1]).toDispatch();
// 打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,
// 因为我们要保存原文件,所以以可写方式打开。
Dispatch selection = app.getProperty("Selection").toDispatch();// 获得对Selection组件
Dispatch.call(selection, "HomeKey", new Variant(6));// 移到开头
Dispatch find = Dispatch.call(selection, "Find").toDispatch();// 获得Find组件
Dispatch.put(find, "Text", targetWord);// 查找字符串targetWord
Dispatch.call(find, "Execute");// 执行查询
Dispatch.put(selection, "Text", replaceWord);// 替换为replaceWord
Dispatch.call(doc, "Save");// 保存
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
app.safeRelease();
* 创建一个word文档
* @param txtContent 要写入word文档的内容
* @param fileName 要保存的word文档路径
public static void createWordFile(String txtContent, String fileName) {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
app.setProperty("Visible", new Variant(false));// 设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs, "Add").toDispatch();
Dispatch selection = Dispatch.get(app, "Selection").toDispatch();
Dispatch.put(selection, "Text", txtContent);
Dispatch.call(Dispatch.call(app, "WordBasic").getDispatch(),
"FileSaveAs", fileName);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
app.safeRelease();
* 根据现有的txt文本来创建word
* @param txt txt文本路径
* @param wordFile word路径
public static void createWordWithTxt(String txt, String wordFile) {
String txtContent = null;
txtContent = bufferedReader(txt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
createWordFile(txtContent, wordFile);
* @param path
* @throws IOException
public static String bufferedReader(String path) throws IOException {
File file = new File(path);
if (!file.exists() || file.isDirectory())
throw new FileNotFoundException();
BufferedReader br = new BufferedReader(new FileReader(file));
String temp = null;
StringBuffer sb = new StringBuffer();
temp = br.readLine();
while (temp != null) {
sb.append(temp + " ");
temp = br.readLine();
return sb.toString();
* 给指定的word文档在字符串指定位置插入图片
* @param wordFile word文档
* @param imagePath
待添加图片的路径
* @param tarStr 指定的字符串位置
public static void insertImage(String wordFile, String imagePath,
String tarStr) {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
app.setProperty("Visible", new Variant(false));// 设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
Dispatch.Method,
new Object[] { wordFile, new Variant(false),
new Variant(false) }, new int[1]).toDispatch();
// 打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,
// 因为我们要保存原文件,所以以可写方式打开。
Dispatch selection = app.getProperty("Selection").toDispatch();
Dispatch.call(selection, "HomeKey", new Variant(6));// 移到开头
Dispatch find = Dispatch.call(selection, "Find").toDispatch();// 获得Find组件
Dispatch.put(find, "Text", tarStr);// 查找字符串tarStr
Dispatch.call(find, "Execute");// 执行查询
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
"AddPicture", imagePath);// 在指定位置插入图片
Dispatch.call(doc, "Save");// 保存
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
app.safeRelease();
* 给指定的word文档在字符串指定位置插入图片
* @param wordFile word文档
* @param replaceInfos
待添加图片的信息
public static void replaceImage(String wordFile,List&Map&String,String&& replaceInfos) {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
app.setProperty("Visible", new Variant(false));// 设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
Dispatch.Method,
new Object[] { wordFile, new Variant(false),
new Variant(false) }, new int[1]).toDispatch();
// 打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,
// 因为我们要保存原文件,所以以可写方式打开。
Dispatch selection = app.getProperty("Selection").toDispatch();
Dispatch.call(selection, "HomeKey", new Variant(6));// 移到开头
Dispatch find = Dispatch.call(selection, "Find").toDispatch();// 获得Find组件
for(int i=0;i&replaceInfos.size();i++){
Map&String,String& info=replaceInfos.get(i);
Dispatch.put(find, "Text", info.get("tarStr"));// 查找字符串tarStr
Dispatch.call(find, "Execute");// 执行查询
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
"AddPicture", info.get("imagePath"));// 在指定位置插入图片
Dispatch.put(find, "Text", info.get("tarStr"));// 查找字符串targetWord
Dispatch.call(find, "Execute");// 执行查询
Dispatch.put(selection, "Text", "");// 替换为replaceWord
Dispatch.call(doc, "Save");// 保存
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
app.safeRelease();
* 在指定word文档的指定位置创建一个表格
* @param wordFile 指定word文档
* @param pos 指定位置
* @param numCols 列数
* @param numRows 行数
public static void createTable(String wordFile, String pos, int numCols,
int numRows) {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
Dispatch selection = null;
Dispatch doc = null;
Dispatch docs = null;
boolean b = false;
app.setProperty("Visible", new Variant(false));// 设置word不可见
docs = app.getProperty("Documents").toDispatch();
doc = Dispatch.invoke(
Dispatch.Method,
new Object[] { wordFile, new Variant(false),
new Variant(false) }, new int[1]).toDispatch();
// 打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,
// 因为我们要保存原文件,所以以可写方式打开。
selection = app.getProperty("Selection").toDispatch();
} catch (Exception e) {
e.printStackTrace();
if (pos == null || pos.equals(""))
b = false;
// 从selection所在位置开始查询
Dispatch find = app.call(selection, "Find").toDispatch();
// 设置要查找的内容
Dispatch.put(find, "Text", pos);
// 向前查找
Dispatch.put(find, "Forward", "True");
// 设置格式
Dispatch.put(find, "Format", "True");
// 大小写匹配
Dispatch.put(find, "MatchCase", "True");
// 全字匹配
Dispatch.put(find, "MatchWholeWord", "True");
// 查找并选中
b = Dispatch.call(find, "Execute").getBoolean();
// 创建表格
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
Dispatch range = Dispatch.get(selection, "Range").toDispatch();
Dispatch newTable = Dispatch.call(tables, "Add", range,
new Variant(numRows), new Variant(numCols)).toDispatch();
Dispatch.call(selection, "MoveRight");
System.out.println("没有找到指定的位置,请检查是否存在这样的位置。");
Dispatch.call(doc, "Save");// 保存
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
app.safeRelease();
* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
WordUtil2.insertImage("d:/简报.docx", "d:/image1.jpg", "37fbbed065");
/* System.out.println("Hello! World");
System.out.println(System.getProperty("java.library.path"));*/
最新教程周点击榜
微信扫一扫

我要回帖

更多关于 主板会不会影响性能 的文章

 

随机推荐