如何实现excel,word和DbGrid数据的互相导入导出(10分)

  • 主题发起人 主题发起人 青云
  • 开始时间 开始时间

青云

Unregistered / Unconfirmed
GUEST, unregistred user!
如何把excel文件输出到DBGrid?
这个问题我已经解决,方法如下:
{
功能描述:把DBGrid输出到Excel表格(支持多Sheet)
调用格式:CopyDbDataToExcel([DBGrid1, DBGrid2]);
}
procedure CopyDbDataToExcel(Args: array of const);
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;
I: Integer;
begin
Screen.Cursor := crHourGlass;
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
VarClear(XLApp);
end;

try
XLApp := CreateOleObject('Excel.Application');
except
application.MessageBox('系统没有安装excel','系统提示',mb_ok+mb_iconinformation);
Screen.Cursor := crDefault;
Exit;
end;

XLApp.WorkBooks.Add;
XLApp.SheetsInNewWorkbook := High(Args) + 1;
for I := Low(Args) to High(Args) do
begin
XLApp.WorkBooks[1].WorkSheets[I + 1].Name := TDBGrid(Args.VObject).Name;
Sheet := XLApp.Workbooks[1].WorkSheets[TDBGrid(Args.VObject).Name];

if not TDBGrid(Args.VObject).DataSource.DataSet.Active then
begin
Screen.Cursor := crDefault;
Exit;
end;
TDBGrid(Args.VObject).DataSource.DataSet.First;
for iCount := 0 to TDBGrid(Args.VObject).Columns.Count - 1 do
Sheet.Cells[1, iCount + 1] :=
TDBGrid(Args.VObject).Columns.Items[iCount].Title.Caption;

jCount := 1;
while not TDBGrid(Args.VObject).DataSource.DataSet.EOF do
begin
for iCount := 0 to TDBGrid(Args.VObject).Columns.Count - 1 do
Sheet.Cells[jCount + 1, iCount + 1] :=
TDBGrid(Args.VObject).Columns.Items[iCount].Field.AsString;
Inc(jCount);
TDBGrid(Args.VObject).DataSource.DataSet.Next;
end;
XlApp.Visible := True;
end;
Screen.Cursor := crDefault;
end;

===========
注意 :uses ComObj;
调用方法:
CopyDbDataToExcel([self.DBGrid1]);



那么,如何把excel文件输入到DBGrid? 这个我不会,谁能告诉我?

还有,word和DbGrid的互相导入又是怎样做的呢?


现学现用,我做了一个小工具,可以把DbGrid的数据输入到Excel
http://www.yzsoftware.com/bbs/show.asp?id=264&bd=90&totable=1



 
后退
顶部