.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Export to Excel gives error before opening file

Posted By:      Posted Date: October 21, 2010    Points: 0   Category :ASP.Net

I have a web page with a gridview control and a button for Exporting to Excel

I get following error when exporting to Excel.

"The fiel you are trying to open, 'someFileName[1].xls, is in a different format than specified by the file extension. Verify that file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"

Clicking Yes opens the file. I would like to avoid showing the above error to the users.

Following code is used from the click event. I've left the commented code here to show what I've tried based on results from internet search. I have Microsoft Office Excel 2007 on local PC.

public static void ToExcel(DataTable dt, string fileName)
			string attachment = "attachment; filename=" + fileName + ".xls";

			GridView gvExcel = new System.Web.UI.WebControls.GridView();
			gvExcel.AutoGenerateColumns = true;
			gvExcel.DataSource = dt;

			//HttpContext.Current.Response.AddHeader("content-disposition", attachment);
			//HttpContext.Current.Response.ContentType = "application/ms-excel";
			HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

View Complete Post

More Related Resource Links

Error opening an excel file

I'm getting this error while trying to open an excel file from my application. I tried this http://blog.crowe.co.nz/archive/2006/03/02/589.aspx but it didn't help. Excel itself opens normally, but it does not from my application. here is a screenshot of my code:
        case "xls":
          System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("ru-RU");
          Microsoft.Office.Interop.Excel.WorkbookClass wb = new Microsoft.Office.Interop.Excel.WorkbookClass();
          wb.Application.Visible = true;
          wb.Application.Workbooks.Open(fileName.ToString(), updateLinks, readOnly,
            missing, missing, missing, missing, missing,
            missing, missing, missing, missing, missing,
            missing, missing);
I didn't find a solution in another threads. Please help me... this option is cruicial in my application. Thank you!


Excel file getting error


when i am trying to save a excel document in library it getting error like "The file cannot be saved because some properties are missing or invalid"

If anyone know the solution for this pls tell me.



How to do wide search and export the records to excel file in ASP.net C# ?


This is my MYSQL  database connection :-

public static OdbcConnection Connection = new OdbcConnection("Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=jdmcrm; User=root;Password=admin;");

My table name is "customer" and  fields liek customerId , companyName , address and date ;

How to do wide search and export the records to excel file ? im quite new in asp.net .

Pls advise . Thanks





WSS2.0 opening pdf documents an error message appears: The document could not be saved. The file may

Hi All,. In a WSS2.0 document library, a user is trying to open pdf files. However, an error message is displayed: 'The document could not be saved. The file may be read only, or another user may have it open. Please save the document with a different name or in a different folder'. No one else has the file open. When the user saves the file using Save As (as it is apparently Read Only) and then saves the file back to the SharePoint site, all previous versions of the file disappear. The user has noted that this has been happening since today, and she had some other network issues that have been resolved. No other users are experiencing this problem.  Not sure if this problem is related to the network problems the user was facing. I've had the user reboot but this has not resolved it. Any ideas?

Need an example of error in OLE DB Source component when source is Excel file.

I want an example of error (not truncation) in individual row or individual cell that can occur in OLE DB Source component when source is Excel file. I'm trying out Error Output of data task component.  I have already tried out Error Output of OLE DB Destination component.

Unable to export data to an excel file in grid format!!!

I'm exporting data to an excel sheet from the ojects list. The data is exporting to excel sheet well, but I'm unable to see the grid format for the data. The data is looking with out the rows and columns lines. I think I'm missing to set some property or somethnig ehwn exporting.   Any help would be really appriciated.

How do you freeze the tablix header row in an Excel export file in SSRS 2008?

So this is totally killing me. I've found out how to make a tablix header row repeat on each page of a PDF export file in SSRS 2008 (which I won't even get started on because all I can say is that 2005 is way better), but I cannot figure out how to make a simple header row of a tablix freeze in an Excel export file.  The tablix is right at the top of the body and spans the entire width of the design area.  How in the world do you get it to freeze in the Excel export file?

SharePoint 2010 Export list Items to Excel, wrong url in iqy file

Hi, I have a SharePoint 2010 Environment that uses https://portal.<something>.com. Exporting to Excel of a list uses wrong URL. When I go to the a list, select the list to show the "office ribbon" and then select export to excel, excel will show the error "Excel cannot connect to the SharePoint List". When I take a look at the IQY file that is created it contains the wrong url: http://portal.<something>.com instead of https://portal.<something>.com Changing the URL in the IQY file manualy to https://portal.<something>.com and then opening it will excel is working fine, list data is exported to excel then. Alternate access mapping is configured to use https://portal.<something>.com as default, export to access is working normaly. I already deleted the http://portal.<something>.com alternate access mapping once to see if this fixes the problem. No fix, even not after IISreset or rebooting the server. Where or how is the export to Excel IQY file created and where is it possible to fix the URL it is putting in the IQY file? Grtz, Arjan

