十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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()