1. 程式人生 > >Google Chrome遊覽器匯出檔案彈出空白頁並顯示頁面下面

Google Chrome遊覽器匯出檔案彈出空白頁並顯示頁面下面

if (result.success) {
var newTab = window.open('about:blank');

newTab.location.href = sy.contextPath+ '/download?filename='+ result.msg;


/**
* 匯出圖表的資訊
*/
public void createChatExcel() {
List<Map> list1 = getLampRealInfolist();
try {
String path = "/sy/template/LampchartTemplate.xls";
InputStream stream = LampRealInfoAction.class.getResourceAsStream(path);
POIFSFileSystem fs = new POIFSFileSystem(stream);// 得到Excel工作簿物件
HSSFWorkbook wb = new HSSFWorkbook(fs);// 得到Excel工作表物件
HSSFSheet sheet = wb.getSheetAt(0);// 得到Excel工作表的行 根據index取得sheet物件
HSSFCellStyle centerStyle = wb.createCellStyle();// 設定為水平居中
centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
centerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFRow dateRow = sheet.getRow(1);// 得到Excel工作表指定行的單元格 取得有效的行數

String energySaverSeq = getRequest().getParameter("energySaverSeq");
String lampNo=getRequest().getParameter("lampNo");
Integer id= Integer.parseInt(energySaverSeq);
EnergySaverInfo energySaverInfo= energySaverInfoService.getById(id);

for (int i = 0; i < list1.size(); i++) {
HSSFRow row;
row = sheet.createRow(i + 2);
try {
Map info = list1.get(i);


if (info != null) {
row.createCell(0).setCellValue(i + 1);
row.createCell(1).setCellValue(lampNo);
row.createCell(2).setCellValue(
energySaverInfo.getSaverName());
String dataTime =info.get("dataTime").toString();
String datavalue = info.get("datavalue").toString();
row.createCell(5).setCellValue(datavalue);
row.createCell(4).setCellValue(
dataTime.substring(0, 4) + "-"
+ dataTime.substring(4, 6) + "-"
+ dataTime.substring(6, 8) + " "
+ dataTime.substring(8, 10) + ":"
+ dataTime.substring(10, 12) + ":"
+ dataTime.substring(12, 14));


switch (operateType) {
case "input_voltage":
row.createCell(3).setCellValue("輸入電壓");
continue;
case "output_voltage":
row.createCell(3).setCellValue("輸出電壓");
continue;
case "output_current":
row.createCell(3).setCellValue("輸出電流");
continue;
case "kwh":
row.createCell(3).setCellValue("有功功率");
continue;
case "kwhFactor":
row.createCell(3).setCellValue("功率因數");
continue;
case "electric":
row.createCell(3).setCellValue("電量");
continue;
default:
logger.info("名稱錯誤");
}
}


} catch (Exception e) {
logger.error("" , e);
json.setSuccess(false);
json.setMsg(e.getMessage());
continue;
}


}
ServletRequest request = ServletActionContext.getRequest();
@SuppressWarnings("deprecation")
String uploadFilePath = new File(request.getRealPath("/"))
.getPath();
String savePdfPath = uploadFilePath + "/uploadExcel";
File up = new File(savePdfPath);
if (!up.exists()) {
up.mkdirs();
}

String sysdate = IsvCom.getSystemDate(IsvCom.DATE_TYPE3);

FileOutputStream exportFile = new FileOutputStream(savePdfPath + "/" + sysdate + ".xls");
wb.write(exportFile);
exportFile.close();


json.setSuccess(true);
json.setMsg(sysdate + ".xls");
//json.setMsg("路燈圖表_" + operateType + ".xls");
} catch (Exception e) {
json.setSuccess(false);
logger.error("" , e);
json.setMsg(e.toString());
} finally {
writeJson(json);
}
}