我现在要把数据库的内容导出生成EXCEL文件。
有三种方案可以考虑:
(1)调用EXCEL,一行一行地写,包括表头
(2)先做好EXCEL空表,然后只向里填数据
(3)用FastReport(或ReportMachine)做好报表,然后直接生成EXCEL文件。
(4)直接用一些控件。如xlreport现在的问题是:
(1)(2)的代码网上很多,但那种更可行,更简单呢?
(3)中生成的EXCEL文件好象丢失了格式,只有数据。
(4)没用过,不知道哪种控件比较可靠?哪位朋友用过?请现身说法,非常感谢!

解决方案 »

  1.   

    如果你的报表格式异常复杂难以用程序实现
    我建议你将数据导入到已有格式的文件中即方法(2)
    如果格式不是非常复杂我建议用(1)
    (3)法和(4)法我都没有使用过
    但是我认为(1)和(2)使用VBA技术,虽然版本的兼容性不是很好,但是还是比较稳定的。
      

  2.   

    完整的导出代码,不过是我用C++  Builder写的。我想楼主四个角也不是白来的,那就请楼主自己换成Delphi了。
       void outexcel()
       {
                Variant ExcelObjectName,iSheetName;
                long i,j,k;
                int iFieldCount;
                AnsiString iFieldName;
                if (MySaveDialog->Execute())
                {
                    ExeclFileName = MySaveDialog->FileName + ".xls";
                }
                else
                {
                    Application->MessageBox("请先选择保存的文件名字!","提示",MB_ICONINFORMATION);
                    return;
                }
                try
                {
                    ExcelObjectName = CreateOleObject("Excel.Sheet");
                }
                catch(...)
                {
                    Application->MessageBox("您系统上没有安装Excel!","提示",MB_ICONWARNING);
                    return;
                }
                try
                {
                    iFieldCount = ExcelADOQuery->FieldCount;
                    iSheetName = ExcelObjectName.OlePropertyGet("ActiveSheet");
                    for(i=0;i<iFieldCount;i++)
                    {
                        iFieldName = ExcelADOQuery->Fields->Fields[i]->FieldName;
                        iSheetName.OlePropertyGet("Cells",1,i+1).OlePropertySet("Value",iFieldName);
                    }
                    i = 1;
                    ExcelADOQuery->First();
                    for(k = 0;k < ExcelADOQuery->RecordCount; k++)
                    {
                        i++;
                        for(j = 0;j < iFieldCount; j++)
                        {
                            iFieldName = ExcelADOQuery->Fields->Fields[j]->AsString;
                            iSheetName.OlePropertyGet("Cells",i,j+1).OlePropertySet("Value",iFieldName);
                        }
                        ExcelADOQuery->Next();
                    }
                    ExcelObjectName.OleProcedure("SaveAs",ExeclFileName);
                    ExcelObjectName.OleProcedure("Close");
                }
                catch(...)
                {
                    Application->MessageBox("导出Excel失败!","警告",MB_ICONWARNING);
                    return;
                }
            }
      

  3.   

    建议用f1book吧,并且f1book支持图形、按钮等
    给你一些基本的操作代码
    //将数据信息写入excel 
    f1book.write('c:\chen.xls',F1FileExcel5);
    f1book.FilePrintSetupDlg;    //打印设置
    //打印f1book数据
     try
        f1book.Selection:='A1:'+str+inttostr(f1.MaxRow);
        f1book.SetPrintAreaFromSelection;
        f1book.FilePrint(true);
      except
        Application.MessageBox('打印出错!','提示消息',MB_ICONWARNING+MB_OK);
      end;
    //设置页面
    f1book.NumSheets := 3;                    //设置为3个sheet
    f1book.Sheet := 1;                        //当前第一个sheet为活动
    f1book.SheetName[1] := '第1页';               //设置第1页标题
    f1book.MaxRow :=10;       ///设置行列
    f1book.MaxCol :=8;
    f1book.TextRC[3,2] := '3行2列';  
    //对选择行设置字体、颜色
    f1book.Selection := 'A1:Z1';
    f1book.SetAlignment(F1HAlignCenter, False, F1VAlignCenter, 0);    
       //F1HAlignLeft 水平居左 F1HAlignCenter  水平居中 
    f1book.SetFont('宋体', 9, False, False, False, False, clBlack, False, False);
    //设置是否显示页数名称
    F1book.ShowTabs:=0;  //不显示,1显示
    //得到活动位置
    F1Book.GetActiveCell(x,y);
    //缩小f1
    f1book.ViewScale := f1book.ViewScale - 10
    //放大
    f1book.ViewScale := f1book.ViewScale + 10
    f1book.ViewScale := 100;  //还原f1book
    //设置f1book整个格子是否显示
    a.showgridlines为False
    b.在设计状态
    1.全选择
    2.format->pattern中任意选择一个喜欢的颜色即可去掉边框的格子。
    //打印方法2
    F1Book.SetPrintScale(100,False,1,1);
    F1Book.FilePrint(True);
    //设置是否显示标题栏
    将showrowheading设为False
    将showcolheading设为False
    //设置滚动
    ShowHScrollBar=0;//不显示
    ShowVScrollBar=0;//不显示
    //锁定某些行、列
    F1Book.EnableProtection := True;
    F1Book.AllowInCellEditing := True;//允许编辑某些格。
    //保存到EXcel
    f1book.write('C:\chen.xls',F1FileExcel5);
    var i:int;                       //读出Excel文件
    i:=F1FileExcel5;
    f1book.Read('C:\chen.xls',i);
    关于Formula One打印格式的动态设置问题,
    如纸张大小、设置横向打印等。简单例子: procedure TCxy.SetPrintFormat(mf1book: Tmf1book);
    var mpagesetup:if1pagesetup;
    begin
    mpagesetup:=mf1book.CreateNewPageSetup;
    mpagesetup.PaperSize:=F1PaperA4;
    mpagesetup.Landscape:=true;
    mpagesetup.FitPages:=true;
    mpagesetup.AutoPageNumber:=true;
    mpagesetup.CenterHoriz:=true;
    mpagesetup.CenterVert:=true;
    mpagesetup.Header:='';
    mpagesetup.Footer:='';
    mpagesetup.GridLines:=false;
    mpagesetup.BlackAndWhite:=true;
    mpagesetup.RowHeadings:=false;
    mpagesetup.ColHeadings:=false;
    mpagesetup.PrintArea:='';
    mf1book.SetPageSetup(mpagesetup);
    end;
    //合并单元格
    var cheng:If1cellformat;   
    begin
    cheng:=F1Book61.GetCellFormat;
    cheng.Set_MergeCells(true);
    F1Book61.SetCellFormat(cheng);
    end;
    var cheng: IF1CellFormat;//自动换行
    begin
      cheng:=F1Book61.GetCellFormat;
      cheng.AlignHorizontal:=$00000006;
      F1Book61.SetCellFormat(cheng);
    end;
      

  4.   

    建议采用方案2,从报表设计的方式上说,这是最合理的,也减轻了编程的复杂。用delphi支持的ole调用完全可以满足要求。