ExpertXls Excel Library ASP.NET Web Forms demo · C#

Workbook Security

Protects a worksheet so that only the unlocked input cells can be edited, hides a formula, protects the workbook structure and, for the .xls format, sets a password to open the file.

Select the format and press the button: the workbook is created on the server and downloaded by the browser.

Workbook format   
Password to open the workbook (.xls only)

Create, edit and read Excel workbooks in ASP.NET: the workbooks are created on the server and sent to the browser. The code of each workbook is in App_Code.

C# code of this sample

WorkbookSecurity.aspx.cs
using System;
using System.Data;
using System.IO;
using System.Web.UI;

using ExpertXls.ExcelLib;
using ExcelLibraryDemos;

/// <summary>
/// Workbook Security: the workbook code is in App_Code\WorkbookSecurityDemo.cs.
/// </summary>
public partial class WorkbookSecurityPage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void buttonCreate_Click(object sender, EventArgs e)
    {
        ExcelWorkbookFormat format = radioXlsx.Checked ? ExcelWorkbookFormat.Xlsx_2007 : ExcelWorkbookFormat.Xls_2003;
        byte[] workbookBytes;
        try
        {
            // create the workbook in memory; the input files are in the folders of the web site
            using (MemoryStream stream = new MemoryStream())
            {
                WorkbookSecurityDemo.CreateWorkbook(format, Server.MapPath("~"), stream, textBoxPassword.Text);
                workbookBytes = stream.ToArray();
            }
        }
        catch (Exception ex)
        {
            labelError.Text = "The workbook could not be created: " + Server.HtmlEncode(ex.Message);
            return;
        }
        WorkbookDownload.Send(Response, workbookBytes, WorkbookSecurityDemo.OutputName + DemoLayout.GetExtension(format), format);
    }
}