zoukankan
html css js c++ java
POI往Word中插入字符串我的实践
代码
package
lili.com;
import
java.io.ByteArrayInputStream;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.OutputStream;
import
org.apache.poi.hwpf.HWPFDocument;
import
org.apache.poi.hwpf.extractor.WordExtractor;
import
org.apache.poi.hwpf.model.StyleSheet;
import
org.apache.poi.hwpf.usermodel.CharacterProperties;
import
org.apache.poi.hwpf.usermodel.CharacterRun;
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
org.apache.poi.poifs.filesystem.DirectoryEntry;
import
org.apache.poi.poifs.filesystem.DocumentEntry;
import
org.apache.poi.poifs.filesystem.POIFSFileSystem;
public
class
POITest
{
/** */
/**
*
@param
args
*
@throws
Exception
*/
public
static
void
main(String[] args)
throws
Exception
{
//
TODO Auto-generated method stub
try
{
FileInputStream in
=
new
FileInputStream(
new
File(
"
E:\\demo1.doc
"
));
POIFSFileSystem pfs
=
new
POIFSFileSystem(in);
HWPFDocument hwpf
=
new
HWPFDocument(pfs);
Range range
=
hwpf.getRange();
StyleSheet styleSheet
=
hwpf.getStyleSheet();
TableIterator it
=
new
TableIterator(range);
//
遍历一个DOC中的所有表格
while
(it.hasNext())
{
Table tb
=
(Table) it.next();
System.out.println(tb.numRows());
//
遍历表格的行
//
for (int i = 0; i < tb.numRows(); i++) {
//
TableRow tr = tb.getRow(i);
//
//
遍历表格的列
//
System.out.println(tr.numCells());
//
for (int j = 0; j < tr.numCells(); j++) {
//
//
往表格中插入数据
//
TableCell td = tr.getCell(j);
//
//
CharacterRun chr1= td.getCharacterRun(0);
//
//
System.out.println("td--"+chr1.getFontName());
//
//
String text = "第" + i + "行第" + j + "列";
//
//
int p = td.numParagraphs();
//
System.out.println("td.numParagraphs :"+p);
//
for(int a=0;a<p;a++)
//
{
//
Paragraph para =td.getParagraph(a);
//
para.insertBefore(text+" ");
//
//
/ para.insertAfter(text);
//
int q=para.numCharacterRuns();
//
System.out.println("numCharacterRuns--"+q);
//
for(int b=0;b<q;b++)
//
{
//
CharacterRun chr= para.getCharacterRun(b);
//
//
System.out.println("para.getCharacterRun-- "+ chr.getFontName()+" text--"+chr.text().trim()+" str--"+chr.toString());
//
//
//
ParagraphProperties pp = new ParagraphProperties();
//
//
//
if(j%2==0){
//
//
//
}
//
}
//
}
//
}
//
}
}
//
WordExtractor we=new WordExtractor(in);
String text
=
range.text();
System.out.println(text);
//
在表格外面插入内容
CharacterProperties cp
=
new
CharacterProperties();
cp.setBold(
true
);
cp.setCharacterSpacing(
10
);
cp.setChse(cp.SPRM_CHARSCALE);
cp.setCapitalized(
true
);
int
p
=
range.numParagraphs();
for
(
int
i
=
0
;i
<
p;i
++
)
{
String name
=
"
飞舞
"
;
String age
=
"
23
"
;
Paragraph para
=
range.getParagraph(i);
//
System.out.println(p);
//
para.insertAfter("--插入 成功!!",cp);
para.replaceText(
"
姓名:————————————
"
,
"
姓名:
"
+
name);
para.replaceText(
"
年龄:————————————
"
,
"
年龄:
"
+
age);
CharacterRun chr
=
para.getCharacterRun(
0
);
System.out.println(
"
第
"
+
i
+
"
段:
"
+
chr.text());
}
System.out.println(
"
\n插入后 生成新文档demo1-----------------------------------------\n
"
);
text
=
range.text();
System.out.println(text);
//
File outputFile = new File("C:/456.doc");
//
OutputStream output = new FileOutputStream(outputFile);
//
hwpf.write(output);
//
output.close();
//
//
byte b[] = content.getBytes("ISO-8859-1");
//
String content="这是固定的字符2";
byte
b[]
=
text.getBytes();
ByteArrayInputStream bais
=
new
ByteArrayInputStream(b);
POIFSFileSystem fs
=
new
POIFSFileSystem();
DirectoryEntry directory
=
fs.getRoot();
DocumentEntry de
=
directory.createDocument(
"
WordDocument
"
, bais);
FileOutputStream ostream
=
new
FileOutputStream(
"
e:/demo.doc
"
);
fs.writeFilesystem(ostream);
bais.close();
ostream.close();
}
catch
(Exception ex)
{
ex.printStackTrace();
}
}
}
查看全文
相关阅读:
第01组 团队项目-需求分析报告
团队项目-选题报告
第08组 Beta冲刺(4/5)
第08组 Beta冲刺(3/5)
第08组 Beta冲刺(2/5)
第08组 Beta冲刺(1/5)
第08组 Alpha事后诸葛亮
第08组 Alpha冲刺(6/6)
第08组 Alpha冲刺(5/6)
第08组 Alpha冲刺(4/6)
原文地址:https://www.cnblogs.com/monica/p/1604703.html
最新文章
第二次结对编程作业
第12组 团队展示
在intellij idea 中进行android 单元测试
推荐书
【技术团队博客】
HTML5 form内button
java.lang.NegativeArraySizeException
BM算法--串匹配
javascrip异步问题
分解质因数法求最大公约数(javascrip实现)
热门文章
javascrip 求最大公因数(分解质数法)发生的问题
【转载】Virtual Box下配置Host-Only联网方式详解
第01组 Alpha事后诸葛亮
第01组 Alpha冲刺(6/6)
第01组 Alpha冲刺(5/6)
第01组 Alpha冲刺(4/6)
第01组 Alpha冲刺(3/6)
第01组 Alpha冲刺(2/6)
第01组 Alpha冲刺(1/6)
第01组 团队Git现场编程实战
Copyright © 2011-2022 走看看