/**
* 下载
*
* @return
*/
@ApiOperation(value = “下载”, response = Result.class)
@RequestMapping(value = “/exoprtAllList.do”, method = RequestMethod.GET)
public Result exoprtAllAdjustList(HttpServletResponse response) throws IOException {
try {
service.exoprtAllList( response);
} catch (Exception e) {
// TODO Auto-generated catch block
log.error(“下载出差啦。。” + e.getMessage(), e);
}
return ResultUtil.success();
}
@Override
public void exoprtAllList(HttpServletResponse response) {
List<AssetConversionDownloadExcel> list = baseMapper.queryDownloadExcelsList();
writeExcel(response, list);
}
private void writeExcel(HttpServletResponse response, List list) {
//File newFile = new File(GlobalConstant.ZGSQ_FILE_PATH);
File newFile = new File(“D:\工作笔记\2003\资产转固\固定资产增置单5.xlsx”);
//新文件写入数据,并下载*****************************************************
InputStream is = null;
XSSFWorkbook xSSFWorkbook=null;
XSSFSheet xSSFSheet=null;
try {
is = new FileInputStream(newFile);
xSSFWorkbook = new XSSFWorkbook(is);
//获取第一个sheet
xSSFSheet = xSSFWorkbook.getSheetAt(0);
} catch (Exception e1) {
e1.printStackTrace();
}
if(xSSFSheet != null){
try {
//写数据
FileOutputStream fos = new FileOutputStream(newFile);
//构建每行的数据内容
int rowNum = 1;
for (AssetConversionDownloadExcel data : list) {
if (data == null) {
continue;
}
//输出行数据
Row row = xSSFSheet.createRow(rowNum++);
int cellNum = 0;
Cell cell;
// 序号
cell = row.createCell(cellNum++);
cell.setCellValue(cellNum);
// 项目编号
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getProjectNo() ? "" : data.getProjectNo());
// 项目名称
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getProjectNm() ? "" : data.getProjectNm());
// 所在金额
cell = row.createCell(cellNum++);
cell.setCellValue(null == data.getProjectAmount() ? null : data.getProjectAmount());
}
xSSFWorkbook.write(fos);
fos.flush();
fos.close();
//下载
InputStream fis = new BufferedInputStream(new FileInputStream(newFile));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setContentType("text/html;charset=UTF-8");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/x-msdownload");
String newName = URLEncoder.encode("固定资产增置单"+System.currentTimeMillis()+".xlsx", "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=\""+ newName + "\"");
response.addHeader("Content-Length", "" + newFile.length());
toClient.write(buffer);
toClient.flush();
}
catch(Exception e) {
e.printStackTrace();
}finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}