PG:創(chuàng)建一個(gè)類型為button的按鈕,設(shè)置按鈕的Action Type為firePartialAction和Event為exportexcel(按鈕類型不用 submitButton 的原因是 submitButton類型的按鈕,在導(dǎo)出后,再往頁面添加行或者保存時(shí)會(huì)提示報(bào)瀏覽器后退的異常 )。
專注于為中小企業(yè)提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)金臺(tái)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
CO:processFormRequest代碼:
if ("exportexcel".equals(pageContext.getParameter(EVENT_PARAM))){
byte abtye0[] = (byte[])am.invokeMethod("export");
try {
//獲取DataObject
DataObject dataObject = pageContext.getNamedDataObject("_SessionParameters");
//根據(jù)DataObject獲取response
HttpServletResponse httpservletresponse = (HttpServletResponse)dataObject.selectValue(null, "HttpServletResponse");
download(pageContext, httpservletresponse, abtye0);
} catch (Exception e) {
e.printStackTrace();
throw OAException.wrapperException(e);
}
}
download方法代碼:
public void download(OAPageContext pageContext,
HttpServletResponse response, byte[] abyte0) {
String fileName = "export";
//fileName = pageContext.getMessage("CUX", "CUX_SRM_IMPORT_FILE_NAME", null);
try {
String charset =
pageContext.getProfile("ICX_CLIENT_IANA_ENCODING");
//設(shè)置文件的格式 字符集
response.setContentType("application/vnd.ms-excel;charset=" +
charset); //gb2312
//設(shè)置文件大小
response.setContentLength(abyte0.length);
//throw new OAException("abyte0.length:"+abyte0.length,OAException.ERROR);
//通知瀏覽器文件的名字
response.setHeader("Content-Disposition",
"attachment;filename=" + fileName + ".xls");
//獲取輸出流
OutputStream toClient = response.getOutputStream();
//將字符數(shù)組寫進(jìn)輸出流
toClient.write(abyte0);
//強(qiáng)制刷新(頁面彈出下載框)
toClient.flush();
//關(guān)閉輸出流
toClient.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} //end download()
AM,export代碼:
public byte[] export() {
byte abyte0[] = null;
CuxAslVOImpl vo = getCuxAslVO1();
CuxAslVORowImpl hRow = null;
int rowcount = vo.getRowCount(); //取當(dāng)前提取的記錄集的記錄數(shù)
if (rowcount == 0)
throw new OAException("沒有需要導(dǎo)出的數(shù)據(jù)", OAException.ERROR);
RowSetIterator deleteIter =
vo.createRowSetIterator("deleteIter"); //建立記錄集的指示符
deleteIter.setRangeStart(0); //設(shè)置循環(huán)起點(diǎn),相當(dāng)于移動(dòng)指針到第一條記錄
deleteIter.setRangeSize(rowcount); //設(shè)置循環(huán)次數(shù)
//第一步,創(chuàng)建一個(gè)webbook,對(duì)應(yīng)一個(gè)Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
//第二步,在webbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("export");
//第三步,在sheet中添加表頭第0行,注意老版本poi對(duì)應(yīng)Excel的行數(shù)列數(shù)有限制short
HSSFRow row = sheet.createRow((int)0);
//第四步,創(chuàng)建單元格,并設(shè)置值表頭 設(shè)置表頭居中
CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER); //創(chuàng)建一個(gè)劇中格式
//CSV
StringBuffer buffer =
new StringBuffer("庫存組織名稱,物料編碼,物料描述,供應(yīng)商編碼,供應(yīng)商名稱,供應(yīng)商地點(diǎn),狀態(tài),是否禁用,結(jié)算方式\r\n");
//Excel
HSSFCell cell = row.createCell((short)0);
cell.setCellValue("庫存組織名稱");
cell.setCellStyle(style);
cell = row.createCell((short)1);
cell.setCellValue("物料編碼");
cell.setCellStyle(style);
cell = row.createCell((short)2);
cell.setCellValue("物料描述");
cell.setCellStyle(style);
cell = row.createCell((short)3);
cell.setCellValue("供應(yīng)商編碼");
cell.setCellStyle(style);
cell = row.createCell((short)4);
cell.setCellValue("供應(yīng)商名稱");
cell.setCellStyle(style);
cell = row.createCell((short)5);
cell.setCellValue("供應(yīng)商地點(diǎn)");
cell.setCellStyle(style);
cell = row.createCell((short)6);
cell.setCellValue("狀態(tài)");
cell.setCellStyle(style);
cell = row.createCell((short)7);
cell.setCellValue("是否禁用");
cell.setCellStyle(style);
cell = row.createCell((short)8);
cell.setCellValue("結(jié)算方式");
cell.setCellStyle(style);
for (int i = 0; i < rowcount; i++) {
row = sheet.createRow((int)i + 1);
hRow = (CuxAslVORowImpl)deleteIter.getRowAtRangeIndex(i); //取得當(dāng)前記錄
//第五步,創(chuàng)建單元格,并設(shè)置值
row.createCell((short)0).setCellValue(hRow.getOrganizationName());
row.createCell((short)1).setCellValue(hRow.getItemNumber());
row.createCell((short)2).setCellValue(hRow.getItemDesc());
row.createCell((short)3).setCellValue(hRow.getVendorNumber());
row.createCell((short)4).setCellValue(hRow.getVendorName());
row.createCell((short)5).setCellValue(hRow.getVendorSiteCode());
row.createCell((short)6).setCellValue(hRow.getAslStatus());
row.createCell((short)7).setCellValue(hRow.getDisableFlag());
row.createCell((short)8).setCellValue(hRow.getPoSettlementMethod());
buffer.append(hRow.getOrganizationName() + "," +
hRow.getItemNumber() + "," + hRow.getItemDesc() +
"," + hRow.getVendorNumber() + "," +
hRow.getVendorName() + "," +
hRow.getVendorSiteCode() + "," +
hRow.getAslStatus() + "," + hRow.getDisableFlag() +
"," + hRow.getPoSettlementMethod() + "\r\n");
}
//第六步,將文件轉(zhuǎn)換成byte數(shù)組
try {
//這里有兩種導(dǎo)出格式,已屏蔽的是CSV,未屏蔽的是EXCEL
//Excel
//文件只能轉(zhuǎn)成流,通過byte流轉(zhuǎn)成二進(jìn)制數(shù)組(流無法序列化做成參數(shù)傳出)
ByteArrayOutputStream os = new ByteArrayOutputStream();
//文件寫入流
wb.write(os);
//流轉(zhuǎn)數(shù)組
abyte0 = os.toByteArray();
//關(guān)閉流
os.close();
// //CSV
// //字符直接轉(zhuǎn)byte數(shù)組
// abyte0 = buffer.toString().getBytes();
} catch (Exception e) {
e.printStackTrace();
}
deleteIter.closeRowSetIterator();
return abyte0;
} //end export()