你这段代码是不是在JSP文件中写的,如果是,检查一下此JSP文件包含这段代码的<% %>之前不能有空行.

解决方案 »

  1.   

    我给你一个例子吧
    页面文件<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ page language="java" contentType="text/html;charset=GBK" %><html:html>
    <html:form method="post" action="/download/uploadExcel" enctype="multipart/form-data">
    <html:button property="button" onclick="printAll()">
    DownLoad 
    </html:button><input type="button" name="excel" value="excel" onclick="downloadexcel()" style="width:50">
    <html:file property="excelfile"/>
    <input type="button" name="showfile" value="showfile" onclick = "alert(document.all.test.value)"/>
    <input type="submit" name="upload" value="upload"/>
    </html:form>
    </html:html>
    <script language='javascript'>function downloadexcel()

    location.href="http://localhost:8080/downfile/download/excel.do"; 
    }</script>java文件
    public class DownFile extends Action {
       public static Logger logger = Logger.getLogger(DownFile.class);
        public ActionForward execute(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
            ActionForward myForward = null;
            try {            String myAction = mapping.getParameter();
                if ("EXCEL".equalsIgnoreCase(myAction)) {
                    myForward = performDownExcel(mapping, form, request, response);
                } 
            } catch (Exception e) {
                throw e;
            }
            return myForward;
        } public ActionForward performDownExcel(ActionMapping mapping,
                                              ActionForm form,
                                              HttpServletRequest request,
                                              HttpServletResponse response) throws
                Exception {
            OutputStream os = response.getOutputStream(); //取得输出流
            try {
                String fname = "test"; //txt文件名
                response.reset(); //清空输出流            response.setContentType("application/vnd.ms-excel"); //定义输出类型
                ExcelBean eb = new ExcelBean();
                eb.expordExcel(os); //调用生成Excel文件bean
                System.setOut(new PrintStream(os));
                os.flush();        } catch (Exception e) {
                System.out.println(e);
            } finally {
                os.close();
            }
            return mapping.findForward("display");
        }
    生产excel的java文件package com.bean;import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.hssf.usermodel.HSSFFooter;
    import org.apache.poi.hssf.usermodel.HSSFHeader;
    import java.io.*;
    public class ExcelBean {    public void expordExcel(OutputStream os) throws Exception{        HSSFWorkbook wb = new HSSFWorkbook();        HSSFSheet sheet = wb.createSheet("new sheet");
            HSSFHeader header = sheet.getHeader();        header.setCenter("工资报表");        HSSFRow row1 = sheet.createRow((short) 0);        HSSFCell cell11 = row1.createCell((short) 0);        cell11.setEncoding(HSSFCell.ENCODING_UTF_16);        cell11.setCellValue("编号");        HSSFCell cell12 = row1.createCell((short) 1);        cell12.setEncoding(HSSFCell.ENCODING_UTF_16);        cell12.setCellValue("部门");        HSSFCell cell13 = row1.createCell((short) 2);        cell13.setEncoding(HSSFCell.ENCODING_UTF_16);        cell13.setCellValue("姓名");        HSSFCell cell14 = row1.createCell((short) 3);        cell14.setEncoding(HSSFCell.ENCODING_UTF_16);        cell14.setCellValue("应发工资");        HSSFCell cell15 = row1.createCell((short) 4);        cell15.setEncoding(HSSFCell.ENCODING_UTF_16);        cell15.setCellValue("基本工资");        HSSFCell cell16 = row1.createCell((short) 5);        cell16.setEncoding(HSSFCell.ENCODING_UTF_16);        cell16.setCellValue("岗位工资");        HSSFCell cell17 = row1.createCell((short) 6);        cell17.setEncoding(HSSFCell.ENCODING_UTF_16);        cell17.setCellValue("奖金");        sheet.setGridsPrinted(true);        HSSFFooter footer = sheet.getFooter();        footer.setRight("Page " + HSSFFooter.page() + " of " +                        HSSFFooter.numPages());
            wb.write(os);    }
        public static void main(String[] args) {        FileOutputStream fos = null;
            try {
                ExcelBean eb = new ExcelBean();
                fos = new FileOutputStream("test.xls");
                eb.expordExcel(fos);        } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
      

  2.   

    我给你一个例子吧
    页面文件<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ page language="java" contentType="text/html;charset=GBK" %><html:html>
    <html:form method="post" action="/download/uploadExcel" enctype="multipart/form-data">
    <html:button property="button" onclick="printAll()">
    DownLoad 
    </html:button><input type="button" name="excel" value="excel" onclick="downloadexcel()" style="width:50">
    <html:file property="excelfile"/>
    <input type="button" name="showfile" value="showfile" onclick = "alert(document.all.test.value)"/>
    <input type="submit" name="upload" value="upload"/>
    </html:form>
    </html:html>
    <script language='javascript'>function downloadexcel()

    location.href="http://localhost:8080/downfile/download/excel.do"; 
    }</script>java文件
    public class DownFile extends Action {
       public static Logger logger = Logger.getLogger(DownFile.class);
        public ActionForward execute(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
            ActionForward myForward = null;
            try {            String myAction = mapping.getParameter();
                if ("EXCEL".equalsIgnoreCase(myAction)) {
                    myForward = performDownExcel(mapping, form, request, response);
                } 
            } catch (Exception e) {
                throw e;
            }
            return myForward;
        } public ActionForward performDownExcel(ActionMapping mapping,
                                              ActionForm form,
                                              HttpServletRequest request,
                                              HttpServletResponse response) throws
                Exception {
            OutputStream os = response.getOutputStream(); //取得输出流
            try {
                String fname = "test"; //txt文件名
                response.reset(); //清空输出流            response.setContentType("application/vnd.ms-excel"); //定义输出类型
                ExcelBean eb = new ExcelBean();
                eb.expordExcel(os); //调用生成Excel文件bean
                System.setOut(new PrintStream(os));
                os.flush();        } catch (Exception e) {
                System.out.println(e);
            } finally {
                os.close();
            }
            return mapping.findForward("display");
        }
    生产excel的java文件package com.bean;import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.hssf.usermodel.HSSFFont;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.hssf.usermodel.HSSFFooter;
    import org.apache.poi.hssf.usermodel.HSSFHeader;
    import java.io.*;
    public class ExcelBean {    public void expordExcel(OutputStream os) throws Exception{        HSSFWorkbook wb = new HSSFWorkbook();        HSSFSheet sheet = wb.createSheet("new sheet");
            HSSFHeader header = sheet.getHeader();        header.setCenter("工资报表");        HSSFRow row1 = sheet.createRow((short) 0);        HSSFCell cell11 = row1.createCell((short) 0);        cell11.setEncoding(HSSFCell.ENCODING_UTF_16);        cell11.setCellValue("编号");        HSSFCell cell12 = row1.createCell((short) 1);        cell12.setEncoding(HSSFCell.ENCODING_UTF_16);        cell12.setCellValue("部门");        HSSFCell cell13 = row1.createCell((short) 2);        cell13.setEncoding(HSSFCell.ENCODING_UTF_16);        cell13.setCellValue("姓名");        HSSFCell cell14 = row1.createCell((short) 3);        cell14.setEncoding(HSSFCell.ENCODING_UTF_16);        cell14.setCellValue("应发工资");        HSSFCell cell15 = row1.createCell((short) 4);        cell15.setEncoding(HSSFCell.ENCODING_UTF_16);        cell15.setCellValue("基本工资");        HSSFCell cell16 = row1.createCell((short) 5);        cell16.setEncoding(HSSFCell.ENCODING_UTF_16);        cell16.setCellValue("岗位工资");        HSSFCell cell17 = row1.createCell((short) 6);        cell17.setEncoding(HSSFCell.ENCODING_UTF_16);        cell17.setCellValue("奖金");        sheet.setGridsPrinted(true);        HSSFFooter footer = sheet.getFooter();        footer.setRight("Page " + HSSFFooter.page() + " of " +                        HSSFFooter.numPages());
            wb.write(os);    }
        public static void main(String[] args) {        FileOutputStream fos = null;
            try {
                ExcelBean eb = new ExcelBean();
                fos = new FileOutputStream("test.xls");
                eb.expordExcel(fos);        } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
      

  3.   

    谢谢zj_pht,我是想针对我的错误,能不能帮我看看
      

  4.   

    File f = new File("c:\\1.xls");
    FileInputStream fin = new FileInputStream(f);
    response.setContentType("application/vnd.ms-excel;charset=gb2312");
    response.setHeader("Content-Disposition", "attachment; filename=1.xls");
    OutputStream output = response.getOutputStream();
    byte[] buf = new byte[1024];
    int r = 0;
    while((r = fin.read(buf, 0, buf.length))!= -1) {
         output.write(buf, 0, r);
    }
    fin.close();
    output.close();试一下
      

  5.   

    在web.xml的
    <welcome-file-list>上面增加:<!-- mime-mapping -->
    <mime-mapping>
    <extension>xls</extension>
    <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>