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!

zeroef
  • 1,949
  • 23
  • 32

2 Answers2

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;

Marcus
  • 5,407
  • 3
  • 31
  • 54
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#

Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480