从数据库中导出数据以excel格式保存到本地

public String exportExcel() throws IOException{

  response.setContentType("application/vnd.ms-excel;charset=utf-8");
  String fileName = new String("*****.xls".getBytes("GBK"),"ISO-8859-1");
  response.setHeader("Content-Disposition","attachment;filename="+fileName);
  response.addHeader("Pragma", "no-cache");
  response.addHeader("Cache-Control", "no-cache");

//从数据库中查询数据
  List<Map<String, String>> data = flowService.getFlowDetailList(areaIds, billingCycle, acctBookIds, applyNo, busiTypeIds, applyStates, applyStaffId, 0, Integer.MAX_VALUE);
  
  ListDataExcelExport export = new ListDataExcelExport();
  String[] cols = new String[]{"apply_no","branch","counties","acct_book_name","busi_type_id","busi_type_name","staff_desc","billing_cycle","name","close_flag","cur_step","cur_step_staff_name","acct_number"};
  String[] colsName = new String[]{"报账单号","分公司","区县","账套","业务编号","业务类","报账员","账期","报账单状态","处理状态","当前环节","当前环节人员名称","流水号"};
  export.export(cols, colsName, data, response.getOutputStream());
 
  return null;
 }

 ListDataExcelExport.java

public class ListDataExcelExport{
 
 private String [] cols;
 private String [] colsName;
 private List<Map<String, String>> data;
 private HSSFWorkbook wb;
 private HSSFSheet sheet;
 private short rowIdx = 0;
 
 public ListDataExcelExport() {
  wb = new HSSFWorkbook();
  sheet = wb.createSheet("Sheet1");
 }
 
 public String getFileSuffix() {
  return "xls";
 }
 
 public void export(String [] cols, String [] colsName,List<Map<String, String>> data, OutputStream outputStream) throws IOException   {
  this.cols = cols;
  this.data = data;
  this.colsName = colsName;
  builTitle();
  writeData();
  wb.write(outputStream);
  outputStream.flush();
 }
 
 /**
  * 构建表格表头
  * @throws IOException
  */
 private void builTitle() throws IOException {
  HSSFCell cell = null;
  HSSFRow row = null;
  int cols = this.colsName.length;
  HSSFCellStyle headStyle = createHeaderStyle(wb);
  row = sheet.createRow(rowIdx);
  row.setHeightInPoints((short) 25);
  for (short i = 0; i < cols; i++) {
   sheet.setColumnWidth(i, (short) 6000);
   cell = row.createCell(i);
   cell.setCellStyle(headStyle);
   // 定义单元格为字符串类型,不设置默认为“常规”
   cell.setCellType(HSSFCell.CELL_TYPE_STRING);
   cell.setCellValue(this.colsName[i]);
  }
 }
 
 private void writeData() throws IOException {
  Object cellValue;
  HSSFCellStyle bodyStyle = createBodyStyle(wb);
  for (int i = 0; i < data.size(); i++) {
   // 创建行
   HSSFRow row = sheet.createRow((short) ++rowIdx);
   for (short j = 0; j < this.cols.length; j++) {
    HSSFCell cell = row.createCell(j);
    // 定义单元格为字符串类型,不设置默认为“常规”
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellStyle(bodyStyle);
    cellValue = data.get(i).get(this.cols[j]);
    cell.setCellValue(cellValue == null ? "" : cellValue.toString());
   }
  }
 }
 
 /**
  * 构建表格列头样式
  * @param wb
  * @return
  */
 private HSSFCellStyle createHeaderStyle(HSSFWorkbook wb) {

  // 设置字体
  HSSFFont font = wb.createFont();
  font.setFontHeightInPoints((short) 12); // 字体高度
  font.setColor(HSSFFont.COLOR_NORMAL); // 字体颜色
  font.setFontName("黑体"); // 字体
  font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度

  // 设置单元格样式
  HSSFCellStyle cellStyle = wb.createCellStyle();
  cellStyle.setFont(font);
  cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平布局:居中
  // 边框
  cellStyle.setBorderTop((short) 1);
  cellStyle.setBorderBottom((short) 1);
  cellStyle.setBorderLeft((short) 1);
  cellStyle.setBorderRight((short) 1);

  cellStyle.setWrapText(true);
  return cellStyle;
  
 }
 
 /**
  * 构建表格列样式
  * @param wb
  * @return
  */
 private HSSFCellStyle createBodyStyle(HSSFWorkbook wb) {

  // 设置单元格样式
  HSSFCellStyle cellStyle = wb.createCellStyle();
  cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 水平布局:居中
  // 边框
  cellStyle.setBorderTop((short) 1);
  cellStyle.setBorderBottom((short) 1);
  cellStyle.setBorderLeft((short) 1);
  cellStyle.setBorderRight((short) 1);

  cellStyle.setWrapText(true);
  return cellStyle;
 }
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值