怎样在程序里,新建一个excel文件,然后把查询结构保存到excel文件里,然后保存这个文件(指定文件名)。多谢大家帮忙啊!~

解决方案 »

  1.   

    你看看吧,可以直接导出。 
    var
    i,j:Integer;
    begin
      with ExcelApplication1 do
      begin
        Disconnect;
        Visible[0]:=True;
        SheetsInNewWorkbook[0]:=1;
        //增加空白文档
        WorkBooks.Add(EmptyParam,0);
        ExcelWorkBook1.ConnectTo(Workbooks[Workbooks.Count] as _Workbook);
      end;
      with ExcelWorkSheet1 do
      begin
        ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
        Name:='退换货物台帐';
        //设计页边距
        with PageSetup do
        //begin
          //HeaderMargin := 1;
        //nd;
      end;
      j:=1;//计数器,记下现在的行数
      with table1 do
      //导出表头
      begin
        if checkbox1.Checked then
        begin
          with ExcelWorkSheet1 do
          begin
            for i:=1 to Fields.Count do
              Cells.Item[1,i]:=Fields[i-1].DisplayName;
            //设置表头的字体加粗
            with Range[Cells.Item[1,1],Cells.Item[1,Fields.Count]] do
            begin
              Font.Bold := 1;
            end;
          end;
          Inc(j);
        end;    First;
        While not Eof do
        begin
          for i := 1 to Fields.Count do
          begin
            ExcelWorkSheet1.Cells.Item[j,i] := Fields[i-1].Value;
          end;
          next;
          Inc(j);
        end;
      end;
    end;
      

  2.   

    var
      Sheet:Variant;
      Excel: Variant;
      ExcelSheet: Variant;
      Worker: TProgram;
    begin
      Worker.CreateExcel(str1 , str2, Excel, ExcelSheet)
      Sheet:= Fexcel.Workbooks[1].WorkSheets[1] ;
      Sheet.cells[2,'H'].Value:=                //赋值
       ..........
    end;