Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:30:59+00:00 2026-06-10T23:30:59+00:00

I’d like to know if it is possible to set the Excel output as

  • 0

I’d like to know if it is possible to set the Excel output as “Locked”, in the sense that when we try to change a Cell’s value, then there will be a warning indicating that we can not change it unless we remove the Sheet’s protection.

I know that we can develop a custom Excel automation code, and set a Password to protect the sheet just before we save it. But, is there any easy way to accomplish this using ReportViewer’s built-in feature?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:31:00+00:00Added an answer on June 10, 2026 at 11:31 pm

    After doing some research, I’ve managed to find the solution 🙂
    The idea is to intercept the Export Report function of ReportViewer, and then run our own process. This process will get output which is the Excel file being generated, and then read it and apply whatever changes necessary, and save it again before it is sent as a Download to User.
    It should be noted however, that the interception method will be different based on what type of Reporting that we use. In my case, my ReportViewer is using WebForm instead of WinForm, and most explanation out there is explaining about ReportExport event which is only available in WinForm.

    For those using WinForm, you can override the ReportExport event like this :

    void reportViewer_ReportExport(object sender, Microsoft.Reporting.WinForms.ReportExportEventArgs e)
    {
        e.Cancel = true;
        // insert your own code to export excel
    }
    

    In WebForm, there is no event handler of ReportExport. The options that I can think of are creating a custom button in .aspx that will execute our custom code, or directly render the excel without needing to preview the report.
    I decided to render the excel file directly. I will use a dataset and get the data from Stored Procedure. Then, I assign the dataset into RDLC and call the Render method to get the output. The output format is in byte[], and I use FileStream to write it. After it is done, I open the Excel file using Interop and apply protection. Here is the code :

    // Setup DataSet (Adapter and Table)
    YourTableAdapters.ATableAdapter ds = new YourTableAdapters.ATableAdapter();
    YourDataSet.ADataTable dt = new YourDataSet.ADataTable ();
    
    ds.Fill(dt, outlet, period);
    
    // Create Report DataSource
    ReportDataSource rds = new ReportDataSource("DataSet1", (System.Data.DataTable)dt);
    
    // Variables needed for ReportViewer Render method
    Warning[] warnings;
    string[] streamIds;
    string mimeType = string.Empty;
    string encoding = string.Empty;
    string extension = string.Empty;
    
    // Setup the report viewer object and get the array of bytes
    ReportViewer viewer = new ReportViewer();
    viewer.ProcessingMode = ProcessingMode.Local;
    viewer.LocalReport.ReportPath = "YourReport.rdlc";
    viewer.LocalReport.DataSources.Add(rds); // Add datasource here
    
    byte[] bytes = viewer.LocalReport.Render("Excel", null, out mimeType,
                                              out encoding, out extension,
                                              out streamIds, out warnings);
    
    // Prepare filename and save_path, and then write the Excel using FileStream.
    String temp_path = Path.Combine(Server.MapPath(Config.ReportPath), "FileName.xls");
    FileStream fs = new FileStream(temp_path, FileMode.Create);
    fs.Write(bytes, 0, bytes.Length);
    fs.Close();
    
    // Open the Excel file created, and add password protection.
    PIDExcel pidexcel = new PIDExcel();
    pidexcel.CollectExcelPID();
    
    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Range lock_range = null;
    
    int excelid = pidexcel.GetNewExcelID();
    
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook = null;
    
    try
    {
        //xlApp.Visible = true;
        xlWorkBook = (Microsoft.Office.Interop.Excel.Workbook)xlApp.Workbooks.Open(temp_path,
                      Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                      Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                      Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                      Type.Missing, Type.Missing);
    
        foreach(Microsoft.Office.Interop.Excel.Worksheet displayWorksheet in xlApp.ActiveWorkbook.Worksheets)
        {
            lock_range = xlApp.Cells;
            lock_range.Select();
            lock_range.EntireColumn.Locked = true;
            displayWorksheet.Protect("<your password here>");
        }
    
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message.Replace("'", "")); ;
    }
    finally
    {
        // Set First Sheet Active
        xlWorkBook.Sheets[1].Select();
        xlApp.DisplayAlerts = false;
        xlWorkBook.Save();
        xlWorkBook.Close(Type.Missing, Type.Missing, Type.Missing);
        xlApp.Quit();
    
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkBook);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp);
    
        GC.WaitForPendingFinalizers();
        GC.Collect();
    
        pidexcel.KillExcel(excelid);
    
    }
    

    By using this concept, I can easily design the report output since I’m using RDLC as a template to populate the data supplied by SP, and then render it. Imagine the hassle if we manually code the report using Excel (setting borders, merge cells, groupings).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.