?
Solved

EXPORT TO EXCEL READ ONLY FILE FROM .NET

Posted on 2010-09-02
12
Medium Priority
?
1,612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 36

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 34

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 34

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 34

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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

765 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