//参数pStrCols 是需要设置为字符串格式的列
procedure TXGridLayoutCortrol.ExportData(pTitle:string='';SePTitle:String='';pTail:string='';pStrCols:string='');
var
i,j:integer;
strlist:Tstringlist;
str,Filename:string;
h,k:integer;
Excelid: OleVariant;
s: string;
v,sheet,range:variant;
icol,irow:integer;
nCols:integer;
nCurrCol:integer;
nCurrRow:integer;
begin
strlist:=TStringList.Create();
str:='';
RzSaveDialog1.Title:='请选择需要导出到的目标文件';
if RzSaveDialog1.Execute = false then exit;
Filename:=trim(RzSaveDialog1.FileName);
nCols := 0;
for j:=0 to mGrid.ColCount - 1 do begin
if mGrid.ColWidths[j]>0 then nCols := nCols + 1;
end;
if nCols = 0 then begin
showmessage('没有数据,无法导出!');
exit;
end;
if (rightstr(filename,4)='.htm') then begin
strlist.Add('<div style="font-size:18pt;"><center><b>'+trim(pTitle)+'</b></center></div>'+'<tr>'+'<div style="font-size:12pt;">'+septitle+'</tr>');
strlist.Add('<Table style="border-collapse:collapse;font-size:12pt" cellpadding=4 border=1 bordercolor=black>');
for i:=0 to mGrid.rowcount -1 do begin
if i=0 then strlist.Add('<tr>')
else strlist.Add('<tr style="color:#000088">');
for j:=0 to mGrid.ColCount - 1 do begin
if mGrid.ColWidths[j]>0 then begin
str := str + '<td><nobr>'+mGrid.Cells[j,i] + '</nobr></td>';
end;
end;
strList.Add(str);
strlist.Add('</tr>');
str := '';
end;
strlist.Add('</Table>');
strlist.Add('<div style="font-size:12pt">'+pTail+'</div>');
strList.SaveToFile(Filename);
strlist.Free;
strlist := nil;
end
else if (rightstr(trim(filename),4)='.xls') then begin
//导出到excel表格
try
Excelid := CreateOLEObject('Excel.Application');
except
Application.MessageBox('Excel没有安装!', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
Exit;
end;
Excelid.Visible := false;
//Excelid.Visible := true;
Excelid.WorkBooks.Add;
//Excelid.WorkBooks[1].WorkSheets[1].Name := pTitle;
Sheet := Excelid.Workbooks[1].WorkSheets[1];
//标题
sheet.cells[1, 1] := pTitle;
sheet.range[sheet.cells[1, 1],sheet.cells[1,nCols]].Select; //选择该列
Excelid.selection.HorizontalAlignment := $FFFFEFF4; //居中
Excelid.selection.MergeCells := True;
//小标题
nCurrRow := 2;
if SePTitle <> '' then begin
Sheet.Cells[2,1] := SePTitle;
sheet.range[sheet.cells[2, 1],sheet.cells[2,nCols]].Select; //选择该列
//Excelid.selection.HorizontalAlignment := $FFFFEFF4; //居中
Excelid.selection.MergeCells := True;
//表体(包括表头)
nCurrRow := 3;
end;
for i:=0 to mGrid.RowCount-1 do begin
nCurrCol := 1;
for j:=0 to mGrid.ColCount-1 do begin
if mGrid.ColWidths[j]>0 then begin
if pos(','+inttostr(j)+',', ','+pStrCols+',')<>0 then begin //导出为字符串格式
Sheet.Cells[nCurrRow,nCurrCol].NumberFormatLocal := '@';
Sheet.Cells[nCurrRow,nCurrCol] := mGrid.Cells[j,i];
end else begin
Sheet.Cells[nCurrRow,nCurrCol] := mGrid.Cells[j,i];
end;
nCurrCol := nCurrCol + 1;
end;
end;
nCurrRow := nCurrRow + 1;
end;
//表尾文字
Sheet.Cells[nCurrRow,1] := pTail;
sheet.range[sheet.cells[nCurrRow, 1],sheet.cells[nCurrRow,nCols]].Select; //选择该列
Excelid.selection.HorizontalAlignment := $FFFFEFF4; //居中
Excelid.selection.MergeCells := True;
try
sheet.cells[1,1].Select;
Excelid.Workbooks[1].SaveAs(FileName);
Excelid.Workbooks[1].close;
Excelid.Quit;
except
Excelid.Quit; //有时写完后立即退出,但写进程还占用着该文件,不允许退出,所以这里再退出一次
end; //实际上就是设一点点延迟,
Excelid := Unassigned;
end else begin
strlist.Add(pTitle);
for i:=0 to mGrid.rowcount -1 do begin
for j:=0 to mGrid.ColCount - 1 do begin
str := str + mGrid.Cells[j,i] + #9;
end;
strList.Add(str);
str := '';
end;
strlist.Add(pTail);
strList.SaveToFile(Filename);
strlist.Free;
strlist := nil;
end;
end;
delphi导出数据到excel
最新推荐文章于 2020-04-22 10:01:38 发布