急求POI 将数据导出到Word的实例
发布网友
发布时间:2022-04-23 05:05
我来回答
共1个回答
热心网友
时间:2023-06-22 20:31
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 s = new ByteArrayInputStream(b);
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
DocumentEntry de = directory.createDocument("WordDocument", s);
FileOutputStream ostream = new FileOutputStream(path);
fs.writeFilesystem(ostream);
s.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 s = new ByteArrayInputStream(b);
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
DocumentEntry de = directory.createDocument("WordDocument", s);
FileOutputStream ostream = new FileOutputStream(path);
fs.writeFilesystem(ostream);
s.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;
}
}
*/
利用POI技术从数据库里提取数据,生成一个Excel文档或者Word文档
import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hwpf.extractor.WordExtractor;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem...
...poit里的内容全部快速地、一次性地拷进word的文档里?各位高手有办法...
1.在powerpoit里面按 文件-保存为(有的是换名保存或是另保存)。2.然后会弹出一个界面,在你输入名字的地方下面,有一个叫保存类型的,拉下去。3.然后选择word的方式就可以了。如果你的不是2003的也行。也是刚刚的方法把powerpoit保存成JPEG文件交换格式,然后在word保存就可以了。
如何用POI3.0生成WORD文档
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...
用JSP如何实现将查询出来的数据导入到Word文档中
2-纯JavaScript脚本实现。主要通过客户端调用本机Office组件来实现。3-在JSP页面引入头文件实现。纯JavaScript脚本实现细节方面大体是创建一个word组件ActiveXObject('Word.Application'),用js通过表ID取得表内容然后保存到word,要注意的是js实现有很多不好的地方,例如Internet选项需要把ActiveX空间全部启用,安...
将JSP页面(里面含有表格,表格数据是从数据库导出的)导出成Word
用最新版本的POI是支持到word2007的,如果你尝试后还不行,给我留言,我给你word2007的全套解析,你可以尝试自己写一套解析代码出来。
如何使用POI操作Word文本框中的内容
FileInputStream fis = new FileInputStream("e:/file.docx");XWPFDocument doc = new XWPFDocument(fis);List<XWPFParagraph> paragraphList = doc.getParagraphs();XWPFParagraph paragraph = paragraphList.get(10);文本框在Word中显示如图所示:第二步,获取XWPFParagraph的XmlObject,然后获得XmlObject对象...
java编程:有人做过导出word文件,并且word中附带excel文件的那种模式吗...
假如是.docx文件,用XWPFDocument 这个就可以,poi里面的类 然后是XWPFTable,XWPFTableRow,XWPFTableCell类。分别获得表格,表格的行,表格的格 doc就多了,网上随便找 但是假如又有段落还有表格,我也卡在这了,假如找到方法,麻烦提醒我一下!!
...在数据库读取的多条BLOB 数据一同插入一个word 中,并打印出来_百度...
首先从数据库里读取出来,然后写到word中(通过apache poi jar包),里面有一些列操作office套件的接口
java中如何用jacob将数据库中的二进制图片存到word中的指定位置
主要用到org.apache.poi 来操作word,而读取数据库图片 通过读取数据库Blob 字段的列然后通过 public static BufferedImage imgChangeBuffer(Blob blob)方法钩子一个 BufferedImage 然后把这个 BufferedImage 设置到word中,希望能帮上忙。
poihtml生成横向word
方式如下:1、首先比如有一篇文章的前边设置一个方向,如横向,然后后边都设置为纵向,这样可以先将插入点定位到纵向页面的结尾,2、当然也可以将所有内容全部圈起来,然后在应用于中选中所选文字进行更改,3、如果文章内容中有许多小节,也还可以选中你要更改的节,然后就点击应用于之后的所选节进行更改...