数据库中数据导出成excel

HSSFWorkbook workbook = new HSSFWorkbook();
  OutputStream os = null;
  try {
   os = response.getOutputStream();
  } catch (IOException e1) {

   e1.printStackTrace();
  }
  response.reset();
  response.setContentType("application/vnd.ms-excel");
  if (request.getHeader("User-Agent").indexOf("Firefox") != -1) {
   response.setHeader("Content-Disposition",
     "attachment; filename=dc.xls");
  } else {
   response.setHeader("Content-Disposition",
     "attachment; filename="+wu+".xls");  //导出文件名
  }

  HSSFCellStyle cellstyle = workbook.createCellStyle();
  cellstyle.setBorderLeft((short) 1);
  cellstyle.setBorderRight((short) 1);
  cellstyle.setBorderTop((short) 1);
  cellstyle.setBorderBottom((short) 1);

  HSSFFont myfont = workbook.createFont();
  myfont.setFontName("Verdana");
  myfont.setFontHeightInPoints((short) 8);
  cellstyle.setAlignment((short) 2);
  cellstyle.setWrapText(true);
  cellstyle.setFont(myfont);
  HSSFSheet sheet = workbook.createSheet("new sheet");
  sheet.setDefaultColumnWidth(Short.valueOf("15"));
  HSSFRow row = null;
  try {
   row = sheet.createRow(0);
   HSSFCell cell11 = row.createCell((short) 0);
   cell11.setCellStyle(cellstyle);
   cell11.setEncoding((short) 1);
   cell11.setCellValue("列名1"); //列名
   cell11 = row.createCell((short) 1);
   cell11.setCellStyle(cellstyle);
   cell11.setEncoding((short) 1);
   cell11.setCellValue("列名2");
   cell11 = row.createCell((short) 2);
   cell11.setCellStyle(cellstyle);
   cell11.setEncoding((short) 1);
   cell11.setCellValue("列名3");
cell11 = row.createCell((short) 3);
   
   cell11.setCellStyle(cellstyle);
   cell11.setEncoding((short) 1);
   cell11.setCellValue("列名4");
   cell11 = row.createCell((short) 4);
   
   cell11.setCellStyle(cellstyle);
   cell11.setEncoding((short) 1);
   cell11.setCellValue("列名5");
   
   if (list1 != null && list1.size() > 0) { //list为从数据库读出的数据,以下是将list循环写入excel
    for (int i = 0; i < list1.size(); i++) {
     row = sheet.createRow((short) sheet.getLastRowNum() + 1);

     HSSFCell cell = row.createCell((short) 0);
     cell.setCellStyle(cellstyle);
     cell.setEncoding((short) 1);
     cell.setCellValue(list1.get(i).toString());
     cell = row.createCell((short) 1);
     cell.setCellStyle(cellstyle);
     cell.setEncoding((short) 1);
     cell.setCellValue(originality);
     cell = row.createCell((short) 2);
     cell.setCellStyle(cellstyle);
     cell.setEncoding((short) 1);
     cell.setCellValue(q);
     cell = row.createCell((short) 3);
     cell.setCellStyle(cellstyle);
     cell.setEncoding((short) 1);
     cell.setCellValue(1);
     cell = row.createCell((short) 4);
     cell.setCellStyle(cellstyle);
     cell.setEncoding((short) 1);
     cell.setCellValue("1111");
    }
   }

workbook.write(os);
   os.flush();
   os.close();
   os = null;
  } catch (Exception e) {
   e.printStackTrace();
  }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值