PG:創建一個類型為button的按鈕,設置按鈕的Action Type為firePartialAction和Event為exportexcel(按鈕類型不用
submitButton
的原因是
submitButton類型的按鈕,在導出后,再往頁面添加行或者保存時會提示報瀏覽器后退的異常
)。
CO:processFormRequest代碼:
if ("exportexcel".equals(pageContext.getParameter(EVENT_PARAM))){
byte abtye0[] = (byte[])am.invokeMethod("export");
try {
//獲取DataObject
DataObject dataObject = pageContext.getNamedDataObject("_SessionParameters");
//根據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");
//設置文件的格式 字符集
response.setContentType("application/vnd.ms-excel;charset=" +
charset); //gb2312
//設置文件大小
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();
//將字符數組寫進輸出流
toClient.write(abyte0);
//強制刷新(頁面彈出下載框)
toClient.flush();
//關閉輸出流
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(); //取當前提取的記錄集的記錄數
if (rowcount == 0)
throw new OAException("沒有需要導出的數據", OAException.ERROR);
RowSetIterator deleteIter =
vo.createRowSetIterator("deleteIter"); //建立記錄集的指示符
deleteIter.setRangeStart(0); //設置循環起點,相當于移動指針到第一條記錄
deleteIter.setRangeSize(rowcount); //設置循環次數
//第一步,創建一個webbook,對應一個Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
//第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("export");
//第三步,在sheet中添加表頭第0行,注意老版本poi對應Excel的行數列數有限制short
HSSFRow row = sheet.createRow((int)0);
//第四步,創建單元格,并設置值表頭 設置表頭居中
CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER); //創建一個劇中格式
//CSV
StringBuffer buffer =
new StringBuffer("庫存組織名稱,物料編碼,物料描述,供應商編碼,供應商名稱,供應商地點,狀態,是否禁用,結算方式\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("供應商編碼");
cell.setCellStyle(style);
cell = row.createCell((short)4);
cell.setCellValue("供應商名稱");
cell.setCellStyle(style);
cell = row.createCell((short)5);
cell.setCellValue("供應商地點");
cell.setCellStyle(style);
cell = row.createCell((short)6);
cell.setCellValue("狀態");
cell.setCellStyle(style);
cell = row.createCell((short)7);
cell.setCellValue("是否禁用");
cell.setCellStyle(style);
cell = row.createCell((short)8);
cell.setCellValue("結算方式");
cell.setCellStyle(style);
for (int i = 0; i < rowcount; i++) {
row = sheet.createRow((int)i + 1);
hRow = (CuxAslVORowImpl)deleteIter.getRowAtRangeIndex(i); //取得當前記錄
//第五步,創建單元格,并設置值
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");
}
//第六步,將文件轉換成byte數組
try {
//這里有兩種導出格式,已屏蔽的是CSV,未屏蔽的是EXCEL
//Excel
//文件只能轉成流,通過byte流轉成二進制數組(流無法序列化做成參數傳出)
ByteArrayOutputStream os = new ByteArrayOutputStream();
//文件寫入流
wb.write(os);
//流轉數組
abyte0 = os.toByteArray();
//關閉流
os.close();
// //CSV
// //字符直接轉byte數組
// abyte0 = buffer.toString().getBytes();
} catch (Exception e) {
e.printStackTrace();
}
deleteIter.closeRowSetIterator();
return abyte0;
} //end export()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。