2024年3月5日发(作者:)
POI插入WORD表格
篇一:poi操作word
package .;
import ;
import .;
import .;
import .;
import ption;
import ist;
import p;
import or;
import ;
import ;
import ;
import Document;
import
dFormatException;
import
aphAlignment;
import cument;
import ragraph;
import n;
import ble;
import bleCell;
import bleRow;
import eption;
import en;
import
isualDrawingProps; import
tiveSize2D;
import
ne;
public class PoiWordTemplate {
private XWPFDocument document;public XWPFDocument
getDocument() { } return document; public void
setDocument(String templatePath) { try { nt
= new
XWPFDocument( ckage(templatePath)); } catch (IOException e) {} // TODO Auto-generated
catch block tackTrace();
//nt = document;
}
/**
* 往模板中插入图片
* @param document
* @param mapImage
* @param width
* @param height
*/
public void replaceTextToImage(Map
mapImage,int width,int height){ List
listParagraphs = agraphs();
for(int i=0;i<();i++){
for (Entry
{
if
((i).getText().trim().indexOf(()) != -1) { CTInline inline
(i).createRun().getCTR().addNewDrawing().addNewInline();
try {
insertPicture(ue(),inline,width,height);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
tackTrace();
} catch ( e) {
// TODO Auto-generated catch block
tackTrace();
}
}
}
}
}
/**
* 替换word中的文字
* @param document
* @param mapValue* @param newText
*/
public void replaceTextToText(Map
mapValue){
List
List
agraphs();
for(int i=0;i<();i++){
listRun = (i).getRuns();
for(int j=0;j<();j++){ =
if(((j).getText(0)) != null ||
"null".equals(((j).getText(0)))){
(j).setText(((j).getText(0)),0); }
}
} } /*** 往表格中插入数据* @param document* @param
list* @param position*/ public void
setTablesValue(List
position,int columnNum) { Iterator
lesIterator();
int count = 0;
Map
HashMap
while (t()) {
XWPFTable table = (XWPFTable) ();
if(count == position){
XWPFTableRow firstRow = (1);
List
for(int i=0;i<();i++){
(i+"", (i).getText().trim()); }
Row(1);//删除第二行 for(int
i=0;i<();i++){XWPFTableRow row =
Row();//创建新的一行时创建了第
ght(30);
/* for(int j=0;j<=columnNum-2;j++){
TableCell();
}*/ 一个cell
}int rcount = berOfRows(); for (int i = 1;
i < rcount; i++) {XWPFTableRow newRow =
(i);List
leCells();for(int
j=0;j<();j++){
(j).setText((i-1).get((j+""))+"");//标题栏不用改变单元格的值
}
}
}
count++;
}
}
/*** 设置用电建议书里的基本信息 第一个表格*/
public void setOfferBorkBasicInfo(Map
map,List
Iterator
lesIterator();
int count = 0;
Map
HashMap
while (t()) {
XWPFTable table = (XWPFTable) ();
if(count == position){
int rcount = berOfRows();
for (int i = 0; i < rcount; i++)
{if(i<=4){ XWPFTableRow newRow = (i);
List
for(int
j=0;j<();j++){if(!"null".equals(((j).getText().trim())+"")){String temp =
(j).getText().trim()+"";(j).removeParagraph(0);(j).setText((temp)+""); } }} } XWPFTableRow sixRow = (6);
List
for(int i=0;i<();i++){(i+"",
(i).getText().trim()); } for(int
i=0;i<();i++){
XWPFTableRow row = Row();//创建新的一行时创建了第
ght(35);
for(int j=0;j<=6;j++){
TableCell();
}
一个cell
} Row(6);//删除模板中的字段行 int rowCount
= berOfRows(); int k= 0; for (int i = 6; i
< rowCount; i++) {XWPFTableRow tempRow =
(i);List
leCells();for(int
j=0;j<();j++){//
(j).removeParagraph(0);
(j).setText((k).get((j+""))+"");}k++;
}
}
count++;
}
}
/*** 生成用电分析结果与优化用电建议* @param list*
@param position*/ public void
setAnanlyResultTbl(List
position){Iterator
lesIterator();
int count = 0;
while (t()) {
XWPFTable table = (XWPFTable) (); if(count ==
position){ XWPFTableRow tempRow = (1);
List
XWPFTableCell cell = (0);
List
篇二:poi导出WORD表格居中
不废话,直接上代码
XWPFDocument doc = newXWPFDocument(); XWPFTabletable =
Table(5, 5);
CTTblPrtablePr =
bl().addNewTblPr();Jc().setVal();
篇三:POI--WORD部分的示例
Apache POI
----Word部分
最近在研究Apache POI,不过只研究了如何做word的部分。网上对于Excel等的介绍也很多例子也很多,但是对于word
缺少的可怜,导致我学的也很费劲,只能算是会了皮毛。但是整理了下例子,也方便以后大家看。最好能有高手把这个word部分的知识和例子多传到网上给大家分享。
Apache POI是一个开源的利用Java读写Excel、WORD等微软OLE2组件文档的项目。最新的版本有很多改进,加入了对采用OOXML格式的 Office 2007支持,如xlsx、docx、pptx文档。以下是POI的几个重要组成部分,以及各组件的功能概述。
POIFS是该项目的最古老,最稳定的一部分。.这是格式化OLE
2复合文档为纯Java的接口。 它同时支持读写功能。所有的组件,最终都依赖于它的定义
HSSF: MS-Excel 97-2003(.xls),基于BIFF8格式的JAVA接口。
XSSF:MS-Excel 2007+(.xlsx),基于OOXML格式的JAVA接口。
HWPF: MS-Word 97-2003(.doc),基于BIFF8格式的JAVA接口。只支持.doc文件简单的操作,读写能力有限。本API为POI项目早期开发,很不幸的 是主要负责HWPF模块开发的工程师-"Ryan Ackley"已经离开Apache组织,现在该模块没有人维护、更新、完善。 XWPF:MS-Word 2007+(.docx),基于OOXML格式的JAVA接口。较HWPF功能完善。
Word例子:
1读取一个word里的内容,只能读取纯文字,word里不能有图片表格等,否则图片和表格就会成为乱码。输出结果在后台显示
import .;
import .;
import ption;
import tractor;
//输出文字
public class world {
public static void main(String [] args){
file;
try {
file = new ("d:");
WordExtractor extractor;
try {
extractor = new WordExtractor(file);
String st=t();
n(st);
} catch (IOException e) {
// TODO Auto-generated catch block
tackTrace();
}
} catch ( e) {
// TODO Auto-generated catch block
tackTrace();
}
}
}
用另一种方式也可以输出文字
public class world2 {
}
}
2,把从一个word里读出来的内容写到另一个里,虽然可以写进去,但是只能把文字、数字、字母等写进去,图片和表格依旧不可以。而且写进去的文字只能是字符形式写进去,这样在打开word文档时就会有个转换器的问题。但是可以写进去,不知道怎么解决。
import .*;
import cument;
import tputStream;
import ;
public class wordWrite {
public static void main(String [] args){try { public
static void main(String[] args) throws Exception{ file;
try { ("d:"); HWPFDocument hDocument= new
HWPFDocument(file); Range rang= ge();
String string=();n(string); }
catch ( e) {} // TODO Auto-generated catch block
tackTrace();
//用了HWPFDocument API对于他的解释是:作为一个存储桶,我们把所有的word数据抛出到其中 我的理解就是把输入流读取(转 载于: 小 龙 文档网:poi,插入word表格)的word数据都放到他这里了 以便调用方法使用
HWPFDocument hDocument= new HWPFDocument(new
("d:"));
Range range= ge(); String
st=();//获得了word里的内容
writeDoc("d:",st);//调用写入的方法
n(st);
}
} tackTrace();}} public static boolean
writeDoc(String path, String string) {boolean w =
false; try { } return w; byte b[] =
es("utf-8"); ByteArrayInputStream bais =
new ByteArrayInputStream(b); POIFS fs = new POIFS();
DirectoryEntry directory = t(); DocumentEntry
de = Document("WordDocument", ostream
= new (path); (ostream); ();
(); tackTrace(); bais); } catch
(IOException e) {
3、把word文档里的某个内容替换成所需要的别的内容。也可以说是修改成自己想要的内容。但是还是仅限于修改文字。
public class word {
public static void main(String [] args){try
{ HWPFDocument hDocument= new HWPFDocument(new Range
range= ge();("d:"));
//在这里把word里的name替换成组织 当然word里要已经有了name这个单词
其实这个时候replaceText()方法并没有真正的替换了你原有word里的内容 你打开文档里面依旧显示name 但是你看后台输出的内容是已经修改了的 这就又需要把修改的内容写进word
eText("name","组织1"); String
st=(); n(st);} catch
(Exception e)
{ tackTrace();}writeDoc("d:",st);
}
public static boolean writeDoc(String path, String
string) {
我研究好久没解决这个问题
boolean w = false;
try { byte b[] = es(); //这里的写入我换了个方式 不过类似 依旧是以字节的形式写入 还是会有什么转换器的错误 fs = new (“d://”);HWPFOutputStream
hos = new HWPFOutputStream();(b, 0,
);o(fs);();w=true; } catch
(Exception e) { tackTrace(); } return w; }
4,把整个word里的内容读出来 写入另一个里 这次无论是图片还是文字或者表格都可以写入,而且没有那个转换器的错误了。但是其实就相当于复制了一个word到另一个word里。却无法实现修改内容在写入啊 或者别的。
public class word2 {
public static void main(String[] args) { try { POIFS
fs = new POIFS(new
("d:"));
HWPFDocument hDocument = new HWPFDocument(fs);
} }} Range range = ge(); out = new
("d:"); (out); ();
(); n(st);
tackTrace(); String st = (); } catch
(Exception e) {


发布评论