Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

EXPORT TO EXCEL READ ONLY FILE FROM .NET

Posted on 2010-09-02
12
Medium Priority
?
1,658 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 35

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 35

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 35

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

636 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