您的位置:首页 > 编程语言 > Java开发

java的导出Excel表格

2017-10-09 11:05 405 查看
导出Excel表格

public void export(HttpServletRequest request, HttpServletResponse response){
try {
Integer totalRow1 = 0,totalRow2 = 0,totalRow3 = 0,totalRow4= 0;
List<String> deptName_list= new ArrayList<String>();
List<String> ylist=new ArrayList<String>();
List<String> tlist=new ArrayList<String>();
List<String> zlist=new ArrayList<String>();
List<String> yblist=new ArrayList<String>();
System.out.println(list.size());
for(Map<String,String> map:list){
String deptName=map.get("deptname");
deptNamelist.add(deptName);
String total=map.get("total");
ylist.add(total);
totalRow1 += Integer.parseInt(map.get("total"));
String tg=map.get("tg");
tlist.add(tg);
totalRow2 += Integer.parseInt(map.get("tg"));
String zbl=map.get("zbl");
zlist.add(zbl);
totalRow3 += Integer.parseInt(map.get("zbl"));
String ybl=map.get("ybj");
yblist.add(ybl);
totalRow4 += Integer.parseInt(map.get("ybj"));
}

deptName_list.add("总计");
yc_list.add(totalRow1.toString());
tg_list.add(totalRow2.toString());
zbl_list.add(totalRow3.toString());
ybj_list.add(totalRow4.toString());

String startTime=request.getParameter("startTime");
String endTime=request.getParameter("endTime");
String start_time="";
if(startTime!=null&&startTime.contains("-")){
start_time=startTime.replace("-", "");
}
String end_time="";
if(endTime!=null&&endTime.contains("-")){
end_time=endTime.replace("-", "");
}
if(deptName_list!=null&&!deptName_list.isEmpty()){
response.setContentType("application/ms-excel;charset=GB2312");
response.setHeader("Content-Disposition", "attachment; filename="+start_time+"-"+end_time+new String("理详情".getBytes("gb2312"), "ISO-8859-1")+".xls");
ServletOutputStream os=response.getOutputStream();
String[] titleContent={"部门名称","常","过","理","结"};
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
//构造标题
sheet.mergeCells(0, 0, titleContent.length-1, 0);//添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
WritableFont bold = new WritableFont(WritableFont.ARIAL,16,WritableFont.BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
WritableCellFormat titleFormate = new WritableCellFormat(bold);//生成一个单元格样式控制对象
titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中
titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//单元格的内容垂直方向居中BLACK
titleFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.RED);
Label title = new Label(0,0,"督查处理详情",titleFormate);
sheet.setRowView(0, 800, false);//设置第一行的高度
sheet.addCell(title);

//构造表头
for (int i = 0; i < titleContent.length; i++) {
WritableFont titleContentBlod = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
WritableCellFormat titleContentFormate = new WritableCellFormat(titleContentBlod);//生成一个单元格样式控制对象
titleContentFormate.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中
titleContentFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//单元格的内容垂直方向居中
titleContentFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.RED);
Label lable = new Label(i,1,titleContent[i],titleContentFormate);
sheet.addCell(lable);
}

WritableFont content = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
WritableCellFormat contentFormate = new WritableCellFormat(content);
contentFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//添加边框
for(int i=0;i<deptName_list.size();i++){
Label deptNameLable = new Label(0,i+2,deptName_list.get(i),contentFormate);
sheet.addCell(deptNameLable);
Label ycNumLable = new Label(1,i+2,yc_list.get(i),contentFormate);
sheet.addCell(ycNumLable);
Label tgNumLable = new Label(2,i+2,tg_list.get(i),contentFormate);
sheet.addCell(tgNumLable);
Label zblNumLable = new Label(3,i+2,zbl_list.get(i),contentFormate);
sheet.addCell(zblNumLable);
Label ybjNumLable = new Label(4,i+2,ybj_list.get(i),contentFormate);
sheet.addCell(ybjNumLable);
}
workbook.write();
workbook.close();
os.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: