java导_java 导出

//导出

@RequestMapping("/excelExport")

@ResponseBodypublic void excelExport(String params, HttpServletRequest

request , HttpServletResponse response,String projectCode

,String projectName,String engineeringName,Integer status,String creatorName,String creatororgname,String createTime,String providername) throws Exception{

BufferedInputStreamin = null;

BufferedOutputStreamout = null;

SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟//path是指欲下载的文件的路径。

/*File file = new File(path);*/

//创建excel

HSSFWorkbook wb = newHSSFWorkbook();

String fileName= "科目信息表.xls";//导出时下载的EXCEL文件名//创建sheet

HSSFSheet sheet = wb.createSheet("科目信息");

sheet.setDefaultColumnWidth(10);

sheet.setDefaultRowHeightInPoints(20);//创建一行

HSSFRow rowTitle = sheet.createRow(0);

rowTitle.setHeightInPoints(20);

HSSFCellStyle styleTitle=wb.createCellStyle();

styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中

HSSFCellStyle styleCenter=wb.createCellStyle();

styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中

styleCenter.setWrapText(true); //设置为自动换行//在行上创建1列

HSSFCell cellTitle = rowTitle.createCell(0);//列标题及样式

cellTitle.setCellValue("科目ID");

cellTitle.setCellStyle(styleTitle);//在行上创建2列

cellTitle = rowTitle.createCell(1);

cellTitle.setCellValue("科目名称");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(2);

cellTitle.setCellValue("上年实际");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(3);

cellTitle.setCellValue("本年预算");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(4);

cellTitle.setCellValue("1月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(5);

cellTitle.setCellValue("2月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(6);

cellTitle.setCellValue("3月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(7);

cellTitle.setCellValue("4月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(8);

cellTitle.setCellValue("5月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(9);

cellTitle.setCellValue("6月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(10);

cellTitle.setCellValue("7月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(11);

cellTitle.setCellValue("8月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(12);

cellTitle.setCellValue("9月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(13);

cellTitle.setCellValue("10月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(14);

cellTitle.setCellValue("11月");

cellTitle.setCellStyle(styleTitle);

cellTitle= rowTitle.createCell(15);

cellTitle.setCellValue("12月");

cellTitle.setCellStyle(styleTitle);

List budgetDetails=budgetDetailService.ExcelInfo();for(int i=0;i

BudgetDetail budgetDetail= budgetDetails.get(i);

HSSFRow row= sheet.createRow(i + 1);

row.setHeightInPoints(20);

HSSFCell cell= row.createCell(0);

cell.setCellValue(budgetDetail.getSubid());//科目ID

cell.setCellStyle(styleCenter);

cell= row.createCell(1);

cell.setCellValue(budgetDetail.getSubname());//科目名称

cell.setCellStyle(styleCenter);

}

String path="c:/aaaa.xls";

String path2="D:/aaaa.xls";

FileOutputStream fout= null;try{

fout= newFileOutputStream(path);

}catch(Exception e){

fout= newFileOutputStream(path2);

}

wb.write(fout);

fout.close();

wb.close();try{try{

InputStream inputStream= newFileInputStream(ResourceUtils.getFile(path));

fileName= URLEncoder.encode(fileName, "UTF-8");

response.setContentType("applicationnd/octet-stream");

response.setCharacterEncoding("UTF-8");

response.setHeader("Content-Disposition", "attachment; filename="+fileName);in = newBufferedInputStream(inputStream);out = newBufferedOutputStream(response.getOutputStream());

}catch(Exception e){

InputStream inputStream= newFileInputStream(ResourceUtils.getFile(path2));

fileName= URLEncoder.encode(fileName, "UTF-8");

response.setContentType("applicationnd/octet-stream");

response.setCharacterEncoding("UTF-8");

response.setHeader("Content-Disposition", "attachment; filename="+fileName);in = newBufferedInputStream(inputStream);out = newBufferedOutputStream(response.getOutputStream());

}byte[] data = new byte[1024];int len = 0;while (-1 != (len=in.read(data, 0, data.length))) {out.write(data, 0, len);

}

}catch(Exception e) {

e.printStackTrace();

}finally{if (in != null) {in.close();

}if (out != null) {out.close();

}

File f= newFile(path);if(f.exists()){

f.delete();//删除

}

File f1=newFile(path2);if(f1.exists()){

f1.delete();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值