SXSSFWorkbook导出Excel并带下拉菜单

public void export_info{  
  //查询数据
  List<Record> listallbatchSsi=Db.use(DictKeys.db_dataSourcetwo_main)
  .find("select fi.batchids from filtratesucceed" );
  //数据个数
  int length=listallbatchSsi.size();
   //用于海量数据Excel导出类
  SXSSFWorkbook wb=new SXSSFWorkbook();
  Sheet sheet = wb.createSheet("导出接触和声纹模板");
  String filename = "导出数据"+".xlsx"; 
  Cell cell;
  //得到Excel工作表的行
  Row row;
  // 常用单元格边框格式 
  // 设置字体和内容位置
  Font f = wb.createFont();
  // 设置字号大小
  f.setFontHeightInPoints((short) 12);
  // 设置自定义颜色
  f.setColor(HSSFColor.BLACK.index);  
  CellStyle style = wb.createCellStyle();
  style.setFont(f);
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
  sheet.setColumnWidth(0, 5000);
  sheet.setColumnWidth(1, 5500); 
  sheet.setColumnWidth(2, 5500); 
  int sheetId = 1;
  int rowIndex = 1;  
  //创建Excel工作表的行
  row = sheet.createRow(0);
  cell = row.createCell(0);
  cell.setCellValue("ID");
  cell.setCellStyle(style);
  DataValidationHelper helper = sheet.getDataValidationHelper(); 
  //CellRangeAddressList(firstRow,lastRow,firstCol,lastCol)设置行列范围   
  //length为数据库查询出的个数
  CellRangeAddressList addressList = new CellRangeAddressList(1, length, 1, 1);
  //如果带双引号超过30个, 打开excel的时候就会提示错误 而且下拉框不生效,
  //如果不带双引号就没有问题(测试心得) 
  //设置下拉框数据 
  String[] list={"无效人名,方言障碍,作废,其它"};    
  DataValidationConstraint constraint = helper.createExplicitListConstraint(list);  
  DataValidation dataValidation = helper.createValidation(constraint, addressList); 
  //处理Excel兼容性问题  
  if(dataValidation instanceof XSSFDataValidation){  
       dataValidation.setSuppressDropDownArrow(true);  
       dataValidation.setShowErrorBox(true);  
  }else{  
      dataValidation.setSuppressDropDownArrow(false);  
  }   
  sheet.addValidationData(dataValidation);  
  //循环查询出的数据并写人excel中
  for (int j = 0; j < length; j++) { 
       row = sheet.createRow(rowIndex);  
       if (listallbatchSsi.get(j).getStr("batchids") == null){
          row.createCell(0).setCellValue("无");
       }else{
          row.createCell(0).setCellValue(listallbatchSsi.get(j).getStr("batchids"));
       } 
    rowIndex++;
  }  
  File file = new File(PathKit.getWebRootPath() + "/TouchVocal/");
  if (!file.isDirectory()) {
    file.mkdir();
  }
  FileOutputStream fout;
  try {
       fout = new FileOutputStream(file + "/" + filename);
       wb.write(fout);
       fout.close();
  } catch (FileNotFoundException e) {
       e.printStackTrace();
  } catch (IOException e) {
       e.printStackTrace();
  }  
}

 

转载于:https://my.oschina.net/sxxachao/blog/607322

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值