如何实现把DBgrid中的数据内容导出?(50分)

  • 主题发起人 Alpinist
  • 开始时间
A

Alpinist

Unregistered / Unconfirmed
GUEST, unregistred user!
我使用Ado连接MS Sql server
用户查询得到的数据显示在DBgrid中
我想把DBgrid中的数据导出为Excel、文本文件或者Word等格式(供用户选择)
但是我直接使用Savetofile( )保存的结果是乱码
请问各位大师要怎么做?
如果只能保存为Excel也可以啊[?][?]
 
我劝你还是保存DBGrid的DataSource的DataSet的内容好一些。
DataSet直接可保存成XML格式。或者你可以自己写一个方法把它保存成文本等等。
 
给段代码你,自已再稍改改吧
procedure ExportExcel(vGrid:TDBGrid;vAds:TDataSet;
vFileName:String;vPBar:TProgressBar);
const
// xlSheetType
xlChart=-4109;
xlDialogSheet=-4116;
xlExcel4IntlMacroSheet=4;
xlExcel4MacroSheet=3;
xlWorkSheet=-4167;
{xlWbaTemplate}
xlWbatChart=-4109;
xlWbatExcel4IntlMacroSheet=4;
xlWbatExcel4MacroSheet=3;
xlWbatWorkSheet=-4167;
begin
//检测文件是否存在
if FileExists(vFileName+'.xls') then
if Msgbox(Pchar('该文件:'+vFileName+'系统中已存在'+Chr(13)+
'点击"是"覆盖,点击"否"中止'),'系统提示',
MB_YesNo+MB_DEFBUTTON1+MB_ICONQUESTION)=IDYES then begin
try
DeleteFile(Pchar(vFileName+'.xls'));
except
end;
end
else
exit;
Try
XLApp:=CreateOleObject('Excel.Application');
XLApp.Workbooks.Add(xlWbatWorkSheet);
XLApp.Workbooks[1].WorkSheets[1].Name:='Sheet1';
XLApp.visible:=True;
except
Msgbox('您的机器里未安装Microsoft Excel。','系统提示',
MB_OK+MB_IconError);
Abort;
end;
Try
vAds.DisableControls;
//设置格式,XLApp.visible:=False时没起作用,改为True试试
if XLApp.visible then begin//设为True时,则进度条不必显示
XLApp.ActiveWindow.DisplayZeros:=False;
XLApp.ActiveWindow.Selection.NumberFormatLocal := '0.00';
XLApp.ActiveWindow.Selection.NumberFormatLocal := 'G/通用格式';
end;
InsertData(vGrid,vAds,vFileName,vPBar);
// HandleRange;
// ChangeColumns;
XLApp.workbooks[1].Worksheets['Sheet1'].Saveas(vFileName);
Msgbox(Pchar('成功写入Excel,文件名:'+vFileName+Chr(13)+
'你可以根据需要进行适当的编辑'),'系统提示',
MB_OK+MB_ICONINFORMATION);
Finally
vAds.EnableControls;
if Not VarIsEmpty(XLApp) then begin
vPBar.Visible:=False;
XLApp.DisplayAlerts:=False;
XLApp.Quit;
XLApp:=Unassigned;
end;
end;
end;
 
//导出到文本
procedure TForm1.Button1Click(Sender: TObject);
var
l_lst_String : TStringList;
i : Integer;
l_String : string;
begin
if not OpenDialog1.Execute then
Exit;
l_lst_String := TStringList.Create;
try
Table1.First;
while not Table1.Eof do
begin
with DBGrid1 do
begin
for i := 0 to Columns.Count - 1 do
begin
if l_String <> '' then
l_String := l_String + ',' + DBGrid1.Columns.Field.AsString
else
l_String := l_String + DBGrid1.Columns.Field.AsString;
end;
l_lst_String.Add(l_String);
l_String := '';
end;
Table1.Next;
end;
l_lst_String.SaveToFile(OpenDialog1.FileName);
finally
l_lst_String.Free;
end;
end;

//将DbGrid的数据导出到Excel
uses comobj, excel97;

procedure TForm1.DBGridToExcel(Multiselect: Boolean; FileNam: string; DBGrid: TDBGrid);
var
MSExcel, MSExcelWorkBook, MSExcelWorkSheet: Variant;
ColumnRange : Variant;
LinesCount, FieldsCount, i, j: Integer;
ExportText : Variant;
SaveCursor : TCursor;
ColumnWidth : array of Integer;
function GetRangeAddr(const ColIndex, RowIndex: Integer): string;
var
i : integer;
begin
if ColIndex > 26 then
Result := Chr((ColIndex div 26) + 64);
i := ColIndex mod 26;
if i > 0 then Result := Result + Chr(i + 64);
Result := Result + IntToStr(RowIndex);
end;
begin
try
MSExcel := CreateOleObject('Excel.Application');
MSExcelWorkBook := MSExcel.WorkBooks.Add;
MSExcelWorkSheet := MSExcel.WorkSheets.Add;
except
ShowMessage('无法与Microsoft Excel连接!');
abort;
end;

try
with DBGrid do
begin
SaveCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
try
FieldsCount := DBGrid.Columns.Count;
with DataSource.DataSet do
begin
DisableControls;
try
LinesCount := 1;
First;
while not Eof do
begin
Inc(LinesCount);
Next;
end;
ExportText := VarArrayCreate([1, LinesCount, 1, FieldsCount], VarVariant); //創建二維數組
LinesCount := 1;
SetLength(ColumnWidth, FieldsCount);
for i := 0 to FieldsCount - 1 do
begin
ExportText[LinesCount, i + 1] := Columns.Title.Caption; //將Columns列标题写到第一行
ColumnWidth := Round(Columns.Width / 64 * 10); //保存Columns列宽以便在Excel中设置列宽
end;
if not MultiSelect then //如果不支持多重选择
begin
First;
while not Eof do
begin
Inc(LinesCount);
for i := 0 to FieldsCount - 1 do
ExportText[LinesCount, i + 1] := DBGrid.Columns.Field.Value;
Next;
end;
end
else
begin //如果支持多重选择
if DBGrid.SelectedRows.Count > 0 then
with DBGrid.DataSource.DataSet do
for i := 0 to DBGrid.SelectedRows.Count - 1 do
begin
GotoBookMark(Pointer(DBGrid.SelectedRows.Items));
Inc(LinesCount);
for j := 0 to FieldsCount - 1 do
ExportText[LinesCount, j + 1] := DBGrid.Columns.Field.Value;
end;
end;
MSExcelWorkSheet.Range['A1:' + GetRangeAddr(FieldsCount, LinesCount)].value := Exporttext;
ColumnRange := MSExcelWorkSheet.Columns;
for i := 1 to FieldsCount do
ColumnRange.Columns.ColumnWidth := ColumnWidth[i - 1]; //设置列宽
MSExcelWorkSheet.Range['A1:' + GetRangeAddr(FieldsCount, LinesCount)].Borders.LineStyle := 1; //設置邊界線
finally
EnableControls;
end;
end;
MSExcelWorkSheet.SaveAs(Filenam);
finally
Screen.Cursor := SaveCursor;
end;
end;
Application.BringToFront; //激活用程序
ShowMessage('所選資料已成功地存檔到'#13 + Filenam);
finally
try
MSExcel.Quit;
except
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DBGridToExcel(False, 'c:/Book1.xls', DbGrid1);
end;
 
我有一个单元,你可以用以下
 
多谢各位

那如果我要从dataset中间导入成excel格式应该怎么样?
 
多人接受答案了。
 
顶部