VENAS excel的出力的過程總結。(有模板出力)
阿新 • • 發佈:2018-11-27
需求:將頁面jqGrid內容輸出到Excel
1.獲取jqGrid內容。
1.1 var postData = $("#gridName").getGridParam('data'); //得到以行為單位的陣列。
var postData = $("#gridSimeiteisiGyosyaInfo").getGridParam('data');
1.2 var JSONData= JSON.stringify(postData); //將從頁面得到的陣列轉化為JSON格式。
var gyosyaInfo = JSON.stringify(postData);
var gyosyaInfo = JSON.stringify(postData);
1.3 新增引數。如果除了jqGrid內容外,還有別的需要傳遞到後臺的引數,如下傳遞。
postData = { "value(gyosyaInfo)" : gyosyaInfo, "value(sinnseiNendo)" : sinnseiNendo, "value(gyoumuKbn)" : gyoumuKbn, "value(areaKbn)" : areaKbn, "value(gyousyuKbn)" : gyousyuKbn, };
1.4 js完整程式碼。
function outputExcel() { // 申請年度 var sinnseiNendo = $("#sinnseiNendo").find("option:selected").text() // 業務區分 var rowId = $("#gridSimeiteisiGyoumu").jqGrid('getGridParam', 'selrow'); var gyoumuKbn = $("#gridSimeiteisiGyoumu").jqGrid('getCell', rowId, 0); if (isEmpty(gyoumuKbn)) { sofia.ui.showDialogJsSetParams("W", "E", "0", "業務區分を選択してください。"); return false; } // 地域區分 var rowId = $("#gridSimeiteisiArea").jqGrid('getGridParam', 'selrow'); var areaKbn = $("#gridSimeiteisiArea").jqGrid('getCell', rowId, 0); if (isEmpty(areaKbn)) { sofia.ui.showDialogJsSetParams("W", "E", "0", "地域區分を選択してください。"); return false; } // 業種區分 var rowId = $("#gridSimeiteisiGyousyu").jqGrid('getGridParam', 'selrow'); var gyousyuKbn = $("#gridSimeiteisiGyousyu").jqGrid('getCell', rowId, 0); if (isEmpty(gyousyuKbn)) { sofia.ui.showDialogJsSetParams("W", "E", "0", "業種區分を選択してください。"); return false; } var postData = $("#gridSimeiteisiGyosyaInfo").getGridParam('data'); var gyosyaInfo = JSON.stringify(postData); gyosyaInfo["sinnseiNendo"] = sinnseiNendo; postData = { "value(gyosyaInfo)" : gyosyaInfo, "value(sinnseiNendo)" : sinnseiNendo, "value(gyoumuKbn)" : gyoumuKbn, "value(areaKbn)" : areaKbn, "value(gyousyuKbn)" : gyousyuKbn, }; $.ajax( { url : sofia.contextPath() + '/keiyaku/wfgs2000/wfgs2010/exportExcel.do', dataType : 'json', data : postData, }).done(function(data) { if (data.fileName) { var action = "fileName=" + data.fileName + "&sysCodeId=W"; sofia.ui.downloadExcelDialog(action); } }); }
2.傳遞引數。(.xml層)
<action path="/keiyaku/wfgs2000/wfgs2010/exportExcel" name="ajaxForm" scope="request" validate="false" extends="//AuthCommandAction">
<set-property key="copyBeanToForm" value="false" />
<set-property key="facadeInterface" value="jp.co.bsnnet.sofia.service.keiyaku.wfgs2000.Wfgs2010Facade" />
<set-property key="method" value="exportExcel" />
<set-property key="ajax" value="true" />
</action>
3.facade && facadeImp
void exportExcel(AjaxValuesBean param, AjaxOutputStreamWrapper output) throws Exception;
public void exportExcel(AjaxValuesBean param, AjaxOutputStreamWrapper output) throws Exception {
try {
Integer shozokuNo = FacadeUtils.getUser().getShozokuNo();
Map ajaxParams = param.getValues(); //得到前臺傳遞的資料,存在map中
//map中取得資料
Object gyosyaInfo = ajaxParams.get("gyosyaInfo");
Object sinnseiNendo = ajaxParams.get("sinnseiNendo");
//轉為string型別
String gyosyaInfoData = gyosyaInfo.toString();
String sinnseiNendoData = sinnseiNendo.toString();
//賦值給dto
Wfgs2010Dto wfgs2010Dto = new Wfgs2010Dto();
wfgs2010Dto.setGyosyaInfoDataList(gyosyaInfoData);
wfgs2010Dto.setSinnseiNendo(sinnseiNendoData);
//呼叫domain層
String fileName = wfgs2010Domain.exportExcel(wfgs2010Dto, shozokuNo);
JSONObject json = new JSONObject();
json.put("error", "0");
json.put("fileName", fileName);
json.put("shozokuNo", shozokuNo);
FacadeUtils.writeJson(json, output);
} catch (Exception e) {
// Excel出力エラー
throw new FwApplicationException(new FwMessage("H.E.13"), e);
}
}
4.Domain && DomianImp
String exportExcel(Wfgs2010Dto wfgs2010Dto,Integer shozokuNo) throws Exception;
public String exportExcel(Wfgs2010Dto wfgs2010Dto,Integer shozokuNo) throws Exception {
Date sysDate = this.keiyakuDomain.getSysDate();
String sysDateStr= this.getDateStr(sysDate);
wfgs2010Dto.setSysDate(sysDate);
// VenasPrintDto 列印excel用的dto(必須設定)
VenasPrintDto printDto = new VenasPrintDto();
printDto.setShozokuNo(shozokuNo); //所屬番號
printDto.setOutputType(Constants.OUTPUT_TYPE_Excel); //列印型別(excel,PDF)
printDto.setOutputFileName(OUTPUT_FILENAME); //最終出力時name
printDto.setTemplateFileName(TEMPLATE_FILENAME); //中間變數name
printDto.setSysCodeId(SYSKBN_KEIYAKU); //頁面機能縮寫
venasPrintDomain.loadSettings(printDto);
/*
*venasPrintDomain.createExcelBookByTempFile(printDto)
*該方法內部會根據在指定目錄寫存放的excel模板,複製改模板,然後新建一個新模板。
*模板存放目錄:c:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\sofinam_venas\report_keiyaku\sheet
*輸出檔案存放目錄:c:\Program Files\Apache Software Foundation\Tomcat *6.0\webapps\sofinam_venas\report_keiyaku\krpt_out
*指定目錄記錄在資料庫cctl_path表中。
*所以必須有指定目錄資料夾。並在模板目錄下存放模板檔案才能成功建立新的資料夾。
*/
ExcelWorkBook xlBook = venasPrintDomain.createExcelBookByTempFile(printDto);
ExcelSheet xlSheet = xlBook.item(xlBook.getWrkBook().getSheetName(0));
//頁面資料 轉換為json型別
String gyosyaInfoDataList = wfgs2010Dto.getGyosyaInfoDataList();
JSONAware jsonValue = parseJson(gyosyaInfoDataList);
JSONArray jsonArray = (JSONArray)jsonValue;
//單獨cell賦值,給(1,1)的單元格賦值("検索年度 : "+wfgs2010Dto.getSinnseiNendo())
xlSheet.setCellVal(1, 1,"検索年度 : "+wfgs2010Dto.getSinnseiNendo());
int iRow = 7; //迴圈開始行
int iCol = 1; //迴圈開始列
//迴圈賦值,頁面grid
for ( int i = 0; i < jsonArray.size(); i++) {
JSONObject json = (JSONObject) jsonArray.get(i);
xlSheet.setCellVal(iRow, iCol++, json.get("simei").toString());
iRow++;
}
//模板設定背景色為白色,設定grid邊框。
int borderFirstRow = 7; // 一覧に罫線を引く先頭行;
int borderLastCol = 13; // 一覧に罫線を引く最終列;
// 設定excel樣式。
CellRangeAddress rng = new CellRangeAddress(borderFirstRow, iRow - 1, 1, borderLastCol);
xlSheet.setRngBorderTop(rng, CellStyle.BORDER_THIN);
xlSheet.setRngBorderBottom(rng, CellStyle.BORDER_THIN);
xlSheet.setRngBorderLeft(rng, CellStyle.BORDER_THIN);
xlSheet.setRngBorderRight(rng, CellStyle.BORDER_THIN);
xlSheet.setRngBorderHorizn(rng, CellStyle.BORDER_THIN);
xlSheet.setRngBorderVrtical(rng, CellStyle.BORDER_THIN);
CellStyle style = xlBook.createCellStyle();
style.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
// 儲存
xlBook.save(printDto.getOutputFile().getAbsolutePath());
return printDto.getOutputFile().getName();
}
public JSONAware parseJson(Object value) throws org.json.simple.parser.ParseException {
JSONParser parser = new JSONParser();
return (JSONAware) parser.parse(value.toString());
}