67,515
社区成员
发帖
与我相关
我的任务
分享
1.一下是导出Excel的工具类:
public class ExportExcel {
private static Log log = LogFactory.getLog(ExportExcel.class);
public static void expExcel(HttpServletResponse res, String fileName, String sheetName, String[] titles, Vector vector) throws Exception{
OutputStream ops = null;
WritableWorkbook workBook = null;
try{
ops = res.getOutputStream(); //取得输出流
res.reset(); //清空输出流
res.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls"); //设定输出文件流
res.setContentType("application/msexcel"); //定义输出类型
workBook = Workbook.createWorkbook(ops); //创建新的Excel工作薄
WritableSheet wsheet = workBook.createSheet(sheetName, 0);
WritableFont font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat format = new WritableCellFormat(font);
for(int i = 0; i < titles.length; i++){
Label label = new Label(i, 0, titles[i], format);
wsheet.addCell(label);
}
font = new WritableFont(WritableFont.createFont("宋体"),12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
format = new WritableCellFormat(font);
for(int j = 0 ; j < vector.size(); j++){
String[] sdata = (String[]) vector.elementAt(j);
for (int k = 0; k < sdata.length; j++) { //在索引0的位置创建单元格(左上端)
Label wlabel1 = new Label(k, j, sdata[k], format); //行、列、单元格中的文本、文本格式
wsheet.addCell(wlabel1);
System.out.println(sdata[k] + ":::" + sdata[k]);
}
}
workBook.write();
}catch(Exception e){
log.error("Java Exception", e);
}finally{
workBook.close();
ops.close();
}
}
}