开通会员
  • 尊享所有功能
  • 文件大小最高200M
  • 文件无水印
  • 尊贵VIP身份
  • VIP专属服务
  • 历史记录保存30天云存储
开通会员
您的位置:首页 > 帮助中心 > poi将word转pdf-急求POI 将数据导出到Word的实例
帮助中心 >

poi将word转pdf-急求POI 将数据导出到Word的实例

2022-12-06 16:38:06

急求poi 将数据导出到word的实例

急求poi 将数据导出到word的实例
import java.io.*;
import java.util.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.util.littleendian;

public class wordtest {
public wordtest() {
}
public static boolean writewordfile(string path, string content) {
boolean w = false;
try {

// byte b[] = content.getbytes("iso-8859-1");
byte b[] = content.getbytes();

bytearrayinputstream bais = new bytearrayinputstream(b);

poifsfilesystem fs = new poifsfilesystem();
directoryentry directory = fs.getroot();

documententry de = directory.createdocument("worddocument", bais);

fileoutputstream ostream = new fileoutputstream(path);

fs.writefilesystem(ostream);

bais.close();
ostream.close();

} catch (ioexception e) {
e.printstacktrace();
}
return w;
}
public static void main(string[] args){
boolean b = writewordfile("e://test.doc","hello");
}
}
/*
public string extracttext(inputstream in) throws ioexception {
arraylist text = new arraylist();
poifsfilesystem fsys = new poifsfilesystem(in);

documententry headerprops = (documententry) fsys.getroot().getentry("worddocument");
documentinputstream din = fsys.createdocumentinputstream("worddocument");
byte[] header = new byte[headerprops.getsize()];

din.read(header);
din.close();
// prende le informazioni dall'header del documento
int info = littleendian.getshort(header, 0xa);

boolean usetable1 = (info & 0x200) != 0;

//boolean usetable1 = true;

// prende informazioni dalla piece table
int complexoffset = littleendian.getint(header, 0x1a2);
//int complexoffset = littleendian.getint(header);

string tablename = null;
if (usetable1) {
tablename = "1table";
} else {
tablename = "0table";
}

documententry table = (documententry) fsys.getroot().getentry(tablename);
byte[] tablestream = new byte[table.getsize()];

din = fsys.createdocumentinputstream(tablename);

din.read(tablestream);
din.close();

din = null;
fsys = null;
table = null;
headerprops = null;

int multiple = findtext(tablestream, complexoffset, text);

stringbuffer sb = new stringbuffer();
int size = text.size();
tablestream = null;

for (int x = 0; x < size; x++) {

wordtextpiece nextpiece = (wordtextpiece) text.get(x);
int start = nextpiece.getstart();
int length = nextpiece.getlength();

boolean unicode = nextpiece.usesunicode();
string tostr = null;
if (unicode) {
tostr = new string(header, start, length * multiple, "utf-16le");
} else {
tostr = new string(header, start, length, "iso-8859-1");
}
sb.append(tostr).append(" ");

}
return sb.tostring();
}

private static int findtext(byte[] tablestream, int complexoffset, arraylist text)
throws ioexception {
//actual text
int pos = complexoffset;
int multiple = 2;
//skips through the prms before we reach the piece table. these contain data
//for actual fast saved files
while (tablestream[pos] == 1) {
pos++;
int skip = littleendian.getshort(tablestream, pos);
pos += 2 + skip;
}
if (tablestream[pos] != 2) {
throw new ioexception("corrupted word file");
} else {
//parse out the text pieces
int piecetablesize = littleendian.getint(tablestream, ++pos);
pos += 4;
int pieces = (piecetablesize - 4) / 12;
for (int x = 0; x < pieces; x++) {
int filepos =
littleendian.getint(tablestream, pos + ((pieces + 1) * 4) + (x *<img src="/images/forum/smiles/icon_cool.gif"/> + 2);
boolean unicode = false;
if ((filepos & 0x40000000) == 0) {
unicode = true;
} else {
unicode = false;
multiple = 1;
filepos &= ~(0x40000000); //gives me fc in doc stream
filepos /= 2;
}
int totlength =
littleendian.getint(tablestream, pos + (x + 1) * 4)
- littleendian.getint(tablestream, pos + (x * 4));

wordtextpiece piece = new wordtextpiece(filepos, totlength, unicode);
text.add(piece);

}

}
return multiple;
}
public static void main(string[] args){
wordtest w = new wordtest();
poifsfilesystem ps = new poifsfilesystem();
try{

file file = new file("c:\\test.doc");

inputstream in = new fileinputstream(file);
string s = w.extracttext(in);
system.out.println(s);

}catch(exception e){
e.printstacktrace();
}

}
public boolean writewordfile(string path, string content) {
boolean w = false;
try {

// byte b[] = content.getbytes("iso-8859-1");
byte b[] = content.getbytes();

bytearrayinputstream bais = new bytearrayinputstream(b);

poifsfilesystem fs = new poifsfilesystem();
directoryentry directory = fs.getroot();

documententry de = directory.createdocument("worddocument", bais);

fileoutputstream ostream = new fileoutputstream(path);

fs.writefilesystem(ostream);

bais.close();
ostream.close();

} catch (ioexception e) {
e.printstacktrace();
}

return w;
}

}

class wordtextpiece {
private int _fcstart;
private boolean _usesunicode;
private int _length;

public wordtextpiece(int start, int length, boolean unicode) {
_usesunicode = unicode;
_length = length;
_fcstart = start;
}
public boolean usesunicode() {
return _usesunicode;
}

public int getstart() {
return _fcstart;
}
public int getlength() {
return _length;
}

}
*/
您已连续签到 0 天,当前积分:0
  • 第1天
    积分+10
  • 第2天
    积分+10
  • 第3天
    积分+10
  • 第4天
    积分+10
  • 第5天
    积分+10
  • 第6天
    积分+10
  • 第7天

    连续签到7天

    获得积分+10

获得10积分

明天签到可得10积分

咨询客服

扫描二维码,添加客服微信