Solved

EXPORT TO EXCEL READ ONLY FILE FROM .NET

Posted on 2010-09-02
12
1,515 Views
Last Modified: 2013-11-27
Hello,
I have the code to export to excel from .net as below.I need help on how to export a read only file that is the exported file should be read only.
Thanks all.
public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
        "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";      
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();
                table.GridLines = GridLines.Both;
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }
                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }
                //  render the table into the htmlwriter
                table.RenderControl(htw);
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
0
Comment
Question by:Star79
12 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 33608099
I do not think it is possible with your current code, because your code does not have acces to security permissions on the client machine.
If you required this, I will suggest to export the grid to the server first, make the file read/only and then export this excel file(server) to the clients browser.
0
 

Author Comment

by:Star79
ID: 33617489
Hello,
Can you pls provide me with the code that does this kind of export.
Thanks a lot.
0
 
LVL 33

Expert Comment

by:Norie
ID: 33629398
Are you sure that code actually exports to Excel?

Isn't it something to do with creating an HTML file?

I know Excel is mentioned at the ContentType for the HTTP request, but is it actually an Excel file being created?

If it is creating an Excel workbook then I think you would have to access the file and it's permissions to make it read-only.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:Star79
ID: 33639514
The above code creates the excel sheet.But pls let me know how to access the file and set its read-only attribute
0
 
LVL 33

Expert Comment

by:Norie
ID: 33639584
Well I honestly don't know - it really depends on what OS you are running and perhaps other things.

You could open the created file in Excel and save it as 'read only recommended'.

Is that what you want to do?

By the way why do you want the file to be read only anyway?
0
 

Author Comment

by:Star79
ID: 33640832
Hello,
yes I want to open the created file in excel and save it as 'read only".Can you pls provide that code.
The user requirement is such that they do not want the excel file to be modifiable for reconciliation purpose.Its definitely a strange requirement.
Thanks
0
 
LVL 33

Expert Comment

by:Norie
ID: 33641861
So what can be done with the workbook?

How about just protection the contents using Protection...?

You should be able to do that in code and it might be a better idea than opening, making read-only etc.

Even better perhaps - distribute the file in a format that isn't easily edited.
0
 

Author Comment

by:Star79
ID: 33646661
Please let me know how it can be done.
Thanks.
0
 
LVL 16

Accepted Solution

by:
13598 earned 500 total points
ID: 33674912
If it is not a requirement that it is an Excel file and you want it to be read-only then you are better off exporting to pdf instead. Try something like this in your code:
HttpContext.Current.Response.ClearHeaders();
 HttpContext.Current.Response.ContentType = "application/pdf";        
 HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename=" + fileName + ".pdf");
0
 

Author Closing Comment

by:Star79
ID: 33898376
I wanted an excel file rather than a pdf but it solved the issue
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question