如何将stringgrid的内容保存到excel中?高手们请教啊~~(20分)

  • 主题发起人 主题发起人 kill_it
  • 开始时间 开始时间
K

kill_it

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将stringgrid中的内容保存到excel中,事情很急啊~~高手们能否给代码。万分感谢。
 
难道没有人知道吗?/如果要打印stringgrid的内容又该如何做呢?我从来没有作
过这样的问题?最好代码以解燃眉之急
 
用tms得advstringgrid可以,方便
 
procedure stringgrid_export_excel(AdsData: Tstringgrid;hctrl:theadercontrol; sName, Title: string;bool:boolean=false);
var
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
i, j: integer;
filename: string;
rangee:excel2000.range;
tmpcursor:tcursor;
begin
tmpcursor:=Screen.Cursor;
Screen.Cursor:=crHourglass;
filename :=sName;
try
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;

except
Application.Messagebox('Excel 没有安装!', 'Hello',MB_Ok);
Abort;
end;
try
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);

if hctrl<>nil then
for j := 0 to hctrl.Sections.Count- 1 do
begin
ExcelWorksheet1.Cells.item[3, j + 1] := hctrl.Sections[j].Text;
ExcelWorksheet1.Cells.item[3, j + 1].font.size := '10';
end;
for i := 4 to adsdata.RowCount+ 3 do
begin
rangee:=ExcelApplication1.Range['A'+INTTOSTR(I),CHR(64+adsdata.ColCount)+INTTOSTR(I)];
rangee.AutoFormat(0,xltext,null,null,null,null,null);
for j := 0 to adsdata.ColCount- 1 do
begin
//rangee.NumberFormat ;
rangee.Font.Size:=10;
rangee.Value:=''''+AdsData.Cells[j,i-4];
rangee:=rangee.Next;
end;

end;
ExcelWorksheet1.Columns.AutoFit;
ExcelWorksheet1.Cells.item[1, 2] := Title;
ExcelWorksheet1.Cells.Item[1, 2].font.size := '14';
if bool then
begin
//ExcelApplication1.Visible[0]:=true ;
ExcelWorksheet1.Printout;
Application.Messagebox(pchar('数据成功打印' + filename), 'Hello',
mb_Ok);
filename:='c:/wh$ds$@#.xls';
ExcelWorksheet1.SaveAs(filename);
end
else
begin
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar('数据成功导出' + filename), 'Hello',
mb_Ok);
end;
finally

ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
end;
if bool then
if FileExists(FileName) then
deletefile(filename);
Screen.Cursor:=tmpcursor;
end;

procedure leftadd_string(i: integer; var s: string);
begin
if length(trim(s))>i then
s:=copy(s,1,i)
else
while length(trim(s))<i do
insert('0',s,1);
end;
 
如果你的stringGrid格式比较复杂(比如多行,合并字段等)。建议你先编辑好一个固定的
Excel格式摸板,再在程序中调用。
 

Similar threads

后退
顶部