/******导出Excel
* @param
* fileName 文件名,sheetNames sheet名称数组,lists 数据List数组
* dataTypeStrings 数据类型数组
* *******/
public static void downLoadExcel(HttpServletResponse response,String fileName, String[] sheetNames,List[] lists){
OutputStream os = null;
try {
os = response.getOutputStream();
response.reset();
response.setContentType("UTF-8");
response.setContentType("Application/x-msdownload");
// response.setHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
response.setHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1")+".xls");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
WritableWorkbook workbook = Workbook.createWorkbook(os);
WritableSheet sheet=null;
WritableFont wf = new WritableFont(WritableFont.TIMES, 15, WritableFont.BOLD, false);
WritableCellFormat titleFormat=new WritableCellFormat(wf);
//wcFormat.setBackground(Colour.RED);//设置单元格的颜色为红色
//数字格式
//NumberFormat nf = new NumberFormat("#");
//WritableCellFormat numberfmt = new jxl.write.WritableCellFormat(nf);
//Number labelNF = new Number(1, 1, 3.1415926, wcfN);
//这个显示当前时间的所有信息,包括年月日小时分秒
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat df = new DateFormat("yyyy-MM-dd hh:mm:ss");
WritableCellFormat datefmt = new WritableCellFormat(df);
//DateTime labelDTF = new DateTime(1, 3, new java.util.Date(), wcfDF);
Number labelNF=null;
Label label=null;
DateTime labelDTF=null;
try {
for (int i = 0; i < sheetNames.length; i++) {
sheet= workbook.createSheet(sheetNames[i], 0);
String [] strings=null;
for (int h = 0; h < lists[i].size(); h++) {
strings=(String[]) lists[i].get(h);
for (int j = 0; j < strings.length; j++) {
try {
if (h==0) {
label = new Label(j,h,strings[j],titleFormat);
//label = new Label(j,h,strings[j]);
sheet.addCell(label);
}else {
if (StringUtils.isNumber(strings[j])) {//数字
labelNF = new Number(j,h,Float.parseFloat(strings[j]));
sheet.addCell(labelNF);
}else {
label = new Label(j,h,strings[j]);
sheet.addCell(label);
}
/*else if (new Integer(dataTypes[j])==2){//时间
labelDTF=new DateTime(j,h,sdf.parse(strings[j]) ,datefmt);
sheet.addCell(labelDTF);
} */
}
} catch (Exception e) {
// TODO: handle exception
label = new Label(j,h,strings[j]);
sheet.addCell(label);
}
}
}
}
}catch (Exception e) {
// TODO: handle exception
}
workbook.write();
workbook.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
response.flushBuffer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}