Excel文件下载不在服务器/站点中工作,但在本地系统中工作

时间:2014-01-20 10:34:55

标签: java

这是我的代码,可以在localhost中正常下载以下载Excel文件。但它不适用于服务器/站点。请帮我解决这个问题。

public void execute() throws Exception {

    //initializing http session object
    objHttpSession = ServletActionContext.getRequest().getSession();

    //checking for 'customerID' attribute in session
    if (objHttpSession.getAttribute("customerID") != null) {

        //initializing http session object
        objRequest = ServletActionContext.getRequest();
        objResponse = ServletActionContext.getResponse();

        Integer campaignID = Integer.parseInt(objRequest.getParameter("campaignId"));

        //now invoking the getData method of KeywordsServiceImpl class
        Object[] dataObject = objKeywordsService.getData(campaignID);

        //retrieving the list of keywords from <dataObject>
        lstkeywords = (List<Keywords>) dataObject[0];
        HSSFWorkbook objWorkbook = null;

        //name of excel file
        String fileName = "SiteReport.xls";

        //calling the ExcelWorkBook method to create the <objWorkbook> object
        objWorkbook = createExcelWorkBook(true, lstkeywords);

        FileOutputStream fileOut = new FileOutputStream(fileName);
        objWorkbook.write(fileOut);
        fileOut.close();

        ServletOutputStream outStream = objResponse.getOutputStream();

        FileInputStream in = new FileInputStream(fileName);

        objResponse.setContentType("application/excel");

        // sets HTTP header attachment; filename=\"report.pdf\"
        objResponse.setHeader("Content-Disposition", "attachment; filename=\"" + fileName);

        // reads the file's bytes and writes them to the objResponse stream
        int length;
        while ((length = in.read()) != -1) {
            outStream.write(length);
        }
        in.close();
        outStream.close();
    }
}

private HSSFWorkbook createExcelWorkBook(boolean row, List<Keywords> lstkeywords) {

    HSSFWorkbook objWorkbook = new HSSFWorkbook();
    HSSFSheet objSheet = null;
    HSSFRow objRow = null;

    objSheet = objWorkbook.createSheet("Serps");
    // wrap column 1 and 2

    objSheet.autoSizeColumn(0, true);
    objSheet.autoSizeColumn(1, true);

      ******************
        //Codes for inserting values in cells
      ******************
    }
    return objWorkbook;
}

0 个答案:

没有答案