//创建httpServletResponse对象
HttpServletResponse response = ServletActionContext.getResponse();
//设置Excel报表名称
String head = "事件报表";
//自动转换其格式
try {
head = new String(head.getBytes("gb2312"), "ISO8859-1");
} catch (UnsupportedEncodingException e1) {
}
//生成报表名称
response.setHeader("Content-disposition","attachment;filename="+head+".xls");
try{
//生成报表 表头
String titles[] = new String[2];
titles[0] = "表头内容1";
titles[1] = "表头内容2";
//创建OutputStream输出流
OutputStream os = response.getOutputStream();
//创建操作excel的Workbook对象 jxl的
WritableWorkbook workbook;
//实例化workbook
workbook = Workbook.createWorkbook(os);
//给excel创建一个sheet
WritableSheet sheet = workbook.createSheet("报表sheet",0);
/**设置标题**/
//设置单元格
sheet.mergeCells(0, 0, 2, 1);//合并单元格的 ( 最左列号[开始列 包含] 左上角的行号 最右列号[结束列 包含] 右下角的行号)
// 设置字体
WritableFont wf = new WritableFont(WritableFont.ARIAL,15,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(jxl.format.Alignment.CENTRE);
//创建一行(一个行号的元素)
Label label1 = new Label(0,0,"事件统计报表",wcf);//
//将该单元格添加到该页
sheet.addCell(label1);
//创建表头
for(int i = 0 ; i < 2 ; i++)
{
Label label = new Label(i,2,titles[i]);
sheet.addCell(label);
}
/**填充表格内容**/
//从第几行开始填充
int curcount = 3;
try {
//注入第一列内容
sheet.addCell(new jxl.write.Label(0, curcount,"内容"));
//注入第二列内容
sheet.addCell(new jxl.write.Label(1, curcount,"内容"));
//跳到下一行
curcount = curcount + 1
} catch (Exception e) {
return null;
}
//设置列宽
sheet.setColumnView(0, 30);
sheet.setColumnView(1, 30);
workbook.write();
workbook.close();
}
catch(Exception e)
{
return "导出错误:"+e.getMessage();
}
return null;
JAVA 导出 Excel 报表
猜你喜欢
转载自blog.csdn.net/qq_35401216/article/details/81866126
今日推荐
周排行