java封装导出Excel

本文介绍了一种使用Java实现的导出多页Excel的方法,包括如何创建Excel工作簿、设置样式、填充数据等。该方法支持自定义文件名、表单名称及数据,并能处理数字与日期格式。

/******导出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();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值