求助:
非常非常急,各位大哥请给个把 DBGRID 里数据导入到 excel 的例子,好吗?
谢谢!
非常非常急,各位大哥请给个把 DBGRID 里数据导入到 excel 的例子,好吗?
谢谢!
解决方案 »
- 新手的提问,谢谢大家啦
- 从表读到treeview显示,如何点击树的节点后在dbgrid显示对应的记录
- 提示:应用程序在当前操作中使用了错误类型的值。这怎么弄了呢?
- 如何解决在VISTA系统下继续运行BDE连接SQLSERVER的程序的问题!!!!谢谢
- 凑热闹,恭喜 cc 也祝:没有的赶快找,找到的赶紧办事,已经办事的 赶紧要孩子
- 新手,问个问题
- 支持 一下 我9:10 揭贴 给分
- 请大家继续关注:用delphi做OA问题专辑(1000分)(一):MTS,COM+,DCOM定义
- 我用delphi写了一个很简单的com组件,在delphi中测试是成功的,但怎么...
- 在richedit中当MOUSE按下时怎样让光标焦点定位到起始位置?
- 定时长取数
- 如何添加IE右键菜单项
ReportCaption, ReportMemo, ReportTtl: string; AddChart: Boolean);
const
CLASS_ExcelApplication: TGUID = '{00024500-0000-0000-C000-000000000046}';
var
ExcelApp, Sheet, Chart: OleVariant;
Unknown: IUnknown;
BM: TBookStr;
Col, Row: Integer;
I: Integer;
begin
if (Grid.DataSource <> nil) and (Grid.DataSource.DataSet <> nil) then
with Grid.DataSource.DataSet do
begin
try
if not Succeeded(GetActiveObject(CLASS_ExcelApplication, nil, Unknown)) then
Unknown := CreateComObject(CLASS_ExcelApplication);
except
raise Exception.Create('不能启动 Microsoft Excel,请确认 Microsoft Excel 已正确安装在本机上');
end;
ExcelApp := Unknown as IDispatch;
ExcelApp.Visible := True;
ExcelApp.Workbooks.Add;
if DisableScreenUpdating then
ExcelApp.ScreenUpdating := False;
DisableControls;
try
BM := Book;
First;
Row := 4;
Col := 1;
for I := 0 to Grid.Columns.Count - 1 do
begin
if Grid.Columns[I].Visible then
ExcelApp.Cells[Row, Col] := Grid.Columns[I].Title.Caption;
Inc(Col);
end;
Inc(Row);
while not Eof do
begin
Col := 1;
for I := 0 to Grid.Columns.Count - 1 do
begin
if Grid.Columns[I].Visible then
if Grid.Columns[I].Field <> nil then
ExcelApp.Cells[Row, Col] := Grid.Columns[I].Field.DisplayText;
Inc(Col);
end;
Inc(Row);
Next;
end;
Col := 1;
for I := 0 to Grid.Columns.Count - 1 do
begin
if Grid.Columns[I].Visible then
ExcelApp.Columns[Col].AutoFit; ;
Inc(Col);
end;
ExcelApp.Cells[1, 1] := ReportCaption;
ExcelApp.Cells[2, 1] := ReportMemo;
ExcelApp.Cells[3, 1] := ReportTtl;
Book := BM;
finally
EnableControls;
if not ExcelApp.ScreenUpdating then
ExcelApp.ScreenUpdating := True;
end;
end;
if AddChart then
begin
Sheet := ExcelApp.ActiveSheet;
Sheet.Range[Sheet.Cells[4, 1], Sheet.Cells[Row - 1, Col - 1]].Select;
Chart := ExcelApp.ActiveWorkbook.Charts.Add;
Chart.SetSourceData(
Sheet.Range[Sheet.Cells[4, 1], Sheet.Cells[Row - 1, Col - 1]], EmptyParam);
Chart.Location(1, EmptyParam);
end;
ExcelApp.ActiveWorkbook.PrintPreview;
end;