Solved

EXPORT TO EXCEL READ ONLY FILE FROM .NET

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

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find word and 6 digit number 22 95
Tags from access to excel 3 26
Server Error 11 47
Excel 2016 - Black cell borders 11 26
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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

932 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