锘??xml version="1.0" encoding="utf-8" standalone="yes"?>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://www.tkk7.com/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;
}
}
*/
涓嬭澆浠ュ悗鎶婅В鍘嬪緱鍒扮殑鏂囦歡鍔犲埌web-inf涓嬮潰鐨刲ib鐩綍涓嬮潰灝卞彲浠ヤ嬌鐢ㄤ簡銆?br />
涓嬮潰鍏堜粙緇嶄竴涓嬬敓鎴恊xcel鏂囦歡瑕佺敤鐨勭被錛?br />
HSSFWorkbook 鐢ㄤ簬鍒涘緩excel鏂囦歡
HSSFSheet 鐢ㄤ簬鍒涘緩excel鐨勪功鍐?br />
HSSFRow 鐢ㄤ簬鍒涘緩涓琛?br />
HSSFCell 鐢ㄤ簬鍒涘緩涓涓崟鍏冩牸
HSSFCellStyle 鐢ㄤ簬鍒涘緩涓涓崟鍏冩牸鐨勬牸寮?br />
HSSFFont 鐢ㄤ簬鍒涘緩涓涓崟鍏冩牸鐨勫瓧浣撴牸寮?br />
涓嬮潰鐢ㄦ湰浜哄啓鐨勪竴涓氱敤鐨勫伐鍏風被浣滀負渚嬪瓙璇存槑涓涓嬬敓鎴愭枃浠剁殑鏂規硶
錛堣繖閲屽彧鍒楀嚭閮ㄥ垎浠g爜錛岃緇嗙殑璇風湅闄勪歡鐨勬簮鐮侊紝濡傛灉瑕佷嬌鐢ㄩ檮浠剁殑浠g爜錛岃淇濈暀鍘熷垱浜虹殑鍚嶅瓧錛?br />
public String createFile(List<Hashtable> hlist)//鏍規嵁浼犲叆鐨勫弬鏁拌鍙栨暟鎹紝榪欓噷鐢ㄤ簡鑼冨瀷鎸囧畾鍙傛暟鏄痟ashtable綾誨瀷鐨刲ist
{
HSSFWorkbook wb = new HSSFWorkbook();//鍒涘緩excel鏂囦歡
for(int hcount=0;hcount<hlist.size();hcount++)
{
Hashtable ha = hlist.get(hcount);//鑾峰彇絎琱count涓厓绱?/span>
HSSFSheet sheettemp = wb.createSheet();
wb.setSheetName(hcount,(String)ha.get("name"),(short)1);//鏂板緩涓涓猻heet
HSSFRow rowtitle = sheettemp.createRow((short)0);//娣誨姞琛ㄥご
HSSFCell celltitle = rowtitle.createCell((short)0);
celltitle.setEncoding(HSSFCell.ENCODING_UTF_16);//璁劇疆瀛椾綋緙栫爜
celltitle.setCellValue((String)ha.get("title"));//璁劇疆琛ㄥご鍐呭
rowtitle = sheettemp.createRow((short)1);//鏂板涓琛岀敤浜庡瓨鏀捐〃澶村唴瀹?/span>
String[] fields = (String[])ha.get("field");//鑾峰彇鍒楀ご
sheettemp.addMergedRegion(new Region(0,(short)0,0,(short)(fields.length-1)));//鍚堝茍絎竴琛?/span>
rowtitle = sheettemp.getRow(0);//鑾峰緱絎竴琛岀殑寮曠敤
celltitle = rowtitle.getCell((short)0);//鑾峰緱絎竴琛岀涓涓崟鍏冩牸鐨勫紩鐢?/span>
setTitleStyle(wb,celltitle);//璁劇疆琛ㄥご鏍煎紡
HSSFRow rowfield = sheettemp.createRow((short)1);//鏂板涓琛岀敤浜庡瓨鏀懼垪澶村唴瀹?/span>
for(int i = 0;i<fields.length;i++)//鏍規嵁鑾峰彇鍒扮殑鍒楀ご鏁版嵁錛屽驚鐜緗垪澶寸殑鍐呭
{
HSSFCell cellfield = rowfield.createCell((short)i);
cellfield.setEncoding(HSSFCell.ENCODING_UTF_16);
cellfield.setCellValue(fields[i]);
setFieldStyle(wb,cellfield);//璁劇疆鍒楀ご鏍煎紡
int xx = cellfield.getStringCellValue().length();
sheettemp.setColumnWidth((short) i,(short)(600*xx));
}
List listtemp = (List)ha.get("body");//鑾峰彇琛ㄦ牸鍐呭
Object[] rowtemp = null;
int count = 0;
for(Iterator it = listtemp.iterator();it.hasNext();)//榪唬鐢熸垚鏁版嵁
{
rowtemp = (Object[])it.next();
HSSFRow rowbody = sheettemp.createRow((short)(count+2));
for(int i = 0;i<rowtemp.length;i++)
{
HSSFCell cellbody = rowbody.createCell((short)i);
cellbody.setEncoding(HSSFCell.ENCODING_UTF_16);
cellbody.setCellValue((String)rowtemp[i]);
}
count++;
}
}
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(setFilepath());//鐢熸垚鏂囦歡
try {
wb.write(fileOut);
} catch (IOException e) {
System.out.println(e.toString());
}
try {
fileOut.close();
} catch (IOException e) {
System.out.println(e.toString());
}
} catch (FileNotFoundException e) {
System.out.println(e.toString());
}
return filename;
}
package test;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5
6import org.apache.poi.hssf.usermodel.HSSFCell;
7import org.apache.poi.hssf.usermodel.HSSFCellStyle;
8import org.apache.poi.hssf.usermodel.HSSFFont;
9import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
10import org.apache.poi.hssf.usermodel.HSSFRichTextString;
11import org.apache.poi.hssf.usermodel.HSSFRow;
12import org.apache.poi.hssf.usermodel.HSSFSheet;
13import org.apache.poi.hssf.usermodel.HSSFWorkbook;
14
15public class ExcelTest {
16
17 public static void main(String[] args) throws IOException {
18
19 // create a new file
20 FileOutputStream out = new FileOutputStream("D:/workbook.xls");
21 // create a new workbook
22 HSSFWorkbook wb = new HSSFWorkbook();
23 // create a new sheet
24 HSSFSheet sheet = wb.createSheet();
25
26 //2.model
27 HSSFRow row = sheet.createRow(2);
28 row.setHeightInPoints(20);
29 HSSFCell cell = row.createCell(2);
30 HSSFFont cnFont = wb.createFont();
31 cnFont.setFontHeightInPoints((short) 10);
32 //font.setFontName("姹変華鎶ュ畫綆");
33 cnFont.setFontName("闅朵功");
34 HSSFCellStyle cnStyle = wb.createCellStyle();
35 cnStyle.setFont(cnFont);
36 cell.setCellStyle(cnStyle);
37 HSSFRichTextString richText = new HSSFRichTextString("涓枃瀛椾綋嫻嬭瘯");
38 cell.setCellValue(richText);
39 HSSFCell enCell = row.createCell(3);
40 HSSFFont enFont = wb.createFont();
41 enFont.setFontHeightInPoints((short) 10);
42 enFont.setFontName("Arial Black");
43 HSSFCellStyle enStyle = wb.createCellStyle();
44 enStyle.setFont(enFont);
45 enCell.setCellStyle(enStyle);
46 enCell.setCellValue(new HSSFRichTextString("English font test"));
47 sheet.setColumnWidth(2, 4000);
48 sheet.setColumnWidth(3, 4000);
49
50 //3.output
51 sheet.setDisplayGridlines(false);
52 sheet.setPrintGridlines(false);
53 HSSFPrintSetup printSetup = sheet.getPrintSetup();
54 //A4綰?/span>
55 printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
56 wb.write(out);
57 out.close();
58 }
59}
60