我用delphi 7做excel程序的导入导出,导入是用ado连接excel实现的,导出是用ole+clipboard实现的,发生如下两个问题:
    一、偶尔弹出‘类rang的pastspecial方法无效’的异常,在区域粘贴时弹出;
    二、导入excel 2007的文件时,弹出‘无法预料的格式’的异常。当前只有连接excel2003的方法,不知道怎么连接excel2007。
    实在不知道怎么处理这两个问题,请各位帮帮忙,谢谢!

解决方案 »

  1.   

    office2007的文件格式与2003的不兼容
      

  2.   

    Excel2007的文件格式已经是基于XML的了
      

  3.   

    Excel2007的格式已经是基于XML的了
      

  4.   

    感谢LZ几位的答复,EXCEL2007的格式已支持。我上了微软的网站,找到了处理方法,只要改变连接字符串为
    'Provider=Microsoft.ACE.OLEDB.12.0;Password="";Data Source='+FileName+';Extended Properties=''Excel 12.0;IMEX=1;HDR=YES'';Persist Security Info=True;'
    就可以实现对2007的读取。现在还有问题一无法得到控制,不知道有没有什么办法可以避免这种情况的发生。
      

  5.   

    还是换Excel03用吧,技术比较成熟,O7的格式都不一样了xlsx.
      

  6.   

    当前已改为ADO连接OLE DB的方式处理,毕竟OLE DB是MS出品,出问题的机率应该会大大降低吧~
      

  7.   

    导入导出都以连接OLE DB处理,应该比剪贴板稳定得多,只是导出的速度慢了很多
      

  8.   

    如果你想快速的导出,建议参考
    http://www.delphifans.com/infoView/Article_205.html但缺点是导出的文件是文本格式的,不可再当做源文件导入数据库.还有就是用OLE DB处理,但速度真的很慢啊,绝对不忽悠~~~~给你一段代码参考,支持多Sheet.Function TFrmUIPes0030315.DataToExcel(sDBGrid: TDBGrid;  Fn: String):Boolean;
    Var
      ExcelApp : Variant;
      i,j,k,m  : Integer;
      sColStr : String;
      S       : String;
      StringList :TStringList;
    Begin
      Result :=False;  StringList :=TStringList.Create;  Try
        Try
          ExcelApp :=CreateOleObject('Excel.Application');
        Except
          Application.MessageBox('系统中的MS Excel软件没有安装或安装不正确!','错误',MB_ICONERROR+MB_OK);
          Exit;
        End;
        ExcelApp.Visible :=False;
        ExcelApp.WorkBooks.Add;    sDBGrid.DataSource.DataSet.DisableControls;
        sDBGrid.DataSource.DataSet.First;
        While Not sDBGrid.DataSource.DataSet.Eof Do Begin
          if StringList.IndexOf(sDBGrid.DataSource.DataSet.FieldByName('GRP_NM').AsString)=-1 then
              StringList.Add(sDBGrid.DataSource.DataSet.FieldByName('GRP_NM').AsString);      sDBGrid.DataSource.DataSet.Next;
        End;
        sDBGrid.DataSource.DataSet.EnableControls;    sColStr:=Chr(65+sDBGrid.FieldCount-1);
        for i := 1 to StringList.Count  do  Begin
          For j :=1 To sDBGrid.FieldCount-1 Do Begin
            //各字段的宽度
            ExcelApp.WorkSheets[i].Columns[j].ColumnWidth :=sDBGrid.Columns[j].Width*0.15;
            //各字段标题
            ExcelApp.WorkSheets[i].Cells[1,j].Value :=sDBGrid.Columns[j].Title.Caption;
            ExcelApp.WorkSheets[i].Cells[1,j].HorizonTalAlignment:=3;     //标题水平居中
          End;
          ExcelApp.WorkSheets[i].Name :=StringList[i-1];
          ExcelApp.WorkSheets[i].Range['A1:'+sColStr+'1'].Font.Name :='宋体';
         // ExcelApp.WorkSheets[i].Range['A1:'+sColStr+'1'].Font.Size :=16;
          ExcelApp.WorkSheets[i].Range['A1:'+sColStr+'1'].Font.Bold :=True;
          ExcelApp.WorkSheets[i].Range['A1:'+sColStr+'1'].Font.Size :=10;      if not sEmpty then
          begin
            m :=2;
            sDBGrid.DataSource.DataSet.DisableControls;
            sDBGrid.DataSource.DataSet.First;
            While Not sDBGrid.DataSource.DataSet.Eof Do Begin
              if sDBGrid.DataSource.DataSet.FieldByName('GRP_NM').AsString=StringList[i-1] then
              Begin
                for k := 1 to sDBGrid.FieldCount-1 do begin
                  ExcelApp.WorkSheets[i].Cells[m,k].Value :=''''+sDBGrid.Fields[k].AsString;
                end;
                m :=m+1;
                S :='A2:'+sColStr+IntToStr(m-1);
              End;
              sDBGrid.DataSource.DataSet.Next;
            End;
            sDBGrid.DataSource.DataSet.EnableControls;
            sDBGrid.DataSource.DataSet.First;
          ExcelApp.WorkSheets[i].Range[s].VerticalAlignment:=$FFFFEFF4;
          ExcelApp.WorkSheets[i].Range[s].Font.Name :='宋体';
          ExcelApp.WorkSheets[i].Range[s].Font.Size :=10;
          ExcelApp.WorkSheets[i].Range[s].Borders.LineStyle :=1;
          ExcelApp.ActiveSheet.PageSetUp.RightMargin :=0.5/0.035;
          ExcelApp.ActiveSheet.PageSetUp.LeftMargin  :=2/0.035;
          ExcelApp.ActiveSheet.PageSetUp.BottomMargin:=0.5/0.035;
          end;      ExcelApp.Selection.Columns.AutoFit;             //自适应宽度
          //ExcelApp.WorkSheets[i].Protect;
        End;
       // ExcelApp.Visible :=True;
        Try
          ExcelApp.ActiveWorkBook.SaveAs(Fn);
        Except    End;  Finally
      //  ExcelApp.DisplayAlerts :=False;
        ExcelApp.Quit;
        VarClear(ExcelApp);
      End;  Result :=True;end;