Opening a PowerPoint presentation fails with error "invalid path or file name".

When you go to a Document Library and try to open the presentation, PowerPoint opens, but then you get the error "The path or file name for "file URL" is invalid.  Please check that the path and file name are correct."I installed MOSS/WSS SP2 about 2 weeks ago, this happened once or twice before then, but I now know of about a half-dozen presentations that won't open.There is a way to get the file to open, if you hit the drop-down next to the file and select Send To and Download a Copy, you can save or open it from there.If you try browsing to if from within PowerPoint, it won't open either, and will also not open in Explorer View.  We have replaced the file, saved as a different name, etc. with no luck.Using both IE 7 and IE 8 and both XP Pro and Vista Business workstations.Any ideas out there?Thanks,Rusty

Error Server Unavailable when try to export it to Excel



I am using SSRS 2005 Version 9.00.4035.00

When i run a query and the query results in 105122 row and when i try to export to it excel I get an

Server Application Unavailable

The web application you are attempting to access on this web server is currently unavailable.  Please hit the "Refresh" button in your web browser to retry your request.

Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server. Please review this log entry to discover what caused this error to occur.

In my group i have added =Ceiling(RowNumber(Nothing)/65000) so that it goes to the next page but it doesnt work


any help will be appreciated




Error when opening Excel document from sharepoint


Hi, I am using Sharepoint 2010 and Excel 2010 x64.

I created a document library where I host a simple excel file for testing. When I click on it , it opens in the web browser just fine.

When I chose to open it with Excel , I get this error : The document could not be opened for editing. A microsoft Sharepoint Foundation compatible application could not be found to edit the document.

I already tried as well to do a check out before opening it and it still doesn't work.

Any ideas ?

PS : same thing happens with Visio 2010.


Data Export to Excel file using "Response.ContentType = "application/csv"


I have developed code to export data to Excel using Response.ContentType = "application/csv". The output Data Excel file loaded with 65536 Rows only, this file is not loaded completely since the limitation of excel is 65536. My Recordcount is more than 700000.

Please any one let me know how I can Export Data to Excel file over than 65536 Rows.

Email: thamaraiselvann@ayso.org

can I use SQL Server to export an excel file directly?


I just want to know except use owc

can we use SQL Server to export excel file ?

if it can be , how to do? thanks alot

When i export a MS Excel file generated By SSRS 2008 i'm not able to change the sheet name (Sheet 1

When i export a MS Excel file generated By SSRS 2008 i'm not able to change the sheet name (Sheet 1,Sheet 2,Sheet 3) dynamically.So Please try to give me a solution.

When i export a MS Excel file generated By SSRS 2008 i'm not able to change the sheet name (Sheet 1



When i export a MS Excel file generated By SSRS 2008 i'm not able to change the sheet name (Sheet 1,Sheet 2,Sheet 3) dyanamically.Sp Please try to give me a solution.

Error opening excel files stored in SharePoint 3.0 from VS2010 (TFS2010 setup)



First I hope this is the correct forum for te below question, and if not, please excuse and if possible divert me to the correct place.

We have a TFS2010 setup with (what I managed to identify) Sharepoint Services 3.0.
We have 2 users working in the same collection, and also share some of the projects in it with the below problems:

1. User A has Office 2007. She is unable to "edit workbook" on an excel file directly from VS2010, and gets an error of 'Unknown error trying to lock file'. I searched this around and didn't find a solution, but did find a workaround - If she checks out the file directly from the SharePoint site, she is able to edit the file, and in fact is able to later "edit workbook" directly from VS2010 (as long as she once checked it out through SharePoint).

2. User B has Office 2010. When he tries to "edit workbook", he gets an error of 'A problem occurred while connecting to the server. If the problem continues, contact your administrator'. When he goes directly to the SharePoint, he is able to check out the file, open it in Excel 2010, but is not able to check it back in (gets an error that the file cannot be located or so, I don't have the exact error). This means that this user cannot work with the SharePpoint stored files at all, and currently works on local copies on hi

Error when trying to export gridview to Excel - RegisterForEventValidation can only be called during

I have a pretty basic vb.net page that has textbox control on it and a "generate" button that when clicked, fills a datagrid.  That all works great, but when the user clicks the "Export to Excel" button, I get this error.. RegisterForEventValidation can only be called during Render(); 

Here's my code behind, I thought I included everything I needed...

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub

Private Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("content-disposition", "attachment;filename=" & strFileName)
        Response.Charset = ""
        Me.EnableViewState = False
        Dim StringWriter As New System.IO.StringWriter()
        Dim HtmlTextWriter As New System.Web.UI.HtmlTextWriter(Str

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend