代碼
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);
????????????????
//
?遍歷一個(gè)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++)?{
//
????????????????????????????
//
?往表格中插入數(shù)據(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);
????????????????
????????????????
//
?在表格外面插入內(nèi)容
????????????????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();
????????????}
????}
?
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

