C
colorow
Unregistered / Unconfirmed
GUEST, unregistred user!
环境:Delphi7+MS SQL Server 2000+Excel 2003
目的:取出数据库中的一行数据,Excel第一列显示各字段名称,第二列显示相应字段值。
问题:导出的Excel里面中文均显示成乱码,编码规则不清楚(如“受检者号码”显示成“”ÊܼìÕߺÅÂë)。
目前的代码:
procedure proExportExcel(DataSource: TDataSource;
AOwner: TComponent);
var
Bookmark: TBookmark;
Col: Integer;
//Row: Integer;
Str: String;
Memo: TMemo;
Ole: OleVariant;
begin
DataSource.DataSet.DisableControls;
Bookmark:= DataSource.DataSet.GetBookmark;
DataSource.DataSet.First;
Ole:= CreateOleObject('Excel.Application');
Ole.WorkBooks.Add;
Ole.WorkBooks[1].WorkSheets[1].Name:= 'Untitled';
Memo:= TMemo.Create(AOwner);
Memo.Visible:= true;
Memo.Parent:= f_questionnaire;
Memo.Clear;
{Str:= '';
for Col:= 0 to DataSource.DataSet.FieldCount- 1do
Str:= Str+ DataSource.DataSet.Fields[col].DisplayLabel+ #9;
Memo.Lines.Add(Str);
for Row:= 0 to DataSource.DataSet.RecordCount- 1do
begin
Str:= '';
for Col:= 0 to DataSource.DataSet.FieldCount-1do
Str:= Str+ DataSource.DataSet.Fields[Col].AsString+ #9;
Memo.Lines.Add(Str);
DataSource.DataSet.Next;
end;
}
for Col:= 0 to DataSource.DataSet.FieldCount- 1do
begin
Str:= '';
Str:= Str+ DataSource.DataSet.Fields[Col].DisplayLabel+ #9;
Str:= Str+ DataSource.DataSet.Fields[Col].AsString;
Memo.Lines.Add(Str);
end;
Memo.SelectAll;
Memo.CopyToClipboard;
Ole.Workbooks[1].WorkSheets['Untitled'].Paste;
//Ole.Workbooks[1].Save;
Ole.Visible:= true;
FreeAndNil(Memo);
DataSource.DataSet.GotoBookmark(Bookmark);
DataSource.DataSet.FreeBookmark(Bookmark);
DataSource.DataSet.EnableControls;
end;
目的:取出数据库中的一行数据,Excel第一列显示各字段名称,第二列显示相应字段值。
问题:导出的Excel里面中文均显示成乱码,编码规则不清楚(如“受检者号码”显示成“”ÊܼìÕߺÅÂë)。
目前的代码:
procedure proExportExcel(DataSource: TDataSource;
AOwner: TComponent);
var
Bookmark: TBookmark;
Col: Integer;
//Row: Integer;
Str: String;
Memo: TMemo;
Ole: OleVariant;
begin
DataSource.DataSet.DisableControls;
Bookmark:= DataSource.DataSet.GetBookmark;
DataSource.DataSet.First;
Ole:= CreateOleObject('Excel.Application');
Ole.WorkBooks.Add;
Ole.WorkBooks[1].WorkSheets[1].Name:= 'Untitled';
Memo:= TMemo.Create(AOwner);
Memo.Visible:= true;
Memo.Parent:= f_questionnaire;
Memo.Clear;
{Str:= '';
for Col:= 0 to DataSource.DataSet.FieldCount- 1do
Str:= Str+ DataSource.DataSet.Fields[col].DisplayLabel+ #9;
Memo.Lines.Add(Str);
for Row:= 0 to DataSource.DataSet.RecordCount- 1do
begin
Str:= '';
for Col:= 0 to DataSource.DataSet.FieldCount-1do
Str:= Str+ DataSource.DataSet.Fields[Col].AsString+ #9;
Memo.Lines.Add(Str);
DataSource.DataSet.Next;
end;
}
for Col:= 0 to DataSource.DataSet.FieldCount- 1do
begin
Str:= '';
Str:= Str+ DataSource.DataSet.Fields[Col].DisplayLabel+ #9;
Str:= Str+ DataSource.DataSet.Fields[Col].AsString;
Memo.Lines.Add(Str);
end;
Memo.SelectAll;
Memo.CopyToClipboard;
Ole.Workbooks[1].WorkSheets['Untitled'].Paste;
//Ole.Workbooks[1].Save;
Ole.Visible:= true;
FreeAndNil(Memo);
DataSource.DataSet.GotoBookmark(Bookmark);
DataSource.DataSet.FreeBookmark(Bookmark);
DataSource.DataSet.EnableControls;
end;