0

im trying to create an excel file on a servlet and send it to the client browser when i did it on a stand alone program the file was created on my computer but when i tried to do it on a servlet it did nothing

servlet:

response.setContentType("text/html;charset=UTF-8");
        // PrintWriter out = response.getWriter();
        String[] items=request.getParameterValues("lecture");
        String course=request.getParameter("course");
        int sheets=Integer.parseInt(request.getParameter("sheets"));
        List <XlElement> xlElements=getAllElements(items);
        ServletOutputStream output=response.getOutputStream();
        try
        {
           response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename="+course+".xls");
            CreateXl xl=new CreateXl();
            xl.createScadualFile(output, xlElements, sheets);
            output.println(course);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
            throw new ServletException("Exception in Excel Sample Servlet", e);
        }
          output.close();

createXl class

   private List<WritableSheet> xlSheets;
    private String[] days={"א","ב","ג","ד","ה"};
    private final int numOfClasses=9;
    private final int cellHeight= 1020;
    private final int cellWidth=15;
    public void createScadualFile(ServletOutputStream output, List <XlElement> items,int sheets) throws IOException, WriteException{
        xlSheets=new ArrayList<WritableSheet>();
        WritableWorkbook workbook = Workbook.createWorkbook(output);
        for(int i=0;i<sheets;i++){
            WritableSheet sheet = workbook.createSheet("week "+(i+1), i);
            xlSheets.add(sheet);
        }
        for(WritableSheet s: xlSheets){
            initSheet(s);
        }
        for(XlElement e: items){
            insertElement(e);
        }
        workbook.write();
        workbook.close();
    }




    private  WritableCellFormat getCellFormat(Colour colour, Pattern pattern) throws WriteException {
        WritableFont cellFont = new WritableFont(WritableFont.TIMES, 12);
        WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
        cellFormat.setBackground(colour, pattern);
        cellFormat.setWrap(true);
        cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.TOP);
        return cellFormat;
    }
    private void initSheet(WritableSheet s) throws WriteException{
        for(int i=0;i<days.length;i++){
            Label l=new Label(i+1,0,days[i],getCellFormat(Colour.GREY_25_PERCENT,Pattern.SOLID));
            s.setColumnView(i+1,cellWidth );
            s.addCell(l);
        }
        for(int i=0;i<numOfClasses;i++){
            Label l=new Label(0,i+1,Integer.toString(i+1),getCellFormat(Colour.GREY_25_PERCENT,Pattern.SOLID));
            s.setRowView(i+1, cellHeight);
            s.addCell(l);
        }
    }
    private void insertElement(XlElement e) throws WriteException{
        Label l=new Label(e.getCol(),e.getRow(),e.toXlString(), getCellFormat(Colour.RED,Pattern.SOLID));
        xlSheets.get(e.getWeek()).mergeCells(e.getCol(), e.getRow(), e.getCol(), e.getRow()+e.getSpan()-1);
        xlSheets.get(e.getWeek()).addCell(l);
    }

dose anybody know what I am doing wrong?

2
  • If it did "nothing" it's likely that what it actually did was throw an Exception. Have you checked your server logs to see what that was? Commented Jan 22, 2014 at 9:47
  • i debuged it and i didnt see any exception throwen it just ran through the code Commented Jan 22, 2014 at 9:59

1 Answer 1

1

First, you should only call response.setContentType() once. You want to return an Excel, so take out the one where you are setting the content type to "text/html;charset=UTF-8".

Second, writing text to the output stream after writing the binary file to it will screw it up. Take out the output.println(course);

Third, I really don't think the output.close(); is needed either, so you might try taking that out as well.

Sign up to request clarification or add additional context in comments.

1 Comment

ok i did all of those things but im still not getting any file to the browser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.