static function THSZYJ.DbtreelistToExcel(DataTreeList:TDataTreeList;
dtlcTemp:TDataTreeListColumn;AFileName:string):integer;//输出treelist数据到excel文件,dtlcTemp为自动缩进的列
const
ciLineWidth=2;
var
ExcelApp,workbook: Object;
i,j,k:integer;
app, docs, doc,worksheets, worksheet,cell,temp: System.DispatchHelper;
tmpNode:TTreeListNode;
begin
ExcelApp:=ComObj.CreateOleObject('Excel.Application');
try
app := System.DispatchHelper.Create(ExcelApp);
app.PropertyPut('Visible', [false]);
docs := System.DispatchHelper.Create(app.PropertyGet('WorkBooks',[]));
docs.InvokeMethod('ADD', []);
workbook := docs.PropertyGet('Count',[]);
if ObjectHelper.ToInt(workbook)>0 then
begin
doc := System.DispatchHelper.Create(docs.PropertyGet('Item',[1]));
worksheets := System.DispatchHelper.Create(doc.PropertyGet('Sheets',[]));
worksheet := System.DispatchHelper.Create(worksheets.PropertyGet('Item',[1]));
//增加列名
j:=1;k:=0;
for i:=0 to DataTreeList.ColumnCount-1 do
begin
if DataTreeList.Columns.Visible then
begin
inc(k);
cell := System.DispatchHelper.Create(worksheet.PropertyGet('Cells',[j,k]));
cell.PropertyPut('Cells', [DataTreeList.Columns.Caption]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Font',[]));
temp.PropertyPut('Name', ['宋体']);
temp.PropertyPut('Bold', [True]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[1]));
temp.PropertyPut('Weight', [ciLineWidth]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[2]));
temp.PropertyPut('Weight', [ciLineWidth]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[3]));
temp.PropertyPut('Weight', [ciLineWidth]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[4]));
temp.PropertyPut('Weight', [ciLineWidth]);
temp := System.DispatchHelper.Create(worksheet.PropertyGet('Columns',[k]));
temp.PropertyPut('ColumnWidth', [DataTreeList.Columns.Width div 8]);
end;
end;
end;
//增加数据
while true do
begin
if not assigned(DataTreeList.TopNode) then
break;
if not assigned(tmpNode) then
tmpNode:=DataTreeList.TopNode
else
tmpNode:=tmpNode.GetNext;
if not assigned(tmpNode) then
break;
inc(j);k:=0;
for i:=0 to DataTreeList.ColumnCount-1 do
begin
if DataTreeList.Columns.Visible then
begin
inc(k);
cell := System.DispatchHelper.Create(worksheet.PropertyGet('Cells',[j,k]));
if dtlcTemp=DataTreeList.Columns then
cell.PropertyPut('Cells', [{lpad('',tmpNode.Level*2,' ')+}tmpNode.Strings])//lpad功能与oracle中的lpad一样。
else
cell.PropertyPut('Cells', [tmpNode.Strings]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[1]));
temp.PropertyPut('Weight', [ciLineWidth]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[2]));
temp.PropertyPut('Weight', [ciLineWidth]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[3]));
temp.PropertyPut('Weight', [ciLineWidth]);
temp := System.DispatchHelper.Create(cell.PropertyGet('Borders',[4]));
temp.PropertyPut('Weight', [ciLineWidth]);
end;
end;
end;
doc.InvokeMethod('SaveCopyAs',[AFileName]);
app.PropertyPut('DisplayAlerts', [False]);
finally
docs.InvokeMethod('Close', []);
app.InvokeMethod('Quit', []);
{ (worksheet as System.IDisposable).dispose;
(worksheets as System.IDisposable).dispose; }
{ (doc as System.IDisposable).dispose;
(docs as System.IDisposable).dispose; }
//(App as System.IDisposable).dispose;
//(workbook as System.IDisposable).dispose;
(ExcelApp as System.IDisposable).dispose;
end;
end;
调用的代码
if sdExcel.Execute then
begin
HSZYJ.THSZYJ.DbtreelistToExcel(DataTreeListSJ,DataTreeListSJWDMLMC,sdExcel.FileName);
end; |