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="http://images.cnblogs.com/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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
如果你的项目不是Maven项目,你可以手动下载POI的jar包,并将其添加到你的项目中。 以下是一个简单的示例,演示如何使用POI数据导出到Excel中: ```java import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelWriter { public static void main(String[] args) throws IOException { // 创建Workbook对象 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建Sheet对象 Sheet sheet = workbook.createSheet("Sheet1"); // 创建表头 Row header = sheet.createRow(0); header.createCell(0).setCellValue("姓名"); header.createCell(1).setCellValue("年龄"); header.createCell(2).setCellValue("性别"); // 创建数据行 List<Person> persons = new ArrayList<>(); persons.add(new Person("张三", 20, "男")); persons.add(new Person("李四", 22, "女")); persons.add(new Person("王五", 25, "男")); int rowNum = 1; for (Person person : persons) { Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(person.getName()); row.createCell(1).setCellValue(person.getAge()); row.createCell(2).setCellValue(person.getGender()); } // 导出Excel文件 try (FileOutputStream outputStream = new FileOutputStream("output.xlsx")) { workbook.write(outputStream); } } private static class Person { private String name; private int age; private String gender; public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public int getAge() { return age; } public String getGender() { return gender; } } } ``` 在这个示例中,我们创建了一个`XSSFWorkbook`对象,并添加了一个名为`Sheet1`的Sheet。然后我们创建了表头和数据行,并使用`FileOutputStream`将Workbook对象写入到输出文件中。 请注意,这个示例使用了POI的XSSF API,因此需要导入`poi-ooxml`和`poi-ooxml-schemas`两个jar包。如果你的项目是Maven项目,则可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.1.1</version> </dependency> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值