项目中要写excel,把这个例子写出来,以后可以看。
1.写excel类
package net.blogjava.chenlb;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
* Jxl 的 Excel写数据器.
* @author chenlb 2007-10-29 上午10:39:31
*/
public class JxlExcelWriter {
/**
* @param datas 封装着Object[]的列表, 一般是String内容.
* @param title 每个sheet里的标题.
*/
public void writeExcel(OutputStream out, List datas, String[] title) {
if(datas == null) {
throw new IllegalArgumentException("写excel流需要List参数!");
}
try {
WritableWorkbook workbook = Workbook.createWorkbook(out);
WritableSheet ws = workbook.createSheet("sheet 1", 0);
int rowNum = 0; //要写的行
if(title != null) {
putRow(ws, 0, title);//压入标题
rowNum = 1;
}
for(int i=0; i<datas.size(); i++, rowNum++) {//写sheet
Object[] cells = (Object[]) datas.get(i);
putRow(ws, rowNum, cells); //压一行到sheet
}
workbook.write();
workbook.close(); //一定要关闭, 否则没有保存Excel
} catch (RowsExceededException e) {
System.out.println("jxl write RowsExceededException: "+e.getMessage());
} catch (WriteException e) {
System.out.println("jxl write WriteException: "+e.getMessage());
} catch (IOException e) {
System.out.println("jxl write file i/o exception!, cause by: "+e.getMessage());
}
}
private void putRow(WritableSheet ws, int rowNum, Object[] cells) throws RowsExceededException, WriteException {
for(int j=0; j<cells.length; j++) {//写一行
Label cell = new Label(j, rowNum, ""+cells[j]);
ws.addCell(cell);
}
}
}
2.使用
public void testWriteExcel() {
List datas = new ArrayList();
String[] data = {"1", "chenlb"};
datas.add(data);
try {
OutputStream out = new FileOutputStream(new File("doc/chenlb.blogjava.net.xls"));
JxlExcelWriter jxlExcelWriter = new JxlExcelWriter();
jxlExcelWriter.writeExcel(out, datas, new String[] {"Id", "name"});
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
posted on 2007-10-29 10:52
流浪汗 阅读(5904)
评论(1) 编辑 收藏 所属分类:
JAVA/J2EE