Exporting data to Excel and protecting Excel worksheet

Hi All,

In my application I have to export some data to excel and after exporting I have to protect specific range in excel worksheet from editing. I can export data without any problems but for some reason excelWorksheet.Protect () method is not working. Here is my code:

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

string workbookPath = ConfigurationManager.AppSettings["ExcelPath"] + @"\MyFile.xls";

Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;

string currentSheet = "Sheet1";

Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);

iRow = 5;


Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("A" + iRow, "A" + iRow);

excelCell.Value2 =


excelCell = (Microsoft.Office.Interop.Excel.

Range)excelWorksheet.get_Range("B" + iRow, "B" + iRow);

excelCell.Value2 =


// and so on for all columns and rows

excelWorksheet.Cells.Locked =



Range excelRange = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("A5", "A15");

excelRange.Cells.Locked =



"admin", false, false, false, false, false, false, false, false, true, false, false, true, false, false, false);

after all this range ("A5","A15") is still editable.

Any ideas ???



7/20/2006 3:08:20 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

0 Replies

Similar Articles

[PageSpeed] 27
Get it on Google Play
Get it on Apple App Store


Similar Artilces:

Export data from web form to excel -- using client's excel app
I am currently trying to figure out if there is any way to export data to the client's excel app. I do not want to use the web server's excel application as it is cutting into the site's performance. Is there any way to use something similar to the what is included below (ExportComponent.vb), but to use the client's excel app? Imports System.Drawing.Color Public Class ExportComponent Inherits System.ComponentModel.Component Public Shared Sub DataToExcel(ByVal dgData As DataGrid, ByVal response As HttpResponse) response.Clear() response.Charse...

Exporting data from web form to excel file
I have a page with datgrid and one button called export into excel. I use this code to export data into excel sheet. If my excel file size become more then 50k then i am having error and i can not open or save the excel file. Here is the code:  Server.ScriptTimeout = 12000 Response.ContentType = "application/vnd.ms-excel" dvwdetail = GetSQLData(VSQL) grdapex.DataSource = dvwdetail.Tables(0) grdapex.DataBind() If i have less row means the excel file size is less then 50k then it works fine but if i have more data file size is more then 50k then it give me error m...

Aspose.Excel.Web 1.0 Released!
Aspose.Excel.Web 1.0 Released! Version 1.0 Released on March 10 2005 ...

Exporting to Excel in Crystal Reports .NET
sir,   I want to export Crystal report to Excel and the following is the code i wrote in the button click.ReportDocument rptExcel = new ReportDocument() ;string strExportFile = "C:/Session_SessionID.xls" ;rptExcel = new RptJobOrderRecap();//report namerptExcel.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile ;rptExcel.ExportOptions.ExportFormatType = ExportFormatType.Excel;ExcelFormatOptions objExcelOptions = new ExcelFormatOptions();objExcelOptions.ExcelUseConstantColumnWidth = false ;rptExcel.ExportOptions.FormatOptions = objExcelOptions ;DiskFileDestinationOp...

