Use password to protect Excel Sheet
Source / Exemple :
unit UTesteProtect;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ExcelApp: OleVariant;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
// SheetType
xlChart = -4109;
xlWorksheet = -4167;
// WBATemplate
xlWBATWorksheet = -4167;
xlWBATChart = -4109;
// Page Setup
xlPortrait = 1;
xlLandscape = 2;
xlPaperA4 = 9;
// Format Cells
xlBottom = -4107;
xlLeft = -4131;
xlRight = -4152;
xlTop = -4160;
// Text Alignment
xlHAlignCenter = -4108;
xlVAlignCenter = -4108;
// Cell Borders
xlThick = 4;
xlThin = 2;
var
ColumnRange: OleVariant;
begin
{ Start Excel }
// By using GetActiveOleObject, you use an instance o
// f Word that's already running,
// if there is one.
try
// create a new instance of Excel
Excel := CreateOleObject('Excel.Application');
except
on E:Exception do
begin
E.Message := 'Cannot start Excel/Excel not installed ?'#13#10+E.Message;
raise;
exit;
end;
end;
// Add a new Workbook, Neue Arbeitsmappe offnen
ExcelApp.Workbooks.Add(xlWBatWorkSheet);
// Open a Workbook, Arbeitsmappe offnen
ExcelApp.Workbooks.Open('c:\pasta1.xls');
// Rename the active Sheet
ExcelApp.ActiveSheet.Name := 'Pasta1';
// Rename
ExcelApp.Workbooks[1].WorkSheets[1].Name := 'Pasta1';
// Insert some Text in some Cells[Row,Col]
ExcelApp.Cells[1, 1].Value := 'Test';
ExcelApp.Cells[2, 1].Value := 'http://www.delphifr.com';
ExcelApp.Cells[3, 1].Value := FormatDateTime('dd-mmm-yyyy', Now);
// Setting a row of data with one call
//ExcelApp.Range['A2', 'D2'].Value := VarArrayOf([1, 10, 100, 1000]);
// Setting a formula
// ExcelApp.Range['A11', 'A11'].Formula := '=Sum(A1:A10)';
// Change Cell Alignement
// ExcelApp.Cells[2, 1].HorizontalAlignment := xlright;
// Change the Column Width.
ColumnRange := ExcelApp.Workbooks[1].WorkSheets[1].Columns;
ColumnRange.Columns[1].ColumnWidth := 20;
ColumnRange.Columns[2].ColumnWidth := 40;
// Change Rowheight / Zeilenhohe andern:
ExcelApp.Rows[1].RowHeight := 15.75;
// Merge cells, Zellen verbinden:
ExcelApp.Range['B3:D3'].Mergecells := True;
// Apply borders to cells, Zellen umrahmen:
ExcelApp.Range['A14:M14'].Borders.Weight := xlThick; // Think line/ Dicke Linie
ExcelApp.Range['A14:M14'].Borders.Weight := xlThin; // Thin line Dunne Linie
// Set Bold Font in cells, Fettdruck in den Zellen
ExcelApp.Range['B16:M26'].Font.Bold := True;
// Set Font Size, Schriftgro?e setzen
ExcelApp.Range['B16:M26'].Font.Size := 12;
//right-aligned Text, rechtsbundige Textausrichtung
ExcelApp.Cells[9, 6].HorizontalAlignment := xlright;
// horizontal-aligned text, horizontale Zentrierung
ExcelApp.Range['B14:M26'].HorizontalAlignment := xlHAlignCenter;
// left-aligned Text, vertikale Zentrierung
// ExcelApp.Range['B14:M26'].VerticallyAlignment := xlVAlignCenter;
{ Page Setup }
ExcelApp.ActiveSheet.PageSetup.Orientation := xlLandscape;
// Left, Right Margin (Seitenrander)
ExcelApp.ActiveSheet.PageSetup.LeftMargin := 35;
ExcelApp.ActiveSheet.PageSetup.RightMargin := -15;
// Set Footer Margin
ExcelApp.ActiveSheet.PageSetup.FooterMargin := ExcelApp.InchesToPoints(0);
// Fit to X page(s) wide by Y tall
ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1; // Y
ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 3; // Y
// Zoom
ExcelApp.ActiveSheet.PageSetup.Zoom := 95;
// Set Paper Size:
// ExcelApp.PageSetup.PaperSize := xlPaperA4;
// Show/Hide Gridlines:
ExcelApp.ActiveWindow.DisplayGridlines := False;
// Set Black & White
ExcelApp.ActiveSheet.PageSetup.BlackAndWhite := False;
// footers
ExcelApp.ActiveSheet.PageSetup.RightFooter := 'Right Footer / Rechte Fu?zeile';
ExcelApp.ActiveSheet.PageSetup.LeftFooter := 'Left Footer / Linke Fu?zeile';
// Show Excel Version:
ShowMessage(Format('Excel Version %s: ', [ExcelApp.Version]));
// Show Excel:
// ExcelApp.Visible := True;
// Save the Workbook
//ExcelApp.SaveAs('c:\filename.xls');
// Save the active Workbook:
// ExcelApp.ActiveSheet.Protect.Password := 'Teste';
ExcelApp.ActiveSheet.Protect(Password:='Teste', DrawingObjects:=True, Contents:=True, Scenarios:=True);//;
//Contents:=True, Scenarios:=True
ExcelApp.ActiveWorkBook.SaveAs('c:\filename.xls');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Quit Excel
if not VarIsEmpty(ExcelApp) then
begin
ExcelApp.DisplayAlerts := False; // Discard unsaved files....
ExcelApp.Quit;
end;
end;
end.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.