excel导入导出操作comobj;

procedure TForm1.N1Click(Sender: TObject);
var
  ExcelApp:Variant;
  expath,str:String;
  ExCountRow,ExCountCol,i,j:integer;
begin
  if dlgOpen1.Execute then
  begin
    expath:=dlgOpen1.FileName;
    application.ProcessMessages;
    try


        ExcelApp:=CreateOleObject('excel.application');
        ExcelApp.visible:=false;
        ExcelApp.workbooks.open(expath);
        ExCountRow:=0;
        while not(length(ExcelApp.cells[ExCountRow+1,1])=0) do
          begin
            Inc(ExCountRow);
          end;


      AdvStringGrid1.RowCount:=ExCountRow-1;
        for i:=1 to AdvStringGrid1.RowCount do
        begin
          for j:=1 to AdvStringGrid1.ColCount do
          begin
            AdvStringGrid1.Cells[j-1,i-1]:=ExcelApp.cells[i+1,j].value;
           end;
        end;


      ExcelApp.workbooks.close;
      Excelapp.quit;
    finally
      ExcelApp.workbooks.close;
      Excelapp.quit;
    end;
  end;

end;




procedure TForm1.N2Click(Sender: TObject);
var
  ExportFile: String;
  i: Integer;
  FExcel : Variant;
begin
  if Application.MessageBox('是否确定导出?','提示',MB_OK or MB_OKCANCEL) <> id_ok then exit;
  Try
    FExcel := CreateOleObject('Excel.Application');
    FExcel.Visible := True;
    ExportFile := extractfilepath(application.ExeName) + '1.xls' ;
    FExcel.Workbooks.add(ExportFile);

    for i := 1 to sg3.RowCount do
    begin
      for j := 0 to sg3.ColCount - 1 do
        FExcel.Workbooks[1].sheets[1].cells(i,j+1) := sg3.Cells[j,i-1] ;

    end;


    OR

    for i := 1 to AdvStringGrid1.RowCount do
    begin
      FExcel.Workbooks[1].sheets[1].cells(i,1) := AdvStringGrid1.Cells[0,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,2) := AdvStringGrid1.Cells[1,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,3) := AdvStringGrid1.Cells[2,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,4) := AdvStringGrid1.Cells[3,i-1] ;


      FExcel.Workbooks[1].sheets[1].cells(i,5) := AdvStringGrid1.Cells[4,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,6) := AdvStringGrid1.Cells[5,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,7) := AdvStringGrid1.Cells[6,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,8) := AdvStringGrid1.Cells[7,i-1] ;


      FExcel.Workbooks[1].sheets[1].cells(i,9) := AdvStringGrid1.Cells[8,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,10) := AdvStringGrid1.Cells[9,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,11) := AdvStringGrid1.Cells[10,i-1] ;
      FExcel.Workbooks[1].sheets[1].cells(i,12) := AdvStringGrid1.Cells[11,i-1] ;
    end;
    ShowMessage('输出完毕!');
  Except
    ShowMessage('发生未知的错误,可能是您的电脑尚未安装电子表格软件!');
  End;
end;



procedure SgToExcel_txt(AdvSg:TAdvStringGrid); 
var
  ExportFile: String;
  i,j: Integer;
  FExcel: Variant; // 表格中的内容可以输出到电子表格中去
  str : String ;
begin
  Try
    FExcel := CreateOleObject('Excel.Application');
    FExcel.Visible := True;
    ExportFile := ExtractFilePath(ExeOrDllPath) + 'output.xls' ;
    FExcel.Workbooks.add(ExportFile);
    FExcel.Workbooks[1].sheets[1].Cells.NumberFormat := '@ ';
    Str := '';
    for i := 0 to AdvSg.RowCount - 1 do
    begin
      for j := AdvSg.FixedCols to AdvSg.ColCount - 2 do
      begin
         Str := Str +  AdvSg.Cells[j,i] + #9 ;
      end;
      Str :=  Str + AdvSg.Cells[AdvSg.ColCount -1,i] + #13 ;
    end;


    ClipBoard.AsText := Str ;
    if ClipBoard.HasFormat(CF_TEXT) then
      FExcel.Workbooks[1].sheets[1].Paste;
    FExcel.Workbooks[1].sheets[1].Columns.AutoFit;
    FExcel.Workbooks[1].sheets[1].Cells[1,1].select;
    //ClipBoard.Clear;
  Except
    ShowError('发生未知的错误,可能是您的电脑尚未安装电子表格软件!');
  End;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值