我将别人帮忙写的一段由DBGrid导出数据到EXCLE中的语句复制到我的,程序里面。但是复制后开头的第一句procedure TFrmMain.DBGridSaveXLS(aDBGrid: TDBGrid; sFileName: string);就报错说orocedure初始化错误。请见图片。 还有我如何用一个按钮调用这段程序,使我点击按钮就会执行导出数据功能?
解决方案 »
- 如何用delphi操作Excel自动填充?
- 单纯的散分...周日晚上结贴...
- delphi中如何将一个access表的数据导入到另一个表中?
- 求英文文献中英对照翻译的文档
- 关于TclientDataSet的问题
- 从access导入多个表的数据到sql server.
- 我下载了一个16*16的字库,通过区位码怎样读里面的点阵内容
- 请问Delphi有没有多态?
- Canvans 的拉伸显示到Printer 是不是问题多多(今天我没有打印出来),推荐一个好的“打印图片”的控件好呢?
- ??怎么换斑竹了?
- 关于结构体数组的传递问题,急。。。。,DLL
- W1033 Unit 'GridsEh' implicitly imported into package 'EhLib110'
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids;type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure DBGridSaveXLS(aDBGrid: TDBGrid; sFileName: string);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}uses DB, Comobj;procedure TForm1.DBGridSaveXLS(aDBGrid: TDBGrid; sFileName: string);
function LineFeedsToXLS(s:string):string;
var
Res: string;
i: Integer;
begin
Res := '';
for i := 1 to Length(s) do
if s[i] <> #13 then
Res := Res + s[i];
Result:=res;
end;
var
FExcel: Variant;
FWorkbook: Variant;
FWorksheet: Variant;
FArray: Variant;
s, z: Integer;
RangeStr, sTitle: string;
aBookMark: TBookMark;
StrtCol, StrtRow, RowCount, ColCount: Integer;
begin
Screen.Cursor := crHourGlass; try
FExcel := CreateOleObject('excel.application');
except
Screen.cursor := crDefault;
MessageDlg('Could not start Microsoft Excel!', mtError, [mbCancel], 0);
Exit;
end; aBookMark := aDBGrid.DataSource.DataSet.GetBookMark;
aDBGrid.DataSource.DataSet.DisableControls;
try
StrtCol := 0;
StrtRow := 0;
FWorkBook := FExcel.WorkBooks.Add;
//FWorkSheet := FWorkBook.WorkSheets.Add;
FWorkSheet := FExcel.WorkBooks[1].WorkSheets[1];
RowCount := aDBGrid.DataSource.DataSet.RecordCount + 1;//加上標題行
ColCount := aDBGrid.Columns.Count;
FArray := VarArrayCreate([0, RowCount - 1 - StrtRow, 0, ColCount - 1 - StrtCol], VarVariant); //Title
for z := StrtCol to ColCount - 1 do
begin
sTitle := aDBGrid.Columns[z].Title.Caption;
if sTitle = '' then
sTitle := aDBGrid.Columns[z].FieldName;
FArray[0, z - StrtCol] := LineFeedsToXLS(sTitle);
end; //data
{for s := StrtRow to RowCount - 1 do
for z := StrtCol to ColCount - 1 do
FArray[s - StrtRow, z - StrtCol] := LineFeedsToXLS();}
s := 1;//s := StrtRow;
aDBGrid.DataSource.DataSet.First;
while not aDBGrid.DataSource.DataSet.Eof do
begin
for z := StrtCol to ColCount - 1 do
FArray[s - StrtRow, z - StrtCol] := LineFeedsToXLS(aDBGrid.Columns[z].Field.DisplayText);
Inc(s);
aDBGrid.DataSource.DataSet.Next;
end; RangeStr := 'A1:'; if (ColCount - StrtCol) > 26 then
begin
if (ColCount - StrtCol) mod 26 = 0 then
begin
RangeStr := RangeStr + Chr(Ord('A') - 2 + ((ColCount - StrtCol) div 26));
RangeStr := RangeStr + 'Z';
end
else
begin
RangeStr := RangeStr + Chr(Ord('A') - 1 + ((ColCount - StrtCol) div 26));
RangeStr := RangeStr + Chr(Ord('A') - 1 + ((ColCount - StrtCol) mod 26));
end;
end
else
RangeStr := RangeStr + Chr(Ord('A') - 1 + (ColCount - StrtCol)); RangeStr := RangeStr + IntToStr(RowCount - StrtRow); FWorkSheet.Range[RangeStr].Value := FArray; if sFileName <> '' then
begin
FWorkbook.SaveAs(sFileName);
FExcel.Quit;
FExcel := unAssigned;
end
else
FExcel.Visible := True;
finally
aDBGrid.DataSource.DataSet.GotoBookMark(aBookMark);
aDBGrid.DataSource.DataSet.EnableControls;
aDBGrid.DataSource.DataSet.FreeBookMark(aBookMark);
Screen.Cursor := crDefault;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
DBGridSaveXLS(DBGrid1, '');
end;end.
你好,我写好后运行报错“this file could not accessed" , 你能解释一下这段语句吗?那段时保存路径,那段时保存文件名等? 因为是菜鸟,实在看不懂,谢谢
unit DBGrid2Excel;interfaceuses
Windows, Variants, Classes, SysUtils, Forms, DB, DBGrids, ComObj;type
TUpAniInfoProc = procedure (const sInfo: string;Position,FullNum: Integer) of object; function DBGridToExcel(dgrSource: TDBGrid;
UpAniInfo: TUpAniInfoProc = nil; SaveFile: String = 'XyBook1.xls'): Integer;implementation
const
MAX_SHEET_ROWS = 65536-1; //Excel每Sheet最大行数
MAX_VAR_ONCE = 1000; //一次导出的条数
function DBGridToExcel(dgrSource: TDBGrid; UpAniInfo: TUpAniInfoProc; SaveFile: String): Integer;
var //从DBGrid导出到Excel(改进至可以导入几乎无限的数据)
MyExcel, varCells: Variant;
MySheet, MyCells, Cell1, Cell2, Range: OleVariant;
iRow, iCol, iRealCol, iSheetIdx, iVarCount, iCurRow, iFieldCount: integer;
CurPos: TBook;
DataSet: TDataSet;
sFieldName: string;
begin //返回导出记录条数
DataSet := dgrSource.DataSource.DataSet; DataSet.DisableControls;
CurPos := DataSet.GetBook;
DataSet.First; MyExcel := CreateOleObject('Excel.Application');
MyExcel.WorkBooks.Add;
MyExcel.Visible := False; if DataSet.RecordCount <= MAX_VAR_ONCE then
iVarCount := DataSet.RecordCount
else
iVarCount := MAX_VAR_ONCE; iFieldCount := dgrSource.Columns.Count; //对DBGrid,只导出显示的列
for iCol:=0 to dgrSource.Columns.Count-1 do
if not dgrSource.Columns[iCol].Visible then //可能有不显示的列
Dec(iFieldCount);
varCells := VarArrayCreate([1,
iVarCount,
1,
iFieldCount], varVariant);
iSheetIdx := 1;
iRow := 0;
Result := 0;
while not DataSet.Eof do
begin
if (iRow = 0) or (iRow > MAX_SHEET_ROWS + 1) then
begin //新增一个Sheet
if iSheetIdx <= MyExcel.WorkBooks[1].WorkSheets.Count then
MySheet := MyExcel.WorkBooks[1].WorkSheets[iSheetIdx]
else
MySheet := MyExcel.WorkBooks[1].WorkSheets.Add(NULL, MySheet);//加在后面
MyCells := MySheet.Cells;
Inc(iSheetIdx);
iRow := 1; iRealCol := 0;
for iCol := 1 to iFieldCount do
begin
MySheet.Cells[1, iCol].Font.Bold := True;
{MySheet.Select;
MySheet.Cells[iRow,iCol].Select;
MyExcel.Selection.Font.Bold := true;}//这种方法也可(Sheet.Select不可少)
while not dgrSource.Columns[iRealCol].Visible do
Inc(iRealCol); //跳过不可见的列
MySheet.Cells[1, iCol] := dgrSource.Columns[iRealCol].Title.Caption;
MySheet.Columns[iCol].ColumnWidth := //以下方法似乎算得还行
Integer(Round(dgrSource.Columns[iRealCol].Width * 2
/ abs(dgrSource.Font.Height)));
sFieldName := dgrSource.Columns[iRealCol].FieldName;
if (DataSet.FieldByName(sFieldName).DataType = ftString)
or (DataSet.FieldByName(sFieldName).DataType = ftWideString) then
begin //对于“字符串”型数据则设Excel单元格为“文本”型
MySheet.Columns[iCol].NumberFormatLocal := '@';
end;
Inc(iRealCol);
end;
Inc(iRow);
end;
iCurRow := 1;
while not DataSet.Eof do
begin
iRealCol := 0;
for iCol := 1 to iFieldCount do
begin
while not dgrSource.Columns[iRealCol].Visible do
Inc(iRealCol); //跳过不可见的列
sFieldName := dgrSource.Columns[iRealCol].FieldName;
varCells[iCurRow, iCol] := DataSet.FieldByName(sFieldName).AsString;
Inc(iRealCol);
end;
Inc(iRow);
Inc(iCurRow);
Inc(Result);
DataSet.Next;
if (iCurRow > iVarCount) or (iRow > MAX_SHEET_ROWS + 1) then
begin
if Assigned(UpAniInfo) then
UpAniInfo(Format('(已导出%d条,共%d条)', [Result, DataSet.RecordCount]),Result, DataSet.RecordCount); //显示已导出条数
Application.ProcessMessages;
Break;
end;
end;
Cell1 := MyCells.Item[iRow - iCurRow + 1, 1];
Cell2 := MyCells.Item[iRow - 1,
iFieldCount];
Range := MySheet.Range[Cell1 ,Cell2];
Range.Value := varCells;
if (iRow > MAX_SHEET_ROWS + 1) then //一个Sheet导出结束
begin
MySheet.Select;
MySheet.Cells[1, 1].Select; //使得每一Sheet均定位在第一格
end;
Cell1 := Unassigned;
Cell2 := Unassigned;
Range := Unassigned; end; MyCells := Unassigned;
varCells := Unassigned;
MyExcel.WorkBooks[1].WorkSheets[1].Select; //必须先选Sheet
MyExcel.WorkBooks[1].WorkSheets[1].Cells[1,1].Select;
MyExcel.Visible := False;
// MyExcel.WorkBooks[1].Saved := True;
MyExcel.DisplayAlerts:= False;
MyExcel.WorkBooks[1].SaveAs(SaveFile);
// MyExcel.WorkBooks[1].SaveCopyAs(SaveFile);
//// 调用Excel另存新档功能
//// MyExcel.Application.CommandBars.FindControl(ID:=748).Execute;
MyExcel.Quit;
MyExcel:= Unassigned;
if CurPos <> nil then
begin
DataSet.GotoBook(CurPos);
DataSet.FreeBook(CurPos);
end;
DataSet.EnableControls;
end;end.
保存成同名PAS文件在你的DBGrid所在窗体加上
procedure TFormData.UpdateAniInfo(const sInfo: string;Position,FullNum: Integer);
begin //更新动画提示信息
Label5.Caption := sInfo; //在PanelWaiting中放一个TLabel,取名LabelWaiting
ProgressBar1.Max:= FullNum;
ProgressBar1.Position:= Position;
if Position>=FullNum-1000 then
begin
ProgressBar1.Position:= 0;
Label5.Caption:= '导出完毕!';
Panel1.Visible:= False;
end;
Panel1.Update; //在窗体中央放一个TPanel,取名PanelWaiting
end;
调用
procedure TFormData.DataOutBtnClick(Sender: TObject);
var
SaveFile: String;
begin
SaveDialog1.FileName:= ChangeFileExt(ExtractFileName(ComboBox1.Text),'');//这个地方自己改一下
if SaveDialog1.Execute then
begin
SaveFile:= SaveDialog1.FileName;
Panel1.Visible:= True;
DBGrid2Excel.DBGridToExcel(DBGrid1, UpdateAniInfo, SaveFile);
end;
end;
FWorkSheet.Range[RangeStr].Value:=FArray;
这2句中说worksheets 和value没有定义。 请问是什么原因?
谢谢