页面顶部加上<%@ page contentType="application/x-msexcel; charset=GBK"%>
或者<%@ page contentType="application/msword; charset=GBK"%>

解决方案 »

  1.   

    <%@ page contentType="application/x-msexcel; charset=GBK"%>
    是将显示的风格就是excle了。我想是在打印的是否才将数据存成excle的形式的。将结果存到本地
      

  2.   

    <%@ page contentType="text/html;charset=gb2312"%> 
    <%@ page import="java.sql.*"%>
    <%@ page import="java.io.*"%>
    <%@ page import="java.util.*"%>
    <jsp:useBean id="user" class="com.xjgz.conn_hyj" scope="page"/>
    <%
    request.setCharacterEncoding("gb2312");
    String sqlStr=request.getParameter("sqlStr");
        response.setContentType("application/vnd.ms-excel");
    String newname="report";
        response.setHeader("Content-Disposition","attachment;filename=newname.xls");  
    response.flushBuffer();
    String filename="";
    ResultSet rs=null;
    try{
    rs=user.executeQuery(sqlStr);
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>欢迎访问</title><style type="text/css">
    <!--
    .style2 {
    color: #000000;
    font-size: 10px;
    }
    .style6 {
    font-size: 14pt;
    color: #FF0000;
    font-weight: bold;
    }
    -->
    </style>
    </head>
    <body>
      <p align="center"><span class="style6">统计报表</span></p>
    <table width="100%"  border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#999999">  
      <tr align="center">
        <td height="21">分局编号</td>
        <td height="21">本地网号</td>
        <td height="21">分局名称</td>
        <td height="21">查询总数</td>
      </tr>
      <tr>
      <%
      filename="report.xls";
      String path = request.getRealPath("/"); //取得目前目录在伺服端的实际位置
      FileWriter fw = new FileWriter(path + "\\admin\\collection\\xlsfile\\"+filename); //建立FileWrite变量,并设定由fw变量变数引用
      while (rs.next())
      {
      %>
      <tr align="center">
        <td height="22"><%=rs.getString("area_id")%></td>
        <td height="22"><%=rs.getString("latn_id")%></td>
        <td height="22"><%=rs.getString("name")%></td>
        <td height="22"><%=rs.getString("bsl")%></td>
      </tr> 
      <%
    BufferedWriter bw = new BufferedWriter(fw); //建立BufferedWriter变量,并设定由bw变量变数引用
    bw.write("分局编号"+"\t"+"本地网号"+"\t"+"分局名称"+"\t"+"查询总数"+"\t");
    bw.newLine(); //分行
    bw.flush(); //将资料更新至档案
    }
    fw.close(); //关闭档案
    }
    catch(Exception e)
    {
    out.print(e.getMessage());
    }
    finally
    {
    try
             {
                if (rs !=null)
                {
                 rs.close();
                 rs = null ;           
                }
             }catch( SQLException sqle ) {}           try
              {     
               user.getClose();
              }
             catch( SQLException sqle ) {}       
    }
      %>
    </table>
    </body>
    </html>
      

  3.   

    用HSSF就实用程序去描述每一个单元格包括样式,大小,风格
    这其中用到了几个poi包:poi-2.0-RC2-20040102.jar;poi-contrib-2.0-RC2-20040102.jar;
    poi-scratchpad-2.0-RC2-20040102.jar你只要提供一个超连接,然后在jsp中用程序生成excel,然后通过jspsmartupload让用户随便下载就够了,希望能对你有所帮助import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.util.Enumeration;
    import java.util.Hashtable;
    import java.util.Vector;import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    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.Region;import system.gr.pk.score.Score;
    import system.pub.Gr_Log.Gr_Log;public class Score1 {
    public synchronized void Set_Excel(Hashtable hs) throws FileNotFoundException {
    try{
    Enumeration enux = null;
    String scorejgmc ="";
    String scoresdate = "";
    String scoreodate = "";
    Vector scorevector = new Vector();

    scorejgmc = (String) hs.get("scorejgmc");
    scoresdate = (String) hs.get("scoresdate");
    scoreodate = (String) hs.get("scoreodate");
    scorevector = (Vector) hs.get("scorevector");

    String tbTile = "# 统计机构:"+scorejgmc+"报表统计时间:"+scoresdate+"--"+scoreodate;

    if (scorevector != null) {
    enux = scorevector.elements();
    }

    String tpPath = getClass().getResource("").toString();
    System.out.println("path==" + tpPath);
    tpPath = tpPath.substring(5, tpPath.indexOf("WEB-INF"))
    + "eReport/score.xls";
    System.out.println("tpPath==" + tpPath);
    FileOutputStream fos = new FileOutputStream(tpPath);//生成文件 HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    wb.setSheetName(0, "score1");
    //System.out.println("99999999999999999");
    //------------------------------------------------
    HSSFRow r = null;//动态生成行
    HSSFCell c = null;//动态生成列 HSSFRow row = s.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    s.addMergedRegion(new Region(0, (short) 0, 0, (short) 11));
    row.setHeight((short) 900);//设定行高度; cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
    HSSFCellStyle cs = wb.createCellStyle();
    cs.setFont(EFont._getFont(wb));
    cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    cell.setCellStyle(cs);
    cell.setCellValue(EFont.getUnicode("报表一", "gb2312"));
    //System.out.println("wo11111111111111");
    // ------------------------------------------------------------------------------------------------------------------------------
        HSSFRow r2 = s.createRow((short)1);
        HSSFCell c2 = r2.createCell((short) 0);
        s.addMergedRegion(new Region(1, (short) 0, 1, (short) 11));
        r2.setHeight((short)900);
        c2.setCellType(HSSFCell.CELL_TYPE_STRING);
        c2.setEncoding(HSSFCell.ENCODING_UTF_16);
        HSSFCellStyle cs2 = wb.createCellStyle();
        cs2.setFont(EFont._getFont_1(wb));
        c2.setCellStyle(cs2);
        c2.setCellValue(EFont.getUnicode(
            tbTile, "gb2312"));
        //System.out.println("222222222222222222");
    //   ------------------------------------------------------------------------------------------------------------------------------
    HSSFCellStyle cs_11 = wb.createCellStyle();
    cs_11.setFont(EFont._getFont_1(wb));
    cs_11.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFRow r11 = s.createRow((short) 2);
    r11.setHeight((short) 400);
    HSSFCell c11 = r11.createCell((short) 0);
    s.addMergedRegion(new Region(2, (short) 0, 2, (short) 2));
    c11.setCellType(HSSFCell.CELL_TYPE_STRING);
    c11.setEncoding(HSSFCell.ENCODING_UTF_16);
    c11.setCellStyle(cs_11);
    c11.setCellValue(EFont.getUnicode("客户号", "gb2312"));
    //System.out.println("3333333333333333");
    //   ++++++++++++++++++++++++++++++++++++++++++++++

      

  4.   

    HSSFCellStyle cs_111 = wb.createCellStyle();
    cs_111.setFont(EFont._getFont_1(wb));
    HSSFCell c111 = r11.createCell((short) 3);
    cs_111.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    s.addMergedRegion(new Region(2, (short) 3, 2, (short) 5));
    c111.setCellType(HSSFCell.CELL_TYPE_STRING);
    c111.setEncoding(HSSFCell.ENCODING_UTF_16);
    c111.setCellStyle(cs_111);
    c111.setCellValue(EFont.getUnicode("客户姓名", "gb2312"));
    //System.out.println("444444444444444");
    //   ++++++++++++++++++++++++++++++++++++++++++++++
    HSSFCellStyle cs_112 = wb.createCellStyle();
    cs_112.setFont(EFont._getFont_1(wb));
    HSSFCell c112 = r11.createCell((short) 6);
    cs_112.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    s.addMergedRegion(new Region(2, (short) 6, 2, (short) 8));
    c112.setCellType(HSSFCell.CELL_TYPE_STRING);
    c112.setEncoding(HSSFCell.ENCODING_UTF_16);
    c112.setCellStyle(cs_112);
    c112.setCellValue(EFont.getUnicode("签约机构", "gb2312"));
    //System.out.println("555555555555555555");
    //   ++++++++++++++++++++++++++++++++++++++++++++++
    HSSFCellStyle cs_113 = wb.createCellStyle();
    cs_113.setFont(EFont._getFont_1(wb));
    HSSFCell c113 = r11.createCell((short) 9);
    cs_113.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    s.addMergedRegion(new Region(2, (short) 9, 2, (short) 11));
    c113.setCellType(HSSFCell.CELL_TYPE_STRING);
    c113.setEncoding(HSSFCell.ENCODING_UTF_16);
    c113.setCellStyle(cs_113);
    c113.setCellValue(EFont.getUnicode("积分", "gb2312"));
    //System.out.println("666666666666666666");

    // 开始循环账户
    HSSFCellStyle cs6 = wb.createCellStyle();
    cs6.setFont(EFont._getFont_1(wb));
    cs6.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    int i = 0;
    //System.out.println("woriiiiiiiiiiiiiiiiiiiiiiiiiiiiii");
    while (enux.hasMoreElements()) {
    r = s.createRow((short) i + 3);
    Score score = new Score();
    score = (Score) enux.nextElement();
    //System.out.println("start.........................");
    //System.out.println("");
    for (short cellnum = (short) 0; cellnum < 1; cellnum++) {
    //帐号列-------------------------------------
    c = r.createCell((short) (0));
    s.addMergedRegion(new Region((short) i + 3, (short) 0,
    (short) i + 3, (short) 2));
    c.setCellStyle(cs6);
    c.setCellType(HSSFCell.CELL_TYPE_STRING);
    c.setEncoding(HSSFCell.ENCODING_UTF_16);
    if (score.getKhh() != null
    && !score.getKhh().equals("")){
    c.setCellValue(String.valueOf(score.getKhh()));
    }else{
    c.setCellValue(String.valueOf(""));
    }

    //账户名称列-------------------------------------------
    c = r.createCell((short) (3));
    s.addMergedRegion(new Region((short) i + 3, (short) 3,
    (short) i + 3, (short) 5));
    c.setCellStyle(cs6);
    c.setCellType(HSSFCell.CELL_TYPE_STRING);
    c.setEncoding(HSSFCell.ENCODING_UTF_16);
    //System.out.println("khmc="+score.getKhmc());
    if (score.getKhmc() != null
    && !score.getKhmc().equals("")){
    c.setCellValue(String.valueOf(score.getKhmc()));
    }else {
    c.setCellValue(String.valueOf(""));
    }
    //System.out.println("...............");
    //开户机构编码;-----------------------------------
    c = r.createCell((short) (6));
    s.addMergedRegion(new Region((short) i + 3, (short) 6,
    (short) i + 3, (short) 8));
    c.setCellStyle(cs6);
    c.setCellType(HSSFCell.CELL_TYPE_STRING);
    c.setEncoding(HSSFCell.ENCODING_UTF_16);
    //System.out.println("qyjg=="+score.getQyjg());
    if (score.getQyjg() != null && !score.getQyjg().equals("")){
    c.setCellValue(String.valueOf(score.getQyjg()));
    }else{
    c.setCellValue(String.valueOf(""));
    }
    //签约日期-----------------------------------------
    c = r.createCell((short) (9));
    s.addMergedRegion(new Region((short) i + 3, (short) 9,
    (short) i + 3, (short) 11));
    c.setCellStyle(cs6);
    c.setCellType(HSSFCell.CELL_TYPE_STRING);
    c.setEncoding(HSSFCell.ENCODING_UTF_16);
    //System.out.println("jf=="+score.getJf());
    if (score.getJf() != null && !score.getJf().equals("")){
    c.setCellValue(String.valueOf(score.getJf()));
    }else {
    c.setCellValue(String.valueOf(""));
    }
    //overa--
    }
    i++;
    }
    wb.write(fos);
    fos.close();
    } catch (Exception e) { System.out.println("生成EXCEL出错" + e.getMessage());
    }
    }
    }