大家好,我在使用 DWR 做 EXCEL 导出的时候遇到一个问题。
大体描述一下:我要使用 DWR 结合 EXT 完成一个将后台数据导出到 EXCEL 功能,主要目的是增加一个导出进度提示,当用户提交导出数据请求时开始给出导出数据进度条提示,当响应结束时进度条隐藏,但是每次进行导出的时候都会报异常:response committed.... available header
我的代码如下:
java 代码
- package bss.rsccut.utils;
- import java.io.IOException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import jxl.Workbook;
- import jxl.format.Colour;
- import jxl.format.UnderlineStyle;
- import jxl.write.Label;
- import jxl.write.WritableCellFormat;
- import jxl.write.WritableFont;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
- import jxl.write.WriteException;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.directwebremoting.WebContext;
- import org.directwebremoting.WebContextFactory;
- import bss.common.DAOException;
- import bss.common.util.SpringUtil;
- import bss.rsccut.cuttask.dao.CutTaskDAO;
- /**
- * @author lsz
- *
- * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
- */
- public class PrintService{
- private static Log logger = LogFactory.getLog(PrintService.class);
- private WebContext ctx;
- private CutTaskDAO cutTaskDAO;
- public PrintService() {
- this.ctx = WebContextFactory.get();
- }
- private CutTaskDAO getCutTaskDAO(int areaId) {
- return (CutTaskDAO) SpringUtil.getBean(areaId, SpringUtil.RSC,
- getServletContext(), "cutTaskDAO");
- }
- private HttpServletResponse getResponse() {
- WebContext ctx = WebContextFactory.get();
- return ctx.getHttpServletResponse();
- }
- private ServletContext getServletContext() {
- return ctx.getServletContext();
- }
- public int export2Excel(Map map) throws IOException {
- logger.debug("-------go to PrintService.export2Excel-------");
- int rtCode = -1;
- HttpServletResponse response = getResponse();
- if (response == null) {
- logger.debug("----response is null--------");
- return -1;
- }
- // response.setStatus(HttpServletResponse.SC_ACCEPTED);
- logger.debug("--------response--------" + response);
- String taskId = "" + map.get("taskId");
- logger.debug("-----taskId-------" + taskId);
- String atomId = "" + map.get("atomId");
- logger.debug("----atomId------" + atomId);
- String targets = "" + map.get("targets");
- logger.debug("-------targets----" + targets);
- cutTaskDAO = getCutTaskDAO(1);
- logger.debug("-------cutTaskDAO------"+cutTaskDAO);
- try {
- if (targets.equals("0")) {
- logger.debug("---------export all data--------");
- map = cutTaskDAO.findAllPrintItems(taskId, atomId);
- } else {
- logger.debug("----------export some data---------");
- map = cutTaskDAO.findAppointedPrintItems(taskId, atomId,
- targets);
- }
- } catch (DAOException e) {
- logger.debug("查询打印头信息异常,异常信息:" + e.getMessage());
- e.printStackTrace();
- }
- List headList = (List) map.get("headList");
- logger.debug("------headList-------\n"+headList);
- List itemList = (List) map.get("itemList");
- Date today = new Date();
- DateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
- String fname = "task_" + taskId + "_rscinf_" + df.format(today);
- response.addHeader("content-disposition", "attachment;filename="
- + fname + ".xls");
- response.setContentType("application/vnd.ms-excel");
- ServletOutputStream outstream = response.getOutputStream();
- WritableWorkbook workbook = Workbook.createWorkbook(outstream);
- WritableSheet sheet = workbook.createSheet("工单信息", 0);
- WritableFont wfc = null;
- WritableCellFormat wcfFC = null;
- //打印头
- WritableFont off = new WritableFont(WritableFont.ARIAL, 8,
- WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
- Colour.BLUE);
- WritableFont nff = new WritableFont(WritableFont.ARIAL, 8,
- WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
- Colour.RED);
- WritableCellFormat ocf = new WritableCellFormat(off);
- WritableCellFormat ncf = new WritableCellFormat(nff);
- wfc = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD, false,
- UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
- wcfFC = new WritableCellFormat(wfc);
- int pos = 0;
- try {
- for (Iterator it = headList.iterator(); it.hasNext();) {
- Map head = (Map) it.next();
- String flag = head.get("flag").toString();
- String headId = head.get("headId").toString();
- String headText = head.get("head").toString();
- head.put("pos", new Integer(pos));
- if (flag.equals("1")) {
- Label label = new Label(pos, 0, "原" + headText, ocf);
- sheet.addCell(label);
- pos++;
- label = new Label(pos, 0, "新" + headText, ncf);
- sheet.addCell(label);
- pos++;
- } else {
- Label label = new Label(pos, 0, headText, wcfFC);
- sheet.addCell(label);
- pos++;
- }
- }
- } catch (WriteException e) {
- logger.debug("写EXCEL头异常,异常信息:" + e.getMessage());
- e.printStackTrace();
- }
- //打印记录
- nff = new WritableFont(WritableFont.ARIAL, 8, WritableFont.NO_BOLD,
- false, UnderlineStyle.NO_UNDERLINE, Colour.RED);
- ncf = new WritableCellFormat(nff);
- List subList = null;
- int nRow = 1;
- int nCol = 0;
- try {
- for (Iterator it = itemList.iterator(); it.hasNext();) {
- subList = (List) it.next();
- nCol = 0;
- for (Iterator subIt = subList.iterator(); subIt.hasNext();) {
- Map item = (Map) subIt.next();
- int nFindPos = 0;
- for (Iterator colIt = headList.iterator(); colIt.hasNext();) {
- Map head = (Map) colIt.next();
- if (head.get("headId").toString().equals(
- item.get("itemId").toString())) {
- nFindPos = Integer.valueOf(
- head.get("pos").toString()).intValue();
- break;
- }
- }
- String oldValue = item.get("oldValue").toString();
- Label label = new Label(nFindPos, nRow, oldValue);
- sheet.addCell(label);
- if (item.get("newFlag").toString().equals("1")) {
- String newValue = item.get("newValue").toString();
- label = new Label(nFindPos + 1, nRow, newValue, ncf);
- sheet.addCell(label);
- }
- }
- nRow++;
- }
- workbook.write();
- workbook.close();
- rtCode = 1;
- } catch (WriteException e) {
- logger.debug("写EXCEL内容异常,异常信息:" + e.getMessage());
- e.printStackTrace();
- }
- return rtCode;
- }
- }
js 代码
- <input type="hiden" id="taskId">
- <input type="button" id="exportBtn" value="导出">
- function __printOrder(atom){
- var taskId = $('taskOps').value;
- if(!taskId)
- {
- Ext.Msg.alert('操作提示', '请选择割接任务');
- disabledBtns(false);
- return;
- }
- var params = {taskId:taskId,atomId:atom,targets:'0'};
- printService.export2Excel(params,{
- callback:function(data){
- if(data == 1){
- Ext.Msg.alert('操作提示','导出成功');
- }else{
- Ext.Msg.alert('操作提示','导出异常,请查看日志');
- }
- },
- timeout:120000,
- exceptionHandler:function(message){
- Ext.Msg.hide();
- Ext.Msg.alert('异常','导出异常,异常信息:'+message);
- },
- httpMethod:'POST',
- rpcType:DWREngine.XMLHttpRequest,
- preHook:function(){
- Ext.Msg.show({
- title:'请稍侯',
- msg:'正在导出数据...',
- closable:false
- });
- },
- postHook:function(){Ext.Msg.hide();}
- });
- }
- Ext.get('exportBtn').on('click',function(){
- __printOrder('103');
- });
哪位大侠有使用DWR访问servlet API的经验或者比较好的解决方法,望不吝赐教,不甚感激!