I would like to make this excel read only so that nobody can change its data. Is there a way to do it?
'Excel
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRange As Excel.Range
' Start Excel and get Application object.
oXL = New Excel.Application
' Set some properties
oXL.Visible = False
oXL.DisplayAlerts = False
' Get a new workbook.
oWB = oXL.Workbooks.Add
oSheet = oWB.Worksheets(2) ' Assuming there is > 1 sheet in the Workbook, get Sheet 2
oSheet.Activate() ' Activate the Worksheet
' Get the active sheet
oSheet = DirectCast(oWB.ActiveSheet, Excel.Worksheet)
oSheet.Name = "Detay"
'Style
Dim style As Excel.Style = oSheet.Application.ActiveWorkbook.Styles.Add("NewStyle")
style.Font.Name = "Verdana"
style.Font.Size = 12
style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black)
style.Interior.Pattern = Excel.XlPattern.xlPatternSolid
style.Font.Bold = True
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
'Headers
oSheet.Range("A" + row_num.ToString + "").Value = "SNO"
oSheet.Range("B" + row_num.ToString + "").Value = "ADISOYADI"
'oSheet.Range("C" + row_num.ToString + "").Value = "NOKTAADI"
oSheet.Range("C" + row_num.ToString + "").Value = "FIRMA"
oSheet.Range("D" + row_num.ToString + "").Value = "BÖLÜM"
oSheet.Range("E" + row_num.ToString + "").Value = "TARIH"
oSheet.Range("F" + row_num.ToString + "").Value = "ÇIKISSAATI"
oSheet.Range("G" + row_num.ToString + "").Value = "GIRISSAATI"
oSheet.Range("A" + row_num.ToString + ":G" + row_num.ToString + "").Font.Bold = True
oSheet.Range("A" + row_num.ToString + ":G" + row_num.ToString + "").Style = style
row_num = row_num + 1
'Calculate
Calculate(row_num, oSheet, period, time1, time2, date1, date2, ekstra)
' Resize the columns
oRange = oSheet.Range(oSheet.Cells(1, 1), _
oSheet.Cells(100, 7))
oRange.EntireColumn.AutoFit()
' Save the sheet and close
oSheet = Nothing
oRange = Nothing
oWB.SaveAs("c:\GirisCikis\Giris-Çikis(" + date1 + " - " + date2 + ").xlsx")
oWB.Close()
oWB = Nothing
oXL.Quit()
Darren KangPosted Jul 28, 2014, 5:05 AM
Also if interested an alternative way that you can try is with this VB.NET DLL designed for Excel files, you can find a protection demonstration sample here.
Rahul PrasadPosted Jul 8, 2014, 3:52 AM
I use Protect method of XlsWorksheetBase class to protect method to protect Excel worksheet.More details about this method please refer - protect Excel worksheet in VB.NET
Namespace ProtectExcel
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Load Workbook
Dim book As New Workbook()
book.LoadFromFile("E:\Work\Documents\VendorInfo.xlsx")
'Protect Workbook
book.Protect("abc-123")
'Save and Launch
book.SaveToFile("ProtectExcel.xlsx", ExcelVersion.Version2010)
System.Diagnostics.Process.Start("ProtectExcel.xlsx")
End Sub
End Class
End Namespace