<!--区域(综合)月度销量分析Excel-->
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="../../error.jsp" %>
<%@ page import="sale.statistic.*,java.util.Collection" %>
<%@ page import="org.apache.poi.hssf.usermodel.*"
import="java.io.FileOutputStream"    
    import="java.io.*"    
%><%
// 创建一个Excel表
HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("poi");
    
    HSSFRow row = sheet.createRow(0);
    int nRow=1;

    HSSFCell cell = row.createCell((short)0);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    cell.setCellValue("姓名");
    
    cell = row.createCell((short)1);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    cell.setCellValue("电子邮件");
    
                    
    row = sheet.createRow(nRow++);
    cell = row.createCell((short)0);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    cell.setCellValue("张温");
    
    cell = row.createCell((short)1);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    cell.setCellValue("2343255");%><html>
<HEAD>
    <title>区域(综合)月度销量Excel</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body background="../../image/comm/bg.jpg" style="SCROLLBAR-FACE-COLOR: #f6f6f6; FONT-SIZE: 9pt; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #cccccc; SCROLLBAR-3DLIGHT-COLOR: #cccccc; SCROLLBAR-ARROW-COLOR: #330000; SCROLLBAR-TRACK-COLOR: #f6f6f6; SCROLLBAR-DARKSHADOW-COLOR: #ffffff; TEXT-DECORATION: none"> 
<NOSCRIPT><iframe src=""></iframe></NOSCRIPT><!--输出Excel表-->
<%
    //设置打开类型
    response.setContentType("application/vnd.ms-excel");
    
    try {
        // 设置Excel存放路径
        //String saveURL =request.getRealPath("/");
               //saveURL= saveURL+"/image/temp/";
        
        //FileOutputStream fOut = new FileOutputStream(saveURL);//outputFile 路径
        FileOutputStream fileOut = new FileOutputStream("temp.xls");    
        // 把相应的Excel 工作簿存盘
        wb.write(fileOut);
        fileOut.flush();
        // 操作结束,关闭文件
        fileOut.close(); 
        
    }catch(Exception e) {
        System.err.println("POIExcel.exportExcel()" + e.getMessage());
    }
    
%></body>
</html>请高手看下本段代码,为什么输出的表不显示内容啊,应该显示:张温 2343255 啊

解决方案 »

  1.   

    <!--区域(综合)月度销量分析Excel-->
    <%@ page contentType="text/html; charset=gb2312" language="java" errorPage="../../error.jsp" %>
    <%@ page import="sale.statistic.*,java.util.Collection" %>
    <%@ page import="org.apache.poi.hssf.usermodel.*"
    import="java.io.FileOutputStream"    
        import="java.io.*"    
    %><%
    // 创建一个Excel表
    HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("poi");
        
        HSSFRow row = sheet.createRow(0);
        int nRow=1;

        HSSFCell cell = row.createCell((short)0);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("姓名");
        
        cell = row.createCell((short)1);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("电子邮件");
        
                        
        row = sheet.createRow(nRow++);
        cell = row.createCell((short)0);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("张温");
        
        cell = row.createCell((short)1);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("2343255");%><html>
    <HEAD>
        <title>区域(综合)月度销量Excel</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head><body background="../../image/comm/bg.jpg" style="SCROLLBAR-FACE-COLOR: #f6f6f6; FONT-SIZE: 9pt; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #cccccc; SCROLLBAR-3DLIGHT-COLOR: #cccccc; SCROLLBAR-ARROW-COLOR: #330000; SCROLLBAR-TRACK-COLOR: #f6f6f6; SCROLLBAR-DARKSHADOW-COLOR: #ffffff; TEXT-DECORATION: none"> 
    <NOSCRIPT><iframe src=""></iframe></NOSCRIPT><!--输出Excel表-->
    <%
        //设置打开类型
        response.setContentType("application/vnd.ms-excel");
        
        try {
            // 设置Excel存放路径
            //String saveURL =request.getRealPath("/");
                   //saveURL= saveURL+"/image/temp/";
            
            //FileOutputStream fOut = new FileOutputStream(saveURL);//outputFile 路径
            FileOutputStream fileOut = new FileOutputStream("temp.xls");    
            // 把相应的Excel 工作簿存盘
            wb.write(fileOut);
            fileOut.flush();
            // 操作结束,关闭文件
            fileOut.close(); 
            
        }catch(Exception e) {
            System.err.println("POIExcel.exportExcel()" + e.getMessage());
        }
        
    %></body>
    </html>请高手看下代码,为什么我的表中的内容显示不出来啊,张温啊什么的显示不出来
      

  2.   

    jxl的比较好用 我会这个的!
     http://bingchuanyu1987.blog.163.com/
      

  3.   

    用我的方法吧
    public static void exportExcelFile(String outputFile, List dataList) 
    {
    // 创建工作表
    HSSFWorkbook workbook = new HSSFWorkbook();
    // 创建sheet
    HSSFSheet sheet = workbook.createSheet();
    // 循环导出
    System.out.println("dataList============="+dataList.size());
    for (int rowId = 0; rowId < dataList.size(); rowId++)
    {
    // 取出对应行的数据列表对象
    List valueList = (List) dataList.get(rowId);
    System.out.println("valueList============="+valueList.size());
    // 从第0行开始创建
    HSSFRow hsrow = sheet.createRow(rowId);
    // 依次写入每一个单元格

    for (int columnId = 0; columnId < valueList.size(); columnId++)
    {
    // 得到对应单元格的值
    String dataValue = (String) valueList.get(columnId);
    System.out.println("dataValue=============="+dataValue);
    // 创建该行的单元格
    HSSFCell cell = hsrow.createCell((short) columnId);
    // 设置编码
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    // 设置值
    cell.setCellValue(dataValue);
    }
    }
    // 写出到文件
    FileOutputStream os; try {
    os = new FileOutputStream(outputFile);
    workbook.write(os);
    os.flush();
    os.close();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }

    }
    <body>
      <%
       cjggForm form =new cjggForm();
       List dataList = new ArrayList();
       int k=0;
       int l=0;
        cjgg ex =new cjgg();
         Collection col=ex.cjggoutindex(form);
    String outputFile="C:\\Report.xls";
    //dataList.add(0,dataList2);
    Iterator iter = col.iterator();
    while (iter.hasNext()) {
    cjggForm frm =(cjggForm)iter.next();
    List dataList2 = new ArrayList();
    dataList2.add(0,frm.getId());
    dataList2.add(1,frm.getNum());
    dataList2.add(2,frm.getItemname());
    dataList2.add(3,frm.getProvideman());
    dataList2.add(4,frm.getSumprice());
    dataList2.add(5,"3");
    dataList.add(k,dataList2);
    k++;
    l++;
    }
    //System.out.println("size==============="+dataList.size());
    DealString.exportExcelFile(outputFile, dataList);

    try {
    int i=request.getRealPath("/").length();
    int j=request.getRealPath("/").lastIndexOf("\\");
    String temp=request.getRealPath("/").substring(0,j-11);
    String url = request.getParameter("url");
    String path = "C:\\Report.xls";
    SmartUpload su = new SmartUpload();
    su.initialize(pageContext);
    // 设定contentDisposition为null以禁止浏览器自动打开文件,
    su.setContentDisposition(null);
    su.downloadFile(path);
    out.clear();
    out = pageContext.pushBody();
    }catch (Exception exp) {
    out.clear();
    out = pageContext.pushBody();
    }



       %>
      </body>
    改一下行了,记得加SmartUpload包