Export To Excel Excel
Do we have any facility to export grid data in Excel with using Report Tool. Just a question, Will Response.contenet Type will work here Thanks, private void btnExportToExcel_Click(object sender,System.EventArgs e) { ExportToExcel("Report.xls",DataGrid1); } private void ExportToExcel(string strFileName,DataGrid dg) { Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("content-disposition", "attachment;filename=" + strFileName); Response.Charset = ""; this.EnableVi...

Export Datagrid to Excel not showing any data on Excel Page
Hi I have a requirement where i need to export the contents of Datagrid to Excel sheet. I have used the following code to do the functionality, but it is not showing up any data on the Excel sheet. It is completely blank, Why is that ???? Any help is appreciated. Here is the code Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Bind_CustomerData() End If End Sub Sub Bind_CustomerData() Dim conPortal As New SqlConnection(Comm...

Exporting in Excel without cells or excel columns merging for one column of data table(need sorting, data filters perfectly)
 Hi,   I have an issue with the excel exporting of the report using ssrs.  I am using only the body section for exporting to excel.  Here in the body i have an Image, Textbox(for Title), textbox2(for dates from,to) and below it i have a table having the columns used for showing the data.  The report gets generated but when i exported to excel, All the columns and rows in the excels sheets come close and its like there is no uniformity.  Few columsn of excel are covered under a single columsn of the data table.  So its making users feel uncomfortab...

Excel Export Control Showing Blank Worksheet in Excel 2003
I have a custom control (C#) that will take any datagrid, htmltable, or repeater and display it as a downloadable Excel document.  It currently works great in Office 97, but we are gradually rolling out Office 2003 and it seems to show a blank worksheet. Here is the code I am using for the control: using System;using System.Data;using System.Drawing;using System.Web;using System.IO;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.ComponentModel;namespace MainWeb.CustomWebControls { public class ExcelExport : WebControl { protected ...

Export data to EXCEL in PB.NET
Hi, I need to export data from database to an excel file. For that i created a PB11.5 application with OLE object and tried to open an excel in the button click. It worked fine in PB11.5. I migrated the same application into PB.NET. It deployed with 0 errors and 0 warnings. When i tried to view the application in web, i got the NULL object reference error. I tried to debug and found that the error came up in the initialization of OLE object. 1. Does that mean PB.NET does not support OLE controls? 2. If not, is there any other way to do EXCEL export in PB.NET? Do any .N...

Export data to multiple Excel Worksheets
I have been able to export data to excel with no problems.  However, I now need to create several worksheets within a single Excel Workbook from my ASP.NET web page. I haven't found much on the 'Net regarding this issue. Has anyone done this before?  Can someone point me in a direction to get this task done? Thanks,   Bryan Hi, Bryan: You can have a look at  george wesolowski's net blog aboutTransforming a DataSet with Multiple Data Tables to an Excel http://www.georgewesolowski.com/blog/PermaLink,guid,f752421c-86a7-4d68-be71-39807b8f03f7.aspx I hope the above i...

Export web form contents to Excel?
Is there much to exporting the contents of a web form to an Excel spreadsheet?  I've been doing some research and the vast majority of articles seem to pertain to exporting GridView and DataGrid controls.Thanks!  RobertF57:Is there much to exporting the contents of a web form to an Excel spreadsheet?  I've been doing some research and the vast majority of articles seem to pertain to exporting GridView and DataGrid controls.  Base on my experience, you can create Excel.Application to solve your problem, check out the following link: http://support.microsoft.c...

export to excel in web form application
hi, there is possible to export datawindow to excel document in web form application? if there is possible, how?can you give a sample application. many thanks Hi, Deny I would recommend you to ask your question in the "webform" newsgroup. Before that it would be of big help to read the existing postings on the theme - search for messages containing some of the words "Excel", "XLS", "DW2XLS". Then if you have further questions, ask. Regards, Ivaylo "Deny Fernando" <half.lefthanded@gmail.com> wrote in message news:a19...

Error when exporting data from sql to excel(error when opening excel spreadsheet)
Im exporting data to and excel spreadsheet with this code :   Dim style As String = "<style> .text { mso-number-format:\@; } </style> "   Response.ClearContent()Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls") Response.ContentType = "application/excel"  Dim sw As New StringWriter()Dim htw As New HtmlTextWriter(sw)   GridView2.RenderControl(htw) 'GridView2.GridLines = GridLines.Both ' Style is added dynamically   Response.Write(style) Response.Write(sw.ToString...

change save as type from web page to excel for gridview exported to excel
Hi Everyone, I have a website set up with a button that exports gridviews on the pages to excel.  That works fine.  The users now have asked that when they then in excel go to save the excel (after viewing it), that the save as dialog box automatically uses the excel format, not web page format, and that is automatically switches to their "my documents" folder.  Can I do this?  Right now, the excel is fine, but when you click on File-Save As the file format (save as type) comes up as Web Page, and the name is in quotes.  If you just switch the format to ex...

Web resources about - Exporting data to Excel and protecting Excel worksheet - asp.net.web-forms

Protecting power - Wikipedia, the free encyclopedia
A protecting power is a state which somehow protects another state, and/or represents the interests of the protected state's citizens in a third ...

New York City Council To Consider Bill Protecting Employees’ Facebook Accounts
The New York City Council will consider a bill, introduced by Councilwoman Annabel Palma (D-Bronx), to ban employers from demanding access to ...

Link Shim - Protecting the People who Use Facebook from Malicious URLs - Facebook
Facebook Security hat eine Notiz mit dem Titel Link Shim - Protecting the People who Use Facebook from Malicious URLs geschrieben. Du kannst ...

The Inside Facebook Guide to Protecting Your Privacy on Facebook
Now that everyone from family to colleagues are connecting on Facebook, how do you continue sharing freely while maintaining your privacy and ...

Teddy bears: protecting innocent children from monsters-under-the-bed since 1902. - Flickr - Photo Sharing ...
Explore SUMMERMYASS' photos on Flickr. SUMMERMYASS has uploaded 1200 photos to Flickr.

JOHN TAVENER - THE PROTECTING VEIL for cello and orchestra - YouTube
Section 1 - Annunciation - The Incarnation - Lament of the Mother of God at the Cross - Christ is risen! - The dormition of the Mother of God ...

UAE must continue protecting local ecosystems to further enhance image
The UAE must continue protecting local ecosystems not only to preserve its natural heritage, but to further enhance its international image, ...

The Facebook filter protecting students from bullies (and themselves)
Posting to Facebook at 3am is rarely well-considered or necessary. But what if there was a sober voice of reason tapping you on the shoulder ...

Protecting Brisbane's brutal charm
Protecting Brisbane's brutal charm

Exclusive: Timor PM protecting 'corrupt' Cabinet members: report
The shock decision by East Timor's Prime Minister to expel foreign judges and advisers from the country appears to be connected to corruption ...

Resources last updated: 12/12/2015 3:41:43 PM