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();
}
}
}
查看全文
相关阅读:
OpenCV && C++ 01
图像矩的理解
Halcon Example
LabVIEW
Working Experience
Working Experience
Working Experience
C++
Trigger,Cursor
Paging
原文地址:https://www.cnblogs.com/monica/p/1604703.html
最新文章
day 15 面向对象
day 13内置函数一 (巨多)
day 12 函数生成器
day 11 迭代器 闭包
day 10 函数的进阶(动态传参,名称空间和作用域,函数的嵌套)
day 09 初识函数(后面有很多天都在接触函数)
day 08 文件操作
day 07 深浅拷贝
1、线性代数
3.1、Factorization Machine模型
热门文章
2.2、Softmax Regression算法实践
2.1、Softmax Regression模型
9、指针、数组和指针算术
1.2、Logistics Regression算法实践
8、C++指针和自由存储空间
7、C++枚举类型
6、C++共用体
5、C++结构体的使用
JAVA的7大模块
OpenCV && C++ 02
Copyright © 2011-2022 走看看