bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Delphi How protect Excel Sheet using Delphi ?
查看: 6766|回復: 0
go

How protect Excel Sheet using Delphi ? [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2011-4-7 17:27 |只看該作者 |倒序瀏覽 |打印
How protect Excel Sheet using Delphi ?
Answer:
  1. unit UTesteProtect;

  2. interface

  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComObj;

  5. type
  6. TForm1 = class(TForm)
  7. Button1: TButton;
  8. procedure Button1Click(Sender: TObject);
  9. procedure FormDestroy(Sender: TObject);
  10. private
  11. { Private declarations }
  12. public
  13. { Public declarations }
  14. end;

  15. var
  16. Form1: TForm1;
  17. ExcelApp: OleVariant;

  18. implementation

  19. {$R *.dfm}

  20. procedure TForm1.Button1Click(Sender: TObject);
  21. const
  22. // SheetType
  23. xlChart = -4109;
  24. xlWorksheet = -4167;
  25. // WBATemplate
  26. xlWBATWorksheet = -4167;
  27. xlWBATChart = -4109;
  28. // Page Setup
  29. xlPortrait = 1;
  30. xlLandscape = 2;
  31. xlPaperA4 = 9;
  32. // Format Cells
  33. xlBottom = -4107;
  34. xlLeft = -4131;
  35. xlRight = -4152;
  36. xlTop = -4160;
  37. // Text Alignment
  38. xlHAlignCenter = -4108;
  39. xlVAlignCenter = -4108;
  40. // Cell Borders
  41. xlThick = 4;
  42. xlThin = 2;
  43. var
  44. ColumnRange: OleVariant;
  45. begin
  46. { Start Excel }

  47. // By using GetActiveOleObject, you use an instance o
  48. // f Word that's already running,
  49. // if there is one.
  50. try
  51. // If no instance of Word is running, try to Create a new Excel Object
  52. ExcelApp := CreateOleObject('Excel.Application');
  53. except
  54. ShowMessage('Cannot start Excel/Excel not installed ?');
  55. Exit;
  56. end;

  57. // Add a new Workbook, Neue Arbeitsmappe offnen
  58. ExcelApp.Workbooks.Add(xlWBatWorkSheet);

  59. // Open a Workbook, Arbeitsmappe offnen
  60. ExcelApp.Workbooks.Open('c:\pasta1.xls');


  61. // Rename the active Sheet
  62. ExcelApp.ActiveSheet.Name := 'Pasta1';

  63. // Rename
  64. ExcelApp.Workbooks[1].WorkSheets[1].Name := 'Pasta1';

  65. // Insert some Text in some Cells[Row,Col]
  66. ExcelApp.Cells[1, 1].Value := 'Test';
  67. ExcelApp.Cells[2, 1].Value := 'http://www.delphi3000.com';
  68. ExcelApp.Cells[3, 1].Value := FormatDateTime('dd-mmm-yyyy', Now);

  69. // Setting a row of data with one call
  70. //ExcelApp.Range['A2', 'D2'].Value := VarArrayOf([1, 10, 100, 1000]);

  71. // Setting a formula
  72. // ExcelApp.Range['A11', 'A11'].Formula := '=Sum(A1:A10)';

  73. // Change Cell Alignement
  74. // ExcelApp.Cells[2, 1].HorizontalAlignment := xlright;

  75. // Change the Column Width.
  76. ColumnRange := ExcelApp.Workbooks[1].WorkSheets[1].Columns;
  77. ColumnRange.Columns[1].ColumnWidth := 20;
  78. ColumnRange.Columns[2].ColumnWidth := 40;

  79. // Change Rowheight / Zeilenhohe andern:
  80. ExcelApp.Rows[1].RowHeight := 15.75;

  81. // Merge cells, Zellen verbinden:
  82. ExcelApp.Range['B3:D3'].Mergecells := True;

  83. // Apply borders to cells, Zellen umrahmen:
  84. ExcelApp.Range['A14:M14'].Borders.Weight := xlThick; // Think line/ Dicke Linie
  85. ExcelApp.Range['A14:M14'].Borders.Weight := xlThin; // Thin line Dunne Linie

  86. // Set Bold Font in cells, Fettdruck in den Zellen

  87. ExcelApp.Range['B16:M26'].Font.Bold := True;

  88. // Set Font Size, Schriftgro?e setzen
  89. ExcelApp.Range['B16:M26'].Font.Size := 12;

  90. //right-aligned Text, rechtsbundige Textausrichtung
  91. ExcelApp.Cells[9, 6].HorizontalAlignment := xlright;

  92. // horizontal-aligned text, horizontale Zentrierung
  93. ExcelApp.Range['B14:M26'].HorizontalAlignment := xlHAlignCenter;

  94. // left-aligned Text, vertikale Zentrierung
  95. // ExcelApp.Range['B14:M26'].VerticallyAlignment := xlVAlignCenter;


  96. { Page Setup }

  97. ExcelApp.ActiveSheet.PageSetup.Orientation := xlLandscape;

  98. // Left, Right Margin (Seitenrander)
  99. ExcelApp.ActiveSheet.PageSetup.LeftMargin := 35;
  100. ExcelApp.ActiveSheet.PageSetup.RightMargin := -15;

  101. // Set Footer Margin
  102. ExcelApp.ActiveSheet.PageSetup.FooterMargin := ExcelApp.InchesToPoints(0);

  103. // Fit to X page(s) wide by Y tall
  104. ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1; // Y
  105. ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 3; // Y

  106. // Zoom
  107. ExcelApp.ActiveSheet.PageSetup.Zoom := 95;

  108. // Set Paper Size:
  109. // ExcelApp.PageSetup.PaperSize := xlPaperA4;

  110. // Show/Hide Gridlines:
  111. ExcelApp.ActiveWindow.DisplayGridlines := False;

  112. // Set Black & White
  113. ExcelApp.ActiveSheet.PageSetup.BlackAndWhite := False;

  114. // footers
  115. ExcelApp.ActiveSheet.PageSetup.RightFooter := 'Right Footer / Rechte Fu?zeile';
  116. ExcelApp.ActiveSheet.PageSetup.LeftFooter := 'Left Footer / Linke Fu?zeile';

  117. // Show Excel Version:
  118. ShowMessage(Format('Excel Version %s: ', [ExcelApp.Version]));

  119. // Show Excel:
  120. // ExcelApp.Visible := True;

  121. // Save the Workbook
  122. //ExcelApp.SaveAs('c:\filename.xls');

  123. // Save the active Workbook:
  124. // ExcelApp.ActiveSheet.Protect.Password := 'Teste';
  125. ExcelApp.ActiveSheet.Protect(Password:='Teste', DrawingObjects:=True, Contents:=True, Scenarios:=True);//;
  126. //Contents:=True, Scenarios:=True
  127. ExcelApp.ActiveWorkBook.SaveAs('c:\filename.xls');
  128. end;

  129. procedure TForm1.FormDestroy(Sender: TObject);
  130. begin
  131. // Quit Excel
  132. if not VarIsEmpty(ExcelApp) then
  133. begin
  134. ExcelApp.DisplayAlerts := False; // Discard unsaved files....
  135. ExcelApp.Quit;
  136. end;

  137. end;

  138. end.
複製代碼
資料來源 http://delphizine.blogspot.com/2 ... ng-delphi_6285.html
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2025-5-2 04:48 , Processed in 0.015831 second(s), 10 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.