我想把jsp页面的数据(从数据库中得到的数据)导出到excel中,我在网上找了两天了,有的js代码但是抛出js error,说什么服务期不能创建automation对象,我也考虑用第三方插件,poi,jxl,但是我的数据已经有了,不需再从数据库中获取,所以就没必要用它们了,高手们怎么解决啊,帮忙啊,搞了好长时间了,还是没头绪,郁闷死了,

解决方案 »

  1.   

    google了一下  这种方法用的是jxl,不是从数据库中得到的数据你看看行不?
      

  2.   

    首先去http://www.andykhan.com/jexcelapi/index.html下载最新的JExcelApi,把jxl.jar置于你的classpath中。写一个javaBean,利用JExcelApi来动态生成excel文档,我这里写一个最简单的,示意性的。复杂的你可能还要查询数据库什么的。///////////////////////////Test.java///////////////////////////////////////////
    package com.jagie.test;
    import java.io.*;
    import jxl.*;
    import jxl.write.*;
    import jxl.format.*;
    import java.util.*;
    import java.awt.Color;public class Test{
     public static void writeExcel(OutputStream os) throws Exception {
      jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
      jxl.write.WritableSheet ws = wwb.createSheet("TestSheet1", 0);
      jxl.write.Label labelC = new jxl.write.Label(0, 0, "我爱中国");
      ws.addCell(labelC);
      jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,20, WritableFont.BOLD, false,
      UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN);
      jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
      wcfFC.setBackground(jxl.format.Colour.RED);
      labelC = new jxl.write.Label(6, 0, "中国爱我",wcfFC);
      ws.addCell(labelC);
      //写入Exel工作表
      wwb.write();
      //关闭Excel工作薄对象
      wwb.close();
     } //最好写一个这样的main方法来测试一下你的这个class是否写好了。
     public static void main(String[] args)throws Exception{
      File f=new File("kk.xls");
      f.createNewFile();
      writeExcel(new FileOutputStream(f));
     }
    }  写一个jsp,来利用Test这个javabean输出excel文档。///////////////////////////test_excel.jsp//////////////////////////<%@page import="com.jagie.test.Test" %>
    <%
     response.reset();
     response.setContentType("application/vnd.ms-excel");
     Test.writeExcel(response.getOutputStream());
    %>  这样就大功告成了,你用ie访问test_excel.jsp就能在ie里面打开动态生成的excel文档了。一点乱码也没有。
      

  3.   

    利用 secser(晓武)  的方法,在你jsp查询数据库的时候,将查询出来的数据放入worksheet中,之后在jsp上面作个超链接,点击右键另存为,直接就是xls格式的,你的意思不就是查询后,直接导出为xls么
      

  4.   

    <%@ page import="java.io.OutputStream" %>
    <%@ page import="org.apache.poi.hssf.usermodel.*,java.util.*,"%>
    <%
    //Get criteria
    String xlsName =request.getParameter("xlsName");
    if (xlsName == null || xlsName.equals("")) xlsName = "newexcel.xls";
    else xlsName =xlsName + ".xls";
    //Set respond type for excel
    response.reset();
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "attachment;filename=" + xlsName);
    OutputStream os=response.getOutputStream();
    //Create excel workbook
    HSSFRow row;
    HSSFCell cell;
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Records");row = sheet.createRow( (short) 0);
    cell = row.createCell((short) 0);
    cell.setCellValue("ACCOUNT");
    cell = row.createCell((short) 1);
    cell.setCellValue("Fund");
    cell = row.createCell((short) 2);
    cell.setCellValue("Old Balance");
    cell = row.createCell((short) 3);
    cell.setCellValue("New Balance");
    cell = row.createCell((short) 4);
    cell.setCellValue("Charge Time");
    cell = row.createCell((short) 5);
    cell.setCellValue("Charge Type");
    cell = row.createCell((short) 6);
    cell.setCellValue("Operator");for (int i=1; i<100;i++)
    {
    //pay=(PaymentVO)allRecord.get(i-1);
    row = sheet.createRow( (short) i); cell = row.createCell((short) 0);
    cell.setCellValue("12345"); //cell.setCellValue(cdrVO.getCallNumber());
    cell = row.createCell((short) 1);
    cell.setCellValue("100"); //cell.setCellValue
    cell = row.createCell((short) 2);
    cell.setCellValue("0.0");
    cell = row.createCell((short) 3);
    cell.setCellValue("100.0");
    cell = row.createCell((short) 4);
    cell.setCellValue("2006-08-25");
    cell = row.createCell((short) 5);
    cell.setCellValue("web");
    cell = row.createCell((short) 6);
    cell.setCellValue("123456");
    }
    //Write to client side
    wb.write(os);
    os.flush();
    os.close();
    %>给个简单的 中间的数据自己加吧 我就是拿这个改的 希望对你有所帮助
      

  5.   

    http://community.csdn.net/Expert/topic/5084/5084069.xml?temp=.4589044
    这个帖子里有你想要的程序
      

  6.   

    最简单的方法:根据业务需要,把你要导出的数据加上<table>,<tr>和<td>标签,setHeader输出为excel文件就可以了
      

  7.   

    我用xiaochun_1984() ( ) 的方法包错:
    org.apache.jasper.JasperException: Unable to compile class for JSPGenerated servlet error:
    Syntax error on token "import", Identifier expected after this tokenAn error occurred at line: 3 in the jsp file: /xd/xdout.jsp
    Generated servlet error:
    HSSFRow cannot be resolved to a typeAn error occurred at line: 3 in the jsp file: /xd/xdout.jsp
    Generated servlet error:
    HSSFCell cannot be resolved to a typeAn error occurred at line: 3 in the jsp file: /xd/xdout.jsp
    Generated servlet error:
    HSSFWorkbook cannot be resolved to a typeAn error occurred at line: 3 in the jsp file: /xd/xdout.jsp
    Generated servlet error:
    HSSFWorkbook cannot be resolved to a typeAn error occurred at line: 3 in the jsp file: /xd/xdout.jsp
    Generated servlet error:
    HSSFSheet cannot be resolved to a type
    这是怎么回事啊?