我的工程结构~~~Struts2.1.8!
           poi.jar 就不详细解说jar文件内容、该有的应该都有了!
 我在导入的时候么有问题!
Action代码:
 
package com.boxun.action;import java.io.IOException;import java.io.OutputStream;
import java.util.List;import javax.servlet.http.HttpServletResponse;import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.interceptor.ServletResponseAware;import com.boxun.bean.Userinfo;
import com.boxun.biz.IUserBiz;
import com.opensymphony.xwork2.ActionSupport;public class OutAction extends ActionSupport implements ServletResponseAware{ /**
 * 
 */
private static final long serialVersionUID = 1L;

private IUserBiz biz ;
public void setBiz(IUserBiz biz) {
this.biz = biz;
}
private String format = "xls";
private HttpServletResponse response;
private String fileName;

public String execute(){
setResponseHeader();
try {
exportExcel(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/** 设置响应头*/
public void setResponseHeader(){
response.setContentType("application/octet-stream;charset=iso-8859-1");
try{
response.setHeader("Content-Disposition", "attachment;filename="
+java.net.URLEncoder.encode(this.fileName, "UTF-8"));
//客户端不缓存
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
}catch(Exception ex){
ex.printStackTrace();
}
}

/**导出数据*/
private void exportExcel(OutputStream os) throws IOException{
Workbook book = new HSSFWorkbook();
Sheet sheet = book.createSheet("导出信息");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("用户名");
row.createCell(2).setCellValue("密码");
row.createCell(3).setCellValue("真实姓名");
row.createCell(4).setCellValue("地址");
row.createCell(5).setCellValue("备注");
CellStyle sty = book.createCellStyle();
List<Userinfo> list = biz.getAll();
for (int i = 1; i < list.size(); i++) {
Userinfo user = list.get(i-1);
row = sheet.createRow(i);
row.createCell(0).setCellValue(user.getId());
row.createCell(1).setCellValue(user.getName());
row.createCell(2).setCellValue(user.getPass());
row.createCell(3).setCellValue(user.getLastname());
row.createCell(4).setCellValue(user.getAddres());
row.createCell(5).setCellValue(user.getRe());
}
try{
book.write(os);
}catch(Exception ex){
ex.printStackTrace();
}
}


public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
this.fileName = "导出数据.xls";
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setServletResponse(HttpServletResponse arg0) {

}

}
response.setContentType("application/octet-stream;charset=iso-8859-1");
这一句的时候就报错!!!

java.lang.NullPointerException
com.boxun.action.OutAction.setResponseHeader(OutAction.java:49)
com.boxun.action.OutAction.execute(OutAction.java:36)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)

解决方案 »

  1.   

    既已不是文本,何必指定字符集

    response.setContentType("application/octet-stream");
    好了
      

  2.   

      不行~~~!!!这样也还是一样的错误~~~也是在这一句就报错了!!!
    response.setContentType("application/octet-stream");
      

  3.   

    这样试试 
    response.setContentType("application/vnd.ms-excel");
      

  4.   

    private HttpServletResponse response;
    这里声明的根本没有实例化啊,那你在使用response肯定报NullPointerException了
      

  5.   

    我做过一个导出Excel的是在struts的配置文件中设置contentType的<action name="downloadExcel" method="WriteExcelFile" class="com.wl.upload.action.uploadAction">
            <param name="savePath">/upload</param>
            <result name="success" type="stream">
               <param name="contentType">text/plain</param>
               <param name="inputname">inputStream</param>
               <param name="contentDisposition">attachment;filename="export.xls"</param>
               <param name="bufferSize">4096</param>
            </result>
            <result name="login">/Error.jsp</result>
        </action>参考...
      

  6.   

    application/vnd.ms-excel 我就用的这个呀。 没问题啊。
      

  7.   

     request只声明但未实例化。
      

  8.   


    public ActionForward doExcel(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    Date date = new Date();
    SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); response.setContentType("application/octet-stream; charset=UTF-8");
    String s = "attachment;filename=" + time.format(date) + ".xls";
    response.setHeader("Content-Disposition", s);

    // 导出Execl表
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("sheet1"); sheet.setColumnWidth((short) 0, (short) 6000);// 给工作表列定义列宽
    sheet.setColumnWidth((short) 1, (short) 3000);// 给工作表列定义列宽
    HSSFRow row1 = sheet.createRow(0);
    HSSFCellStyle cellStyle = wb.createCellStyle(); // 创建单元格样式
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
    cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 HSSFFont font = wb.createFont(); // 设置单元格字体
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setFontName("宋体");
    font.setFontHeight((short) 200);
    cellStyle.setFont(font);

    //设置表头
    List<String> btList = new ArrayList<String>();
    btList.add("录入人");
    btList.add("录入时间");
    btList.add("责任车间");
    btList.add("责任班组");
    btList.add("故障类型");
    btList.add("故障部位");
    btList.add("问题内容");

    HSSFCell cell = null;
    for (int i = 0; i < btList.size(); i++) {// 设置表头
    cell = row1.createCell((short) i);
    cell.setCellStyle(cellStyle); // 设置表头样式
    String str = (String) btList.get(i);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
    cell.setCellValue(str);
    }

    //设置内容
    List<Aqzlwt> list = aqzlwtService.getAllAqzlwtByQueren();
    for (int i = 0; i < list.size(); i++) {
    Aqzlwt aq = list.get(i);
    HSSFRow row = sheet.createRow(i + 1);// HSSFFont font2 = wb.createFont();
    // font2.setColor(HSSFColor.RED.index);
    // HSSFCellStyle cellStyle2 = wb.createCellStyle();// 创建单元格显示样式;
    // cellStyle2.setFont(font2); HSSFCell dataCell3 = row.createCell((short) 0);
    dataCell3.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
    dataCell3.setCellValue(aq.getLrr()); HSSFCell dataCell4 = row.createCell((short) 1);
    dataCell4.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
    dataCell4.setCellValue(aq.getLrrqToPage()); HSSFCell dataCell5 = row.createCell((short) 2);
    dataCell5.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
    dataCell5.setCellValue(aq.getZrcjmc());

    HSSFCell dataCell6 = row.createCell((short) 3);
    dataCell6.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
    dataCell6.setCellValue(aq.getZrbzmc());

    HSSFCell dataCell7 = row.createCell((short) 4);
    dataCell7.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
    dataCell7.setCellValue(aq.getGzlxmc());

    HSSFCell dataCell8 = row.createCell((short) 5);
    dataCell8.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
    dataCell8.setCellValue(aq.getGzbwmc());

    HSSFCell dataCell9 = row.createCell((short) 6);
    dataCell9.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码解决中文高位字节截断
    dataCell9.setCellValue(aq.getWtms());
    // dataCell9.setCellStyle(cellStyle2);

    }
    //页面输出下载
    try {
    wb.write(response.getOutputStream());
    } catch (IOException e) {
    e.printStackTrace();
    }


    return null;
    }希望对你有帮助
      

  9.   


    羊毛出在羊身上~~~一直都是我大意了~~~
    public void setServletResponse(HttpServletResponse arg0) {
            

    既然这样写~~~没有设置~~~!!!o(︶︿︶)o 唉!!!
    其他的都没错!!!response.setContentType("application/vnd.ms-excel;");//可以!
    response.setContentType("application/octet-stream;charset=iso-8859-1");//也可以//Struts2 的 Aware方式第一次用~~~所以~~~o(︶︿︶)o 唉!!!
    public void setServletResponse(HttpServletResponse response) {
    this.response = response;
    }感谢各位! 非常感谢!!!