需求:将页面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());
}