try<br> Excel := CreateOleObject('Excel.Application');<br> except<br> on e: Exception do begin<br> MessageBox(GetActiveWindow, '导出失败,请先安装电子表格Microsoft Excel', '警告提示', MB_ICONWARNING or MB_OK);<br> Excel := null;<br> Exit;<br> end;<br> end;<br><br> try<br> WorkBook := Excel.Application.WorkBooks.Add;<br> WorkSheet := WorkBook.WorkSheets[1];<br><br> //列<br> for c := 0 to VisibleColumnCount - 1 do begin<br> WorkSheet.Columns[c + 1].NumberFormat := '@';<br> WorkSheet.Cells[1, c + 1].Value := VisibleColumns[c].Caption.Text;<br> end;<br><br> //行<br> for r := 0 to Nodes.Count - 1 do begin //改变下遍历方式<br> for c := 0 to VisibleColumnCount - 1 do begin<br> <br> WorkSheet.Cells[r + 2, c + 1].Value := Nodes[r].Texts[c];<br> end;<br> end;<br><br> try<br> if WorkBook.SaveAs(fileName) then begin<br> <br> MessageBox(GetActiveWindow, pchar('成功导出到文件' + fileName), '提示', MB_ICONINFORMATION or MB_OK);<br> end;<br> except<br> on e: Exception do begin<br> MessageBox(GetActiveWindow, '保存失败,请检查文件是否正在使用', '提示', MB_ICONWARNING or MB_OK);<br> exit;<br> end;<br> end;<br> except<br> on e: Exception do begin<br> MessageBox(GetActiveWindow, PChar(e.Message), '警告', MB_ICONWARNING or MB_OK);<br> WorkBook.Saved := True;<br> WorkBook := null;<br> Excel.Quit;<br> Excel := null;<br> Exit;<br> end;<br> end;<br><br> WorkBook.Saved := True;<br> WorkBook := null;<br> Excel.Quit;<br> Excel := null;