用了皮肤,原来写的将StringGrid保存为Excel的过程不能用了》》》
现附上的的代码:
procedure GridSaveToExcel(Grid: TStringGrid);
var
   i, r: integer;
   ExcelApp: texcelapplication;
   Excelbook: texcelworkbook;
   excelsheet: texcelworksheet;
   SaveDialog: TSaveDialog;
begin
   begin
      try
         ExcelApp := TExcelApplication.Create(nil);
         Excelbook := TExcelWorkbook.Create(nil);
         excelsheet := TExcelWorksheet.Create(nil);
      except
         MsgBox('系统没有安装Excel!');
         Exit;
      end;
   end;
   SaveDialog := TSaveDialog.Create(nil);
   SaveDialog.Filter := 'Excel File';
   SaveDialog.FileName := widestring(FormatDateTime('yyyy-MM-dd', now));
   if SaveDialog.Execute then
      begin
         ExcelApp.Connect; //与Excel建立连接
         ExcelApp.Workbooks.Add(null, 0); //添加工作簿
         Excelbook.ConnectTo(ExcelApp.Workbooks[1]);
         excelsheet.ConnectTo(Excelbook.Sheets[1] as _WorkSheet); //与工作表连接
         for i := 0 to grid.RowCount - 1 do //在工作表填写数据
            for r := 0 to grid.ColCount - 1 do
               begin
                  excelsheet.Cells.Item[i + 1, r + 1] := grid.Cells[r, i];
               end;
         try
            Excelbook.SaveCopyAs(SaveDialog.FileName + '.xls');
         except
            MsgBox('该文件正在使用,不能覆盖!', 2);
         end;
         ExcelApp.DisplayAlerts[LOCALE_USER_DEFAULT] := False;
         ExcelApp.Quit;
      end;
   ExcelApp.Disconnect;
   Excelbook.Disconnect;
   excelsheet.Disconnect;
   FreeAndNil(ExcelApp);
   FreeAndNil(Excelbook);
   FreeAndNil(excelsheet);
   FreeAndNil(SaveDialog);
end;

解决方案 »

  1.   

    procedure MsgBox(const s: string; iType: Integer = 0);
    begin
       case iType of
          0: MessageDlg(s, mtInformation, [mbOk], 0); //感叹i
          1: MessageDlg(s, mtConfirmation, [mbOk], 0); //询问?
          2: MessageDlg(s, mtWarning, [mbOk], 0); //警告!
          3: MessageDlg(s, mtError, [mbOk], 0); //出错*
          4: MessageDlg(s, mtCustom, [mbOk], 0); //一般
       end;
    end;
      

  2.   

    跟下代码吧,vclskin好象是会引起dialog方面的问题,我的project里用了vclskin后内嵌的web的下载文件都用不了
      

  3.   

    给你一个其他的,我一直用的,有皮肤也照样的function  sgPrintExcel(Form : TForm ;  sg : TStringGrid;
               sTitleName, sSheetName,myrec ,strGS  :  string) :integer;
    var
       i, j  :integer;
       vare, Sheet, Title  : Variant;
       gslx :string;
       gslAll:Tstringlist ;
       gs :Tstringlist ;
    begin
      if sg.RowCount =0 then
        begin
          application.MessageBox('当前无记录!','提示',64);
          result :=0;
          Exit;
        end;
      try
        vare := CreateOleObject('excel.application');
        vare.Visible := True;
        vare.WorkBooks.Add();
        vare.WorkBooks[1].WorkSheets[1].Name := sTitleName;    Sheet := vare.WorkBooks[1].Worksheets[1];
        Title := Sheet.Rows;
        title.Rows[1].Font.Name := '楷体';
        title.Rows[1].Font.Size := 18;
        title.Rows[1].Font.Bold := True;
        title.Rows[1].Font.Color := clBlue;
        title.Rows[2].Font.Name := '宋体';
        title.Rows[2].Font.Size := 12;
        title.Rows[2].Font.Bold := True;
        title.Rows[2].Font.Color := clBlue;
        Sheet.Cells[1, 1] := sSheetName;
        Sheet.Cells[2, 1] := '打印日期:'+formatdatetime('yyyy年MM月dd日',GetCurrentDate) ;    for i:= 0 to sg.RowCount -1 do
        begin
          for j:=0 to sg.ColCount -1 do
            Sheet.Cells[i+3,j+1] := sg.Cells[j,i];
        end;    gs := SplitString(strgs,';') ;    for j:=0 to gs.Count-1 do
        begin
          gslx := copy(gs[j],1,pos(':',gs[j])-1);
          gslAll := SplitString(copy(gs[j],pos(':',gs[j])+1,length(gs[j])),',') ;
            for i:=0 to gslall.Count -1 do
            begin
              if gslx ='0' then
                  sheet.range[gslAll[i]+'4',gslAll[i]+myrec  ].NumberFormatLocal :='00000000000000' ;
              if gslx ='1' then
                  sheet.range[gslAll[i]+'4',gslAll[i]+myrec  ].NumberFormatLocal :='0.000' ;
            end;    end;    result :=1 ;
       except
         on E: Exception do
         begin
           application.MessageBox (pchar( e.Message),pchar(e.HelpContext ), 64);
           result :=0 ;
         end;
      end;
    end;//调用列子
    if sgPrintExcel(from1 , StringGrid,'',cmbYear.text+'年'+cmbmonth.text+'月收费汇总表',inttostr(sg.RowCount),'1:c,d,e,f,g,h,i,j,k') = 1 then
      begin
        application.MessageBox('输出完成!','提示',64) ;
        exit ;
      end;
      

  4.   

    c,d,e,f,g,h,i,j,k 为excel中列 ,输出时应该与StringGrid列一一对应