java 導出excel (兩種方式:1,保存到服務器中,2.不保存服務器中,直接轉流方式。);第二種方式優先考慮 提示:使用Ajax請求流下載失效,使用表單提交流下載正常,原因未知


/*
* 將數據放入excel表中
*/
//1.創建一個excel對象
HSSFWorkbook excel = new HSSFWorkbook();
//2.樣式
//單元格樣式
HSSFCellStyle cellStyle = excel.createCellStyle();
HSSFCellStyle fontStyle = excel.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//創建一個居中樣式
//字體樣式
HSSFFont font = excel.createFont();
font.setColor(HSSFColor.RED.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
fontStyle.setFont(font);
fontStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//創建一個居中樣式
//2.在excel中添加一個sheet
HSSFSheet sheet = excel.createSheet("幫扶記錄");
//3.在sheet中添加第0行:標題(xx的幫扶記錄情況)
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue(rkxx.getStr("XM")+"的幫扶記錄情況");
cell.setCellStyle(fontStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));//合並7列成一列,放大標題(起始行,截至行,起始列, 截至列)
//4.創建單元格,設置表頭,以及表頭樣式
//創建第二行,開始寫表頭
row = sheet.createRow(1);
//第一列
cell = row.createCell(0);
cell.setCellValue("序號");
cell.setCellStyle(cellStyle);
//第二列
cell = row.createCell(1);
cell.setCellValue("幫扶人員");
cell.setCellStyle(cellStyle);
//第三列
cell = row.createCell(2);
cell.setCellValue("幫扶人聯系電話");
cell.setCellStyle(cellStyle);
//第四列
cell = row.createCell(3);
cell.setCellValue("幫扶時間");
cell.setCellStyle(cellStyle);
//第五列
cell = row.createCell(4);
cell.setCellValue("幫扶內容");
cell.setCellStyle(cellStyle);
//第六列
cell = row.createCell(5);
cell.setCellValue("幫扶結果");
cell.setCellStyle(cellStyle);
//第七列
cell = row.createCell(6);
cell.setCellValue("幫扶單位");
cell.setCellStyle(cellStyle);

//5.寫入數據
for (int i = 0; i < bfjl.size(); i++) {
//在excel中新增一行
row = sheet.createRow(i + 2);//標題占一行,表頭已占一行,故從第三行開始添加數據
//在新增的一行中,依次創建單元格放入上面定義的列值
row.createCell(0).setCellValue(i+1);//序號
row.createCell(1).setCellValue(bfjl.get(i).getStr("BFRY"));//幫扶人員
row.createCell(2).setCellValue(bfjl.get(i).getStr("BFRLXDH"));//幫扶人聯系電話
row.createCell(3).setCellValue(bfjl.get(i).getTimestamp("BFSJ").toString());//幫扶時間
row.createCell(4).setCellValue(bfjl.get(i).getStr("BFNR"));//幫扶內容
row.createCell(5).setCellValue(bfjl.get(i).getStr("BFJG"));//幫扶結果
row.createCell(6).setCellValue(bfjl.get(i).getStr("BFDW"));//幫扶單位
}
//6.將其生成一個excel文件,輸出
//String name = PathKit.getWebRootPath()+"/download/"+rkxx.getStr("xm")+".xls";
String name =rkxx.getStr("xm")+".xls";
try {
//在本地生成一個excel文件,在傳
/*File file = new File(name);
FileOutputStream file1 = new FileOutputStream(file);
excel.write(file1);
file1.close();*/

       String downFileName = new String("登陸日志.xls");  

        try {  

      //若不進行編碼在IE下會亂碼  

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

       } catch (UnsupportedEncodingException e) {  

            e.printStackTrace();  

        }


//直接獲取輸出,直接輸出excel(優先使用)
OutputStream output=response.getOutputStream();
   response.reset();
   response.setHeader("Content-disposition", "attachment; downFileName ="+ URLEncoder.encode(name, "utf-8"));
   response.setContentType("application/msexcel");        
   excel.write(output);
   output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM