java html导出excel_java实现导出网页数据到excel

org.apache.poi.hssf.usermodel.HSSFWorkbook,

org.apache.poi.hssf.usermodel.HSSFSheet,

org.apache.poi.hssf.usermodel.HSSFRow,

org.apache.poi.hssf.usermodel.HSSFCell"%>

Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

">

Listlist=(List)request.getAttribute("ceshiList");StringtitleStr= "导出的excel的名称";Stringtitle= new String(titleStr.getBytes("gb2312"),"iso-8859-1");

response.setHeader("Content-Disposition","attachment;filename=\"" + title +".xls"+"\"");

HSSFWorkbook wb= newHSSFWorkbook();

HSSFFont font=wb.createFont();

font.setFontHeightInPoints((short)14);

font.setBoldweight((short)600);

HSSFSheet sheet=wb.createSheet("sheet1");

sheet.setColumnWidth((short)0,(short)(1500));

sheet.setColumnWidth((short)1,(short)(4500));

sheet.setColumnWidth((short)2,(short)(4500));

sheet.setColumnWidth((short)3,(short)(4500));

HSSFCellStyle style=wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

HSSFCellStyle styleFont=wb.createCellStyle();

styleFont.setAlignment(HSSFCellStyle.ALIGN_CENTER);

styleFont.setFont(font);//以下以写表头//表头为第一行

HSSFRow head=sheet.createRow((short)0);

HSSFCell headCell=head.createCell((short)0);

sheet.addMergedRegion(newRegion(0,(short)0,0,(short)3));//合并单元格

HSSFRow row=sheet.createRow((short)1);

HSSFCell cell1=row.createCell((short)0);

HSSFCell cell2=row.createCell((short)1);

HSSFCell cell3=row.createCell((short)2);

HSSFCell cell4=row.createCell((short)3);

headCell.setEncoding(HSSFCell.ENCODING_UTF_16);

headCell.setCellType(HSSFCell.CELL_TYPE_STRING);

headCell.setCellStyle(styleFont);

cell1.setEncoding(HSSFCell.ENCODING_UTF_16);

cell1.setCellType(HSSFCell.CELL_TYPE_STRING);

cell1.setCellStyle(style);

cell2.setEncoding(HSSFCell.ENCODING_UTF_16);

cell2.setCellType(HSSFCell.CELL_TYPE_STRING);

cell2.setCellStyle(style);

cell3.setEncoding(HSSFCell.ENCODING_UTF_16);

cell3.setCellType(HSSFCell.CELL_TYPE_STRING);

cell3.setCellStyle(style);

cell4.setEncoding(HSSFCell.ENCODING_UTF_16);

cell4.setCellType(HSSFCell.CELL_TYPE_STRING);

cell4.setCellStyle(style);//定义表头的内容

headCell.setCellValue(titleStr);

cell1.setCellValue("序号");

cell2.setCellValue("姓名");

cell3.setCellValue("班级");

cell4.setCellValue("生日");

SimpleDateFormat sdf= newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");for(inti=1; i

Ceshi ceshi=list.get(i-1);//定义数据从第二行开始

row=sheet.createRow((short) i+ 1);

cell1=row.createCell((short)0);

cell2=row.createCell((short)1);

cell3=row.createCell((short)2);

cell4=row.createCell((short)3);

headCell.setEncoding(HSSFCell.ENCODING_UTF_16);

headCell.setCellType(HSSFCell.CELL_TYPE_STRING);

headCell.setCellStyle(styleFont);

cell1.setEncoding(HSSFCell.ENCODING_UTF_16);

cell1.setCellType(HSSFCell.CELL_TYPE_STRING);

cell1.setCellStyle(style);

cell2.setEncoding(HSSFCell.ENCODING_UTF_16);

cell2.setCellType(HSSFCell.CELL_TYPE_STRING);

cell2.setCellStyle(style);

cell3.setEncoding(HSSFCell.ENCODING_UTF_16);

cell3.setCellType(HSSFCell.CELL_TYPE_STRING);

cell3.setCellStyle(style);

cell4.setEncoding(HSSFCell.ENCODING_UTF_16);

cell4.setCellType(HSSFCell.CELL_TYPE_STRING);

cell4.setCellStyle(style);//填充内容

cell1.setCellValue(i);if(ceshi.getUserName() != null){

cell2.setCellValue(ceshi.getUserName());

}else{

cell2.setCellValue("");

}if(ceshi.getClassName() != null){

cell3.setCellValue(ceshi.getClassName());

}else{

cell3.setCellValue("");

}if(ceshi.getBirth() != null){

cell4.setCellValue(sdf.format(ceshi.getBirth()));//转换数据库出来的date类型为简单日期格式

}else{

cell4.setCellValue("");

}

}

wb.write(response.getOutputStream());

response.getOutputStream().flush();

out.clear();

response.getOutputStream().close();

out.clear();

out=pageContext.pushBody();%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值