请各位高手帮我解决这个问题。
解决方案 »
- dbgrid 如何删除多条记录
- 大家帮忙看看这样的表 用什么表格控件能实现 dbgrideh cxgrid 能实现吗?
- 为什么保存到SQL 2000后台数据库,日期的格式会不一致呢?
- 请问怎样让TryStrToDatetime('1999年1月1日',D)返回True?
- 高手请进来,看看这条查询语句错在哪了?
- @@@@@诚招重庆籍DELPHI程序员,有意请联系[email protected]
- 好象被宝兰愚弄
- 寻人开发智能出卷系统
- 哪位有dos622的xcopy 请E:[email protected]急用!!!!!!!!
- 有关adoquery
- 用什么办法可以打开后缀为dat的文件,注:这不是影响文件,只是存储了数据,但一般工具打开就是乱码,最好能告诉我在哪儿能下载工具
- 关于自定义表格的打印问题.请看..
下面给出用到的方法:
//注意:下面的方法必须包含 ComObj, Excel97 单元
//-----------------------------------------------------------
// if toExcel = false, export dbgrid contents to the Clipboard
// if toExcel = true, export dbgrid to Microsoft Excel
procedure ExportDBGrid(toExcel: Boolean);
var
bm: TBook;
col, row: Integer;
sline: String;
mem: TMemo;
ExcelApp: Variant;
begin
Screen.Cursor := crHourglass;
DBGrid1.DataSource.DataSet.DisableControls;
bm := DBGrid1.DataSource.DataSet.GetBook;
DBGrid1.DataSource.DataSet.First;
// create the Excel object
if toExcel then
begin
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.WorkBooks.Add(xlWBatWorkSheet);
ExcelApp.WorkBooks[1].WorkSheets[1].Name := 'Grid Data';
end;
// First we send the data to a memo
// works faster than doing it directly to Excel
mem := TMemo.Create(Self);
mem.Visible := false;
mem.Parent := MainForm;
mem.Clear;
sline := '';
// add the info for the column names
for col := 0 to DBGrid1.FieldCount-1 do
sline := sline + DBGrid1.Fields[col].DisplayLabel + #9;
mem.Lines.Add(sline);
// get the data into the memo
for row := 0 to DBGrid1.DataSource.DataSet.RecordCount-1 do
begin
sline := '';
for col := 0 to DBGrid1.FieldCount-1 do
sline := sline + DBGrid1.Fields[col].AsString + #9;
mem.Lines.Add(sline);
DBGrid1.DataSource.DataSet.Next;
end;
// we copy the data to the clipboard
mem.SelectAll;
mem.CopyToClipboard;
// if needed, send it to Excel
// if not, we already have it in the clipboard
if toExcel then
begin
ExcelApp.Workbooks[1].WorkSheets['Grid Data'].Paste;
ExcelApp.Visible := true;
end;
FreeAndNil(mem);
// FreeAndNil(ExcelApp);
DBGrid1.DataSource.DataSet.GotoBook(bm);
DBGrid1.DataSource.DataSet.FreeBook(bm);
DBGrid1.DataSource.DataSet.EnableControls;
Screen.Cursor := crDefault;
end;
var
eclApp,WorkBook:Variant;
xlsFileName:string;
L: Integer;
s, lang:string;
i,j:integer;
jj:integer;
xssz:array [0..100] of integer;
xssm:array [0..100] of string;
k:integer;
kk:integer;
beginif form_gchhs.s.Execute then
begin
xlsFileName:= form_gchhs.s.FileName;
try
//创建OLE对象Excel Application与 WorkBook
eclApp:=CreateOLEObject('Excel.Application');
WorkBook:=CreateOLEObject('Excel.Sheet');
except
ShowMessage('您的机器里未安装Microsoft Excel。');
Exit;
end;
for i:=0 to 100 do
begin
xssz[i]:=0;
xssm[i]:='';
end;
k:=-1;
l:=1;
workBook:=eclApp.workBooks.Add;
for i:=0 to grid.Columns.Count -1 do
begin
if grid.Columns[i].visible=true then
begin
k:=k+1;
xssz[k]:=i;
for kk:=0 to qu.FieldCount -1 do
begin
if qu.Fields[kk].fieldname=grid.columns[i].fieldname then
xssz[k]:=kk;
end;
xssm[k]:=grid.columns[i].title.caption;
eclApp.Cells(l , k+1):=xssm[k];
end;
end;
with qu do
begin
first;
while not qu.eof do
begin
l:=l+1;
for j:=0 to k do
eclApp.Cells(l , j+1):=fields[xssz[j]].AsString;
qu.next;
end;
end;
WorkBook.saveas(xlsFileName);
WorkBook.Close;
eclApp.Quit;
//退出Excel Application
//释放VARIANT变量
//eclApp:=Unassigned;
ShowMessage('文件已保存!');
end;
end;