1. 程式人生 > >springmvc匯出excel彈出框,前端用ajax請求

springmvc匯出excel彈出框,前端用ajax請求

1、js頁面:

$(function(){
$("#btnWater").click(function(){
var machineID=$("#txtMachine").val();
var proVersion=$("#txtVersion").val();
var jsonvalue={machineID:machineID,proVersion:proVersion};
if(machineID!="")
{
location.href='/FiledTranslate/ExcelOut.do?machineID='+machineID+"&proVersion="+proVersion;


}
else
{
alert('機型不能為空');
}
});
});

2、jsp頁面:

<body>
機型ID:
<input type="text" id="txtMachine">
協議版本:
<input type="text" id="txtVersion">
<input type="button" id="btnWater" value="Excel匯出" class="btn">
</body>

3、controller程式碼:

@RequestMapping("/ExcelOut.do")
private void ExcelOut(HttpServletRequest request,HttpServletResponse response)
{
String machine=request.getParameter("machineID");
String proVersion=request.getParameter("proVersion");
JSONObject json=translateService.getMachine_Name("machine_name",machine,proVersion);
ManageExcel(json, machine,response);
return ;
}

  // 寫excel

public void ManageExcel(JSONObject jsonObject,String machineID,HttpServletResponse response)

{
ExcelOperator eOperator=new ExcelOperator();
// 建立Excel的工作書冊 Workbook,對應到一個excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 建立Excel的工作sheet,對應到一個excel文件的tab
HSSFSheet sheet = wb.createSheet("sheet1");


// 設定excel每列寬度,給工作表列定義列寬(實際應用自己更改列數)   
for (int i = 0; i <=3; i++) {   
sheet.setColumnWidth(i, 7000);   
}  
// 建立單元格樣式
HSSFCellStyle style = wb.createCellStyle();
// 建立字型樣式
HSSFFont font = wb.createFont();
// 建立Excel的sheet的一行
HSSFRow row = sheet.createRow(0);
row.setHeight((short) 500);// 設定行的高度
// 建立一個Excel的單元格
HSSFCell cell = row.createCell(0);
// 合併單元格(startRow,endRow,startColumn,endColumn)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
// 給Excel的單元格設定樣式和賦值
cell.setCellValue("執行資料資訊");
cell.setCellStyle(eOperator.getCellStyle(style,font,"T"));
//設定列頭   
HSSFRow row2 = sheet.createRow(1); 
cell= row2.createCell(0);   
cell.setCellStyle(eOperator.getCellStyle(style,font,"H"));   
cell.setCellValue(new HSSFRichTextString("中文名稱"));   


cell = row2.createCell(1);   
cell.setCellStyle(eOperator.getCellStyle(style,font,"H"));   
cell.setCellValue(new HSSFRichTextString("hive的欄位型別"));   


cell= row2.createCell(2);   
cell.setCellStyle(eOperator.getCellStyle(style,font,"H"));   
cell.setCellValue(new HSSFRichTextString("英文名稱"));   


cell= row2.createCell(3);   
cell.setCellStyle(eOperator.getCellStyle(style,font,"H"));   
cell.setCellValue(new HSSFRichTextString("mongodb的欄位型別")); 


int i=2;
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()){
String key = keys.next();
String value = jsonObject.get(key).toString();
if(key.equals("type_id")||key.equals("_id")||key.equals("prot_ver"))  
{
}else{
row = sheet.createRow(i);
cell = row.createCell(0);
cell.setCellValue(key);


cell = row.createCell(1);
String type=value.substring(0, 1);
cell.setCellValue(eOperator.rtnFieldType(type));


cell = row.createCell(2);
String str=translateService.getEnglishInfo(key);
cell.setCellValue(str);


cell = row.createCell(3);
cell.setCellValue(value);
i++;
}
}
//js呼叫是彈出現在視窗
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
String fileName = machineID;
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 設定response引數,可以開啟下載頁面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}