先要导入poi3.7.jar包
public class VipExcelServlet extends HttpServlet {
ConsumeBiz consumeBiz = new ConsumeBizImpl();
//访问网址:http://localhost:8080/excelExport/servlet/ExcelExportServlet
public void doGet(HttpServletRequest request, HttpServletResponse response) {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
HttpSession session=request.getSession();
String time1 = (String) session.getValue("ctime1");
String time2 = (String) session.getValue("ctime2");
String ctype = (String) session.getValue("ctype");
String cvnumber = (String) session.getValue("cvnumber");
List<Consume> consumelist = consumeBiz.getConsume(cvnumber,ctype,time1,time2);
response.setContentType("octets/stream");
String excelName = "会员消费记录表";
try {
response.addHeader("Content-Disposition", "attachment;filename="+new String( excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
OutputStream out = response.getOutputStream();
List<Consume> list = getConsume(consumelist);
// exportExcel(excelName,headers,list, out);
exportExcel(excelName,list, out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @Description: 模拟从数据库中查询出来的数据,一般是数据表中的几列
* @Auther: lujinyong
* @Date: 2013-8-22 下午2:53:58
*/
private List<Consume> getConsume(List<Consume> consumelist) throws Exception
{
List<Consume> list = new ArrayList();
for(int i =0; i<consumelist.size();i++){
list.add(consumelist.get(i));
}
return list;
}
/**
*
* @Description: 生成excel并导出到客户端(本地)
* @Auther: lujinyong
* @Date: 2013-8-22 下午3:05:49
*/
protected void exportExcel(String title,List<Consume> list,OutputStream out){
//声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
//设置表格默认列宽度为15个字符
sheet.setDefaultColumnWidth(20);
//生成一个样式,用来设置标题样式
HSSFCellStyle style = workbook.createCellStyle();
//设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式,用于设置内容样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
//产生表格标题行
HSSFRow row = sheet.createRow(0);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("流水号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("金额");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("类型");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("会员ID");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("交易日");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("操作员");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("分店");
cell.setCellStyle(style);
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
Consume con = (Consume) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(con.getCNumber());
row.createCell((short) 1).setCellValue((double) con.getCMony());
row.createCell((short) 2).setCellValue(con.getCType().equals("1")?"XX":"XX");
row.createCell((short) 3).setCellValue(con.getCVnumber());
cell = row.createCell((short) 4);
cell.setCellValue(con.getCTime());
row.createCell((short) 5).setCellValue(con.getCUnumber());
row.createCell((short) 6).setCellValue(con.getCSsid().equals("1001")?"XXX":"XXX");
}
try {
workbook.write(out);
System.out.println("excel导出成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}