如何使用servlet发送excel文件

时间:2014-01-22 09:44:15

标签: java servlets jxl

我试图在servlet上创建一个excel文件并将其发送到客户端浏览器,当我在一个独立的程序上执行它时,该文件是在我的计算机上创建的,但是当我尝试在servlet上执行它时它什么也没做/ p>

的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);
    }
是否有人知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,您只应拨打response.setContentType()一次。您想要返回Excel,请取出将内容类型设置为"text/html;charset=UTF-8"的那个。

其次,在将二进制文件写入后将文本写入输出流将使其搞砸。取出output.println(course);

第三,我真的不认为output.close();也是必需的,所以你也可以试试这个。