程序
response.reset();
response.setContentType("application/vnd.ms-excel"); //改成输出excel文件
response.setHeader("Content-disposition","attachment; filename=file.xls" );
OutputStream os =response.getOutputStream();
Workbook wb;
WritableWorkbook wwb=null;
try {
wb = Workbook.getWorkbook(new java.io.File(request.getRealPath("/")+"/excel/exportFile.xls"));
wwb = Workbook.createWorkbook(os, wb);
} catch (BiffException e) {
e.printStackTrace();
}
if(wwb==null) {
return;
}
WritableSheet ws1 = null;
try {
ws1 = wwb.getSheet(0);
Label label = new Label(0, 0, "值", getNormolCell());
ws1.addCell(label);
} catch(Exception ex) {
ex.printStackTrace();
System.out.println("写入Excel文件发生错误!!!");
}
try {
wwb.write();
os.flush();
wwb.close();
}catch(Exception ex){
ex.printStackTrace();
}finally {
if( os != null)
os.close();
}
样式
public static WritableCellFormat getNormolCell() {// 9号字体,上下左右居中,带黑色边框
WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 9);
WritableCellFormat format = new WritableCellFormat(font);
try {
format.setAlignment(jxl.format.Alignment.CENTRE);
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
} catch (WriteException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return format;
}