java 简单实现Excel导出功能
2019-01-12 本文已影响0人
小岛wink
点击下载按钮后,js:
//下载表格
function downLoadTable(){
var year = $("#selectYear").val();
location.href="${ctx}/specialFound/downLoadTable?year="+year;
}
类:
@RequestMapping(value="/downLoadTable",method=RequestMethod.GET)
@ResponseBody
public void downLoadTable(HttpServletRequest request, HttpServletResponse response) throws IOException{
ProjectBudgetInfoMVO projectBudgetInfo = new ProjectBudgetInfoMVO();
String year = request.getParameter("year");//年份
List<ProjectBudgetInfoMVO> userList = new ArrayList<ProjectBudgetInfoMVO>();
projectBudgetInfo.setReportYear(year);
CacheManager cacheManager = CacheManager.getInstance();
String yearName = (String) cacheManager.get("table.cache.idvalue.status", "PROJECT_BUDGET_INFO:REPORT_YEAR."+year);
try {
userList = projectBudgetInfoDelegate.queryList(projectBudgetInfo);
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel");
OutputStream out = response.getOutputStream();
response.setHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(yearName+"项目运算", "utf-8") + ".xls");
Workbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("项目预算");
//设置单元格样式
XSSFCellStyle hssfCellStyle = (XSSFCellStyle) workbook.createCellStyle();
hssfCellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//居中显示
hssfCellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//纵向居中
Font font = workbook.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 16);//设置字体大小
int userCount = userList.size();
CellRangeAddress region1 = new CellRangeAddress(0,0,0,2 );
sheet1.addMergedRegion(region1);
//创建行
Row row1 = sheet1.createRow(0);//标题
Cell cell1 = row1.createCell(0);
cell1.setCellValue(year+"项目预算");
//创建行
Row row = sheet1.createRow(1);
//创建单元格
Cell cell = row.createCell(0);
cell.setCellValue("资金名称");//设置第一行第一格的值
cell = row.createCell(1);
cell.setCellValue("项目名称");
cell = row.createCell(2);
cell.setCellValue("预算金额");
cell = row.createCell(3);
cell.setCellValue("处室");
cell = row.createCell(4);
cell.setCellValue("项目细化");
cell = row.createCell(5);
cell.setCellValue("备注");
cell.setCellStyle(hssfCellStyle);//设置单元格的文本居中显示
for(int i = 0;i<userCount;i++){
//内容
Row rows = sheet1.createRow(i+2);
Cell cells = rows.createCell(0);
cells.setCellValue(userList.get(i).getCapitalNameText());//设置第一行第一格的值
cells = rows.createCell(1);
cells.setCellValue(userList.get(i).getProjectNameText());
cells = rows.createCell(2);
cells.setCellValue(userList.get(i).getBudgetAmount());
cells = rows.createCell(3);
cells.setCellValue(userList.get(i).getBusinessOfficeText());
cells = rows.createCell(4);
cells.setCellValue(userList.get(i).getProjectRefinement());
cells = rows.createCell(5);
cells.setCellValue(userList.get(i).getRemarks());
cells.setCellStyle(hssfCellStyle);
}
workbook.write(out);
out.flush();
out.close();
} catch (SysException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AppException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}