关于Dataset to Excel的问题(100分)

  • 主题发起人 主题发起人 nfy
  • 开始时间 开始时间
N

nfy

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个可以把Dataset to Excel的函数,但他有一个问题,就是比如编号为'000001'这样的记录转到excel中就变为1了,谁能帮我改下这个函数.
数据环境
ADO+Accesss
函数如下
//将dataset导出EXCEL的function
//procedure WriteToExcel(AdsData: TADODataSet; sName, Title: string);
procedure WriteToExcel(AdsData: TADOQuery; sName, Title: string);
var
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
i, j: integer;
filename: string;
begin
//filename := concat(extractfilepath(application.exename), sName, '.xls');
filename:=sName;
try
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox('Excel 没有安装!','Hello', MB_ICONERROR + mb_Ok);
Abort;
end;
try
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
AdsData.First;
for j := 0 to AdsData.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[3, j + 1] := AdsData.Fields[j].DisplayLabel;
ExcelWorksheet1.Cells.item[3, j + 1].font.size :='10';
end;
for i := 4 to AdsData.RecordCount + 3 do
begin
for j := 0 to AdsData.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[i, j + 1] :=
AdsData.Fields[j].Asstring;
ExcelWorksheet1.Cells.item[i, j + 1].font.size :='10';
end;
AdsData.Next;
end;
ExcelWorksheet1.Columns.AutoFit;
ExcelWorksheet1.Cells.item[1, 2] := Title;
ExcelWorksheet1.Cells.Item[1, 2].font.size :='14';
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar('数据成功导出' + filename), 'Hello',
mb_Ok);
finally
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
end;
end;

 
有人能答吗
 
AdsData.Fields[j].Asstring
改为 quotedstr(AdsData.Fields[j].Asstring)怎么样呢?
 
没那么复杂吧?
Excel := CreateOleObject('Excel.Application');
Excel.WorkBooks.Add;
ExcelCells := Excel.WorkSheets[1].Cells;
//------------Fill Data
for iRow:=1 to tds.RecordCount do
begin
for iColumn := 0 to tds.Fields.Count-1 do
ExcelCells.Item[iRow,iColumn+1] := tds.Fields[iColumn].AsString;
tds.Next;
end;
//------------Fill Data
Excel.Caption := Caption;
Excel.Visible := true;
ExcelCells := null;
Excel := null;
 
把你要轉換的數字強行定為文字就行了...就是在數字中加上引號...
 
哈哈自己解决了
begin
//在这里加上这条语句就行了
// ExcelWorksheet1.Cells.item[i, j + 1].NumberFormatLocal := '@';

ExcelWorksheet1.Cells.item[3, j + 1] := AdsData.Fields[j].DisplayLabel;
ExcelWorksheet1.Cells.item[3, j + 1].font.size :='10';
end;


 

Similar threads

I
回复
0
查看
847
import
I
I
回复
0
查看
610
import
I
I
回复
0
查看
653
import
I
I
回复
0
查看
544
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部