百度空间 | 百度首页 
 
查看文章
 
將StringGrid中的數據導入到Excel表
2008-12-06 13:26
這個可是我自己的代碼.今天做的時候順便記下.雖然這些都是很初級的東西.最近懶的很,除了工作,也不愿意動腦筋學習.(反面人物,大家別攻擊我啊~~).

   下面的是一個菜單事件,雖然可以寫成一個函數,但是,我懶得整理.相信看的人也應該都會的啊...是吧?

procedure TForm1.SaveToEx1Click(Sender: TObject);
var
   ExcelApp,xlSheet : Variant;
   i,j: integer;
   FileNM : string;
begin
   try
     ExcelApp := CreateOleObject('Excel.Application');
     xlsheet := CreateOleObject('Excel.Sheet');

   Except
     ShowMessage('請檢查您的機器有沒有安裝Excel!');
     ExcelApp.Quit;
     ExcelApp := Unassigned;
   end;

   try
      xlSheet := ExcelApp.WorkBooks.Add;

     for i := 1   to StringGrid1.ColCount do
     begin
       for j := 1 to StringGrid1.RowCount do
       xlSheet.WorkSheets[1].Cells[j, i] := StringGrid1.Cells[i - 1,j - 1];
     end;
     if SaveDialog1.Execute then
       FileNM := SaveDialog1.FileName;
     xlSheet.SaveAs(FileNm);
     ShowMessage('導出數據完成!');
   finally
     xlSheet.Close;
     ExcelApp.Quit;
     ExcelApp := UnAssigned;
   end;
end;

http://hi.baidu.com/yueyun889/blog/item/0dd9848fc8dfcaf8503d9275.html


类别:delphi园地 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu