List list = this.getDateList(); //获取数据list。 之后转换成xls
String voucherno = "";
if(list.size()<=0){
}
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
sheet.setColumnWidth((short) 0, (short) (256*15));
sheet.setColumnWidth((short) 1, (short) (256*15));
sheet.setColumnWidth((short) 2, (short) (256*15));//标题栏
HSSFRow row = sheet.createRow(( short ) 0 );
String [] arr = {"单据号","财务凭证号","金额"};
for(int i=0;i<arr.length;i++){
setCell(row,i,arr[i]);
}
//内容
for(int i =0;i<list.size();i++){
AccInputTaxTurnOutHisDto schema = (AccInputTaxTurnOutHisDto)list.get(i);
row = sheet.createRow(( short ) i+1 );
int index = 0;
voucherno = schema.getVoucherNo();
setCell(row,index++,schema.getCertiNo());
setCell(row,index++,schema.getRemark1());
setCellDouble(row,index++,schema.getTurnOutFee());
}
try {
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
String filename = URLEncoder.encode("转出凭证号" + voucherno+"进项转出清单.xls", "UTF-8"); //确保文件名不会中文乱码,这个很关键
response.setHeader("Content-disposition", "attachment;filename="+filename);
OutputStream fos = response.getOutputStream();
wb.write(fos);
fos.flush(); //通过respose 向页面输出,页面会得到下载的提示
fos.close();
} catch (Exception e) {
System.err.println("生成xls文件出错"+e);
}
private void setCell(HSSFRow row,int place,String value){
HSSFCell cell = row.createCell(( short ) place );
cell.setEncoding(HSSFCell.ENCODING_UTF_16); //确保单元格支持中文
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(value);
}
private void setCellDouble(HSSFRow row,int place,double value){
HSSFCell cell = row.createCell(( short ) place );
cell.setEncoding(HSSFCell.ENCODING_UTF_16); //确保单元格支持中文
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(value);
}