springMVC框架網頁下載功能實現
阿新 • • 發佈:2019-01-24
List<Map<String,Object>> list=selectData()
;//到資料庫中查詢需要匯出的資訊
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("匯出資訊");//excel sheet標題
HSSFRow row = sheet.createRow((int) 0);//建立匯出的行數(以0開始)
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式 HSSFCell cell = row.createCell((short) 0);//建立第一列
cell.setCellValue("資料1");//第一列列名
cell.setCellStyle(style);//第一列樣式(居中) cell = row.createCell((short) 1);//建立第二列 cell.setCellValue("資料2");//第二列列名
cell.setCellStyle(style);//第二列樣式(居中)
for(int i=0;i<list.size();i++){ //給列賦值
row = sheet.createRow( i + 1);//迴圈建立 Map<String,Object> map1=list.get(i); row.createCell((short) 0).setCellValue(map1.get(0).toString());//給列賦值 row.createCell((short) 1).setCellValue(map1.get(1).toString()); }
try{
FileOutputStream out=new FileOutputStream("下載路徑");
wb.write(out); out.close();
load(request,response,session);//網頁下載
}catch(Exception e){e.printStackTrace}