数据导出到excel,前面的0自动去掉了,我不想去掉,因为这个字段是‘学号,去掉学号就不对了?我的代码是怎么写的,请大家看看:procedure XscjmdToExcel(query:TDataSet;Kcmc,kslx,cjlx:string);
var
ExcelHWND : THandle; //handle for detect whether Excel had already open
ExcelApp : TExcelApplication;
ExcelWbk : TExcelWorkBook;
ExcelSht : TExcelWorkSheet;
SaveDialog : TSaveDialog;
ExcelOpened : Boolean;
i,j : integer;
StringTemp : String;
begin
try
ExcelApp := TExcelApplication.Create(nil);
ExcelApp.Connect;
ExcelWbk := TExcelWorkBook.Create(nil);
except
Application.Messagebox('Excel 没有安装!’', '警告', MB_ICONERROR + mb_Ok);
exit;
end;
ExcelWbk.ConnectTo(ExcelApp.Workbooks.Add(EmptyParam,0));
ExcelSht := TExcelWorkSheet.Create(nil);
ExcelSht.ConnectTo(ExcelWbk.Worksheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _worksheet);
ExcelSht.Columns.AutoFit;
StringTemp:='';
with Query do
begin
first;
StringTemp := StringTemp + '序号' + #9
+ '班级课程号' + #9
+ '学号'+#9
+ '姓名'+#9
+ '课程名称'+#9
+ '考试类型'+#9
+ cjlx+'(成绩)'+#9;
StringTemp:=StringTemp+#13;
for i := 0 to recordcount - 1 do
begin
StringTemp := StringTemp + trim(fieldbyname('序号').AsString) + #9
+ trim(fieldbyname('班级课程号').AsString) + #9
+ trim(fieldbyname('学号').AsString) + #9
+ trim(fieldbyname('姓名').AsString) + #9
+ kcmc + #9
+ kslx + #9
+ trim(fieldbyname(cjlx).AsString) + #9;
StringTemp := StringTemp + #13;
next;
end;
end;
clipboard.Clear;
clipboard.Open;
clipboard.astext := StringTemp;
clipboard.Close;
ExcelSht.paste;
//弹出保存窗口
SaveDialog := TSaveDialog.Create(nil);
SaveDialog.Filter := 'Excel Files|*.xls';
if SaveDialog.Execute then
ExcelSht.SaveAs(SaveDialog.FileName);
//释放控件
SaveDialog.Free;
ExcelSht.Disconnect;
ExcelWbk.Close;
ExcelWbk.Disconnect;
ExcelApp.Disconnect;
ExcelSht.Free;
ExcelWbk.Free;
ExcelApp.Quit;
ExcelApp.Free;
end;
var
ExcelHWND : THandle; //handle for detect whether Excel had already open
ExcelApp : TExcelApplication;
ExcelWbk : TExcelWorkBook;
ExcelSht : TExcelWorkSheet;
SaveDialog : TSaveDialog;
ExcelOpened : Boolean;
i,j : integer;
StringTemp : String;
begin
try
ExcelApp := TExcelApplication.Create(nil);
ExcelApp.Connect;
ExcelWbk := TExcelWorkBook.Create(nil);
except
Application.Messagebox('Excel 没有安装!’', '警告', MB_ICONERROR + mb_Ok);
exit;
end;
ExcelWbk.ConnectTo(ExcelApp.Workbooks.Add(EmptyParam,0));
ExcelSht := TExcelWorkSheet.Create(nil);
ExcelSht.ConnectTo(ExcelWbk.Worksheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _worksheet);
ExcelSht.Columns.AutoFit;
StringTemp:='';
with Query do
begin
first;
StringTemp := StringTemp + '序号' + #9
+ '班级课程号' + #9
+ '学号'+#9
+ '姓名'+#9
+ '课程名称'+#9
+ '考试类型'+#9
+ cjlx+'(成绩)'+#9;
StringTemp:=StringTemp+#13;
for i := 0 to recordcount - 1 do
begin
StringTemp := StringTemp + trim(fieldbyname('序号').AsString) + #9
+ trim(fieldbyname('班级课程号').AsString) + #9
+ trim(fieldbyname('学号').AsString) + #9
+ trim(fieldbyname('姓名').AsString) + #9
+ kcmc + #9
+ kslx + #9
+ trim(fieldbyname(cjlx).AsString) + #9;
StringTemp := StringTemp + #13;
next;
end;
end;
clipboard.Clear;
clipboard.Open;
clipboard.astext := StringTemp;
clipboard.Close;
ExcelSht.paste;
//弹出保存窗口
SaveDialog := TSaveDialog.Create(nil);
SaveDialog.Filter := 'Excel Files|*.xls';
if SaveDialog.Execute then
ExcelSht.SaveAs(SaveDialog.FileName);
//释放控件
SaveDialog.Free;
ExcelSht.Disconnect;
ExcelWbk.Close;
ExcelWbk.Disconnect;
ExcelApp.Disconnect;
ExcelSht.Free;
ExcelWbk.Free;
ExcelApp.Quit;
ExcelApp.Free;
end;
excelsht.Cells.NumberFormatLocal:='@';