看看这个例子 希望对你有帮助!
package com.cari.web.cache;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
public class outecell {
public void exportClassroom(OutputStream os) throws ParseException {
try {
WritableWorkbook wbook= Workbook.createWorkbook(os); //建立excel文件
WritableSheet wsheet = wbook.createSheet("监测报表", 0); //工作表名称
//设置Excel字体
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
WritableCellFormat titleFormat = new WritableCellFormat(wfont);
String[] title = { "通道名称", "特征值类型", "最大值", "最小值","平均值" };
//设置Excel表头
for (int i = 0; i < title.length; i++) {
Label excelTitle = new Label(i, 0, title[i], titleFormat);
wsheet.addCell(excelTitle);
}
/*
int c = 1; //用于循环时Excel的行号
//ClassroomService cs = new ClassroomService();
List jiancebaobiao=null;
//List list = cs.findAllClassroom(); //这个是从数据库中取得要导出的数据
Iterator it = jiancebaobiao.iterator();
while (it.hasNext()) {
ClassroomDTO crdto = (ClassroomDTO) it.next(); //对象
Label content1 = new Label(0, c, crdto.getRoomname());
Label content2 = new Label(1, c, crdto.getCapicity().toString());
Label content3 = new Label(2, c, crdto.getRoomTypeId().toString());
Label content4 = new Label(3, c, crdto.getRemark());
wsheet.addCell(content1);
wsheet.addCell(content2);
wsheet.addCell(content3);
wsheet.addCell(content4);
c++;
} */
wbook.write(); //写入文件
wbook.close();
os.close();
} catch (Exception e) {
throw new ParseException("导出文件出错", 1);
}
}
public static void main(String[] args) {
File f=new File("F://监测报表.xls");
try {
f.createNewFile();
new outecell().exportClassroom(new FileOutputStream(f));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2010年10月11日 10:20