jxl导出数据到excel

File excel = new File("d:/dd.xls");
   if(!excel.exists()){
    excel.createNewFile();
   }  

try {
WritableWorkbook wbook = Workbook.createWorkbook(excel); //建立excel文件
WritableSheet wsheet = wbook.createSheet("教室信息表", 0); //工作表名称
//设置Excel字体
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
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的行号
List list = cs.findAllClassroom(); //这个是从数据库中取得要导出的数据
Iterator it = list.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();
} catch (Exception e) {

throw new PaikeException("导出文件出错");

}

你可能感兴趣的:(C++,c,Excel,C#)