4

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!

2 Answers 2

4

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;

Sign up to request clarification or add additional context in comments.

Comments

2

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#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.