这个我做过,不过我的思路和你的有点不一样.我的是这样的:
首先通过查询语句得到满足查询条件的数据,把这些数据写到一个JTable中,然后在把这个JTable的内容生成Excel文件.这样子用户可以在生成Excel文件前得到自己想要的文件.其实这个不难,我给你一些源代码,你可以自己看看,有不懂的可以再问.
/*
 * TestExcel.java
 *
 * Created on 2004年8月3日, 下午2:15
 */package Autosale;
import java.io.File;import jxl.Workbook;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.WritableFont;
import jxl.write.WritableCellFormat;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.DateFormat;public class TestExcel {
    private final String FILE_NAME="c:\\test.xls";
    private String []title={"Test1","Test2"};
    private String [][]content = {{"1111","2222"},{"3333","4444"},{"5555","6666"}};
    
    private File output;
    
    private WritableWorkbook wbook;
    private WritableSheet wsheet;
    
    
    public TestExcel() {
        init();
    }
    
    private void init() {
        output=new File(FILE_NAME);
        try{
            wbook=Workbook.createWorkbook(output);
            wsheet=wbook.createSheet("TEST EXCEL",0);
        }
        catch(Exception e){System.out.println(e.getMessage());}
    }
    
    public void export() {
        try{
            Label l=null;
            jxl.write.Number n=null;
            jxl.write.DateTime d=null;
            
            WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
            WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
            
            WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
            WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
            
            WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,  jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            WritableCellFormat detFormat = new WritableCellFormat(detFont);
            
            
            l=new Label(0, 0, "TEST EXCEL", headerFormat);
            wsheet.addCell(l);
            
            //add cell
            for(int i=0;i<title.length;i++) {
                l=new Label(i, 1, title[i], titleFormat);
                wsheet.addCell(l);
            }
            
            
            for(int j = 0; j<content[1].length; j ++){
                for(int i = 0; i<=content[2].length; i ++) {
                    l=new Label(j,i+2,content[i][j],detFormat);
                    wsheet.addCell(l);
                }}
            
            wbook.write();
            wbook.close();
        }catch(Exception e){ e.printStackTrace(); }
    }
    
    public static void main(String []args) {
        TestExcel test = new TestExcel();
        test.export();
    }
}

解决方案 »

  1.   

    那个jxl的包到这里下载:
    http://www.andykhan.com/jexcelapi/
    祝你好运
      

  2.   

    不知道好使么!我试试看 吧1!谢谢不知道,在生成excel报表时是否需要安装excel能不能脱离其他软件的支持,比如说pdf 需要adobe,excel格式需要excel这样客户就要安装支持软件才可以!!
      

  3.   

    我好好看看你的代码!!能实现一种我就满足了我也是初次接触!!很多都是基于web应用的你的资料很难得!可以加你的qq么FRANK1982::
    我的是174605008
      

  4.   

    周末休息,刚来上网,呵呵生成excel其实不需要excel,只是你要打开excel文件的话基本上是需要安装excel的.要脱离其他软件的支持你必须自己写一个基本功能和excel差不多的类来实现你想要的打印格式.qq一直加不了,说什么网络忙
    呵呵
    你加我试试看:5609295
    注明csdn,5257007
    谢谢
      

  5.   

    大家帮忙看看我发的那个JTree的问题啊  怎么又看JTable的,没看JTree的啊?
      

  6.   

    http://dev.csdn.net/develop/article/33/33461.shtm