publicvoidexport_info{
//查询数据
ListlistallbatchSsi=Db.use(DictKeys.db_dataSourcetwo_main)
.find("selectfi.batchidsfromfiltratesucceed");
//数据个数
intlength=listallbatchSsi.size();
//用于海量数据Excel导出类
SXSSFWorkbookwb=newSXSSFWorkbook();
Sheetsheet=wb.createSheet("导出接触和声纹模板");
Stringfilename="导出数据"+".xlsx";
Cellcell;
//得到Excel工作表的行
Rowrow;
//常用单元格边框格式
//设置字体和内容位置
Fontf=wb.createFont();
//设置字号大小
f.setFontHeightInPoints((short)12);
//设置自定义颜色
f.setColor(HSSFColor.BLACK.index);
CellStylestyle=wb.createCellStyle();
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
sheet.setColumnWidth(0,5000);
sheet.setColumnWidth(1,5500);
sheet.setColumnWidth(2,5500);
intsheetId=1;
introwIndex=1;
//创建Excel工作表的行
row=sheet.createRow(0);
cell=row.createCell(0);
cell.setCellValue("ID");
cell.setCellStyle(style);
DataValidationHelperhelper=sheet.getDataValidationHelper();
//CellRangeAddressList(firstRow,lastRow,firstCol,lastCol)设置行列范围
//length为数据库查询出的个数
CellRangeAddressListaddressList=newCellRangeAddressList(1,length,1,1);
//如果带双引号超过30个,打开excel的时候就会提示错误而且下拉框不生效,
//如果不带双引号就没有问题(测试心得)
//设置下拉框数据
String[]list={"无效人名,方言障碍,作废,其它"};
DataValidationConstraintconstraint=helper.createExplicitListConstraint(list);
DataValidationdataValidation=helper.createValidation(constraint,addressList);
//处理Excel兼容性问题
if(dataValidationinstanceofXSSFDataValidation){
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
}else{
dataValidation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(dataValidation);
//循环查询出的数据并写人excel中
for(intj=0;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++;
}
Filefile=newFile(PathKit.getWebRootPath()+"/TouchVocal/");
if(!file.isDirectory()){
file.mkdir();
}
FileOutputStreamfout;
try{
fout=newFileOutputStream(file+"/"+filename);
wb.write(fout);
fout.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}