Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'd like to export a table I have on my webpage to an Excel file and lock some cells in the new file. Here is my current code:

Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader("content-disposition", "attachment;filename=EpicSearchResults.xls")
    Response.Charset = ""
    Me.EnableViewState = False

    Dim sw As New System.IO.StringWriter()
    Dim htw As New System.Web.UI.HtmlTextWriter(sw)

    Session("tblResults").RenderControl(htw)

    Response.Write(sw.ToString())
    Response.End()
End Sub

This code works in generating the Excel file. I've searched for an was unable to find much for a solution to lock cells in the new file. Thanks in advance for the help!

share|improve this question

2 Answers

up vote 4 down vote accepted

Like Leniel said, you probably need a better library. Another one you could check out is EPPlus. Works wonderfully.

The code in EPPlus to lock cells is worksheet.Cells["A1:G60"].Style.Locked = true;

share|improve this answer
Thanks for the suggestion. I'll take a look. – zeroef May 5 '11 at 19:21

I think you'll need a more powerful library to do what you want. This involves changing the way you currently export data to Excel.

Take a look at NPOI: http://npoi.codeplex.com/discussions/252597

References:

Create Excel (.XLS and .XLSX) file from C#

share|improve this answer
Thanks for the suggestion. I'll take a look. – zeroef May 5 '11 at 19:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.