Solved

EXPORT TO EXCEL READ ONLY FILE FROM .NET

Posted on 2010-09-02
12
1,469 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Star79
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Star79
Comment Utility
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
Comment Utility
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
Comment Utility
Please let me know how it can be done.
Thanks.
0
 
LVL 16

Accepted Solution

by:
13598 earned 500 total points
Comment Utility
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
Comment Utility
I wanted an excel file rather than a pdf but it solved the issue
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now