写一个查询结果的页面,文件名为result2excel.jsp,并在头部加上
<%@ page contentType="application/vnd.ms-excel; charset=gb2312" import="java.sql.*"%>
<%
    response.setHeader("Content-disposition","attachment; filename=test.xls");
%>然后在需要下载的地方,写一个按钮
<input type="button" value="下载Excel文档" name="download1" onclick="location.href='result2excel.jsp'" >这个是个简单的实现方法,其实,就是欺骗客户端的浏览器,以为这个文件是个excel文件(当然,其实只是个网页的table),然后会要求下载为Excel格式。这里的方法是设定了 响应的头(response)的mime类型为“application/vnd.ms-excel”,也就是Excel文件类型。

解决方案 »

  1.   

    怎么设置excel单元格的格式呢?现在下载下来的数据都是用科学计数法显示的,怎么能设置为正常显示原值呢?
      

  2.   

    用Jakata的一个开原API : POI
    你搜索一下有文章的
      

  3.   

    原作者:SonyMusic
    读:rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
    在Java中读取Excel文件的内容 
    在这里,我使用的是一个叫Java Excel API的东西,类似的还有jakarta的POI,不过感觉那个
    太复杂了点儿。而且jxl对中文的支持相当的好,至少我在用的过程中一点问题没出。 
    一、下载地址
    http://www.andykhan.com/jexcelapi/ 
    二、特性
    可以读取Excel 95, 97, 2000文件
    可以读或写Excel 97及其以后版本的的公式(不过我发现好像有bug)
    生成Excel 97格式的电子表格
    支持字体、数字和日期格式化
    支持单元格的颜色和阴影
    可以编辑现有的文件 
    三、读文件
    //声明一下,记得后面要关闭哦。。
    Workbook workbook = null; 
    try {
       workbook = Workbook.getWorkbook(new File("d:\\temp\\TestRead.xls"));
    } catch (Exception e) {
       throw new Exception("file to import not found!");

    Sheet sheet = workbook.getSheet(0);
    Cell cell = null; 
    int columnCount=3;
    int rowCount=sheet.getRows();
    for (int i = 0; i <rowCount; i++) {
       for (int j = 0; j <columnCount; j++) {
           //注意,这里的两个参数,第一个是表示列的,第二才表示行
           cell=sheet.getCell(j, i);
           //要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确
           if(cell.getType()==CellType.NUMBER){
               System.out.print(((NumberCell)cell).getValue());
           }
           else if(cell.getType()==CellType.DATE){
               System.out.print(((DateCell)cell).getDate());
           }
           else{
               System.out.print(cell.getContents());
           }
           
           //System.out.print(cell.getContents());
           System.out.print("\t");
       }
       System.out.print("\n");
    }
    //关闭它,否则会有内存泄露
    workbook.close(); 写:wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
    在Java中向Excel文件写入内容 四、导出数据到Excel文件中
    下面的例子,设置了数字、日期的格式,还有字体,颜色等。 
    File tempFile=new File("d:/temp/output.xls");
    WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
    WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0); 
    //一些临时变量,用于写到excel中
    Label l=null;
    jxl.write.Number n=null;
    jxl.write.DateTime d=null; 
    //预定义的一些字体和格式,同一个Excel中最好不要有太多格式
    WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.BLUE); 
    WritableCellFormat headerFormat = new WritableCellFormat (headerFont); 
    WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.RED); 
    WritableCellFormat titleFormat = new WritableCellFormat (titleFont); 
    WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, Underlinestyle.NO_UNDERLINE, jxl.format.Colour.BLACK); 
    WritableCellFormat detFormat = new WritableCellFormat (detFont); 
    NumberFormat nf=new NumberFormat("0.00000");  //用于Number的格式
    WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf); 
    DateFormat df=new DateFormat("yyyy-MM-dd");//用于日期的
    WritableCellFormat dateFormat = new WritableCellFormat (detFont, df); 
    //剩下的事情,就是用上面的内容和格式创建一些单元格,再加到sheet中
    l=new Label(0, 0, "用于测试的Excel文件", headerFormat);
    sheet.addCell(l); 
    //add Title
    int column=0;
    l=new Label(column++, 2, "标题", titleFormat);
    sheet.addCell(l);
    l=new Label(column++, 2, "日期", titleFormat);
    sheet.addCell(l);
    l=new Label(column++, 2, "货币", titleFormat);
    sheet.addCell(l);
    l=new Label(column++, 2, "价格", titleFormat);
    sheet.addCell(l); 
    //add detail
    int i=0;
    column=0;
    l=new Label(column++, i+3, "标题 "+i, detFormat);
    sheet.addCell(l);
    d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
    sheet.addCell(d);
    l=new Label(column++, i+3, "CNY", detFormat);
    sheet.addCell(l);
    n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);
    sheet.addCell(n); 
    i++;
    column=0;
    l=new Label(column++, i+3, "标题 "+i, detFormat);
    sheet.addCell(l);
    d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
    sheet.addCell(d);
    l=new Label(column++, i+3, "SGD", detFormat);
    sheet.addCell(l);
    n=new jxl.write.Number(column++, i+3, 98832, priceFormat);
    sheet.addCell(n); 
    //设置列的宽度
    column=0;
    sheet.setColumnView(column++, 20);
    sheet.setColumnView(column++, 20);
    sheet.setColumnView(column++, 10);
    sheet.setColumnView(column++, 20); 
    workbook.write();
    workbook.close();
      

  4.   

    function AutomateExcel()
    {
          var i,j;
       // Start Excel and get Application object.
          var oXL = new ActiveXObject("Excel.Application");      oXL.Visible = true;   // Get a new workbook.
          var oWB = oXL.Workbooks.Add();
          var oSheet = oWB.ActiveSheet;   // Add table headers going cell by cell.
       /*
          oSheet.Cells(1, 1).Value = "First Name";
          oSheet.Cells(1, 2).Value = "Last Name";
          oSheet.Cells(1, 3).Value = "Full Name";
          oSheet.Cells(1, 4).Value = "Salary";
         */  for(i=0;i<tblout.rows.length;i++)
    for(j=0;j<tblout.rows(i).cells.length;j++)
    oSheet.Cells(i+1, j+1).Value = tblout.rows(i).cells(j).innerText
       
          oXL.Visible = true;
          oXL.UserControl = true;
    }
    IE的属性得修改,工具-》选项-》-》安全-》自定义级别-》起用前三项