Guys, I have a problem here.

String FileName = "FileName.xlsx";
                 String FilePath = input.PostedFile.FileName;
                 System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
                 //response.ClearContent();
                 //response.Clear();
                 response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                 response.AddHeader("Content-Disposition", "attachment; filename=" + FileName );
                 //response.Flush();
                 response.End();

I want to download an excel file which is converted from csv plain text file. But everytime I download, I m unable to download excel and open although the download dialogbox is .xlsx. Any suggestion?

what is the error?

try change content type to:

Response.ContentType = "application/vnd.ms-excel";

however I'd still want to know the error its giving you

Thanks for reply guys. An error is occured while I try to open .xlsx file. It stated unable to Excel cannot open the filename.xlsx because the file format is invalid or extension....

While I try to open explicitly using notepad...I found that the content is html code..
Logically the .xlsx content should be some kind of words are unreadable...but it seems I download the html page code rather than excel file itself...any opinions?

hm... change the content type as i explained. Also, if you open the original file you are meant to download, does it work?

take a look at this method (in case you are exporting from database to excel to download)

export to excel - asp.net

Hi, jfarrugia.

The method u gv me are works..but not the result I wanted.Becoz my excel file now is contain those html button and design on my page rather than excel file data.

My data it seems cant be loaded. When I try my logic in window form. it works as I desire...any extra info?

what is the version of Dot Net Framework you are working with ?????
framework 2.0 doesn't support .xlsx format

hi mono_jit23,

I m using 4.0 dot net framework..

Have you tried changing the content type as i explained? change to application/vnd.ms-excel

And also, have you tried opening FileName.xlsx directly on the server?

Hi jfarrugia,

thanks for your reply. I did follow your method. and my excel downloaded is showing all render html code..like screen shot of my page rather than my uploaded 1.

server?I m using localhost now...not yet deploy

ok but when you double click on the file, does it open alright?

also, im using a similar app with the below:

Response.ContentType = "application/ms-excel"
Response.AddHeader("Content-Disposition", "inline;filename=" & PageName & ".xlsx")


and it seems to work OK.

Yea I did double click. I can open but the content is not what I uploaded..

The content is like screen shot of my page..
content that I have uploaded in csv format is unable to display successfully

oh so its an upload you need not a download.. correct?

I need download.sorry if i m misunderstanding..

I browse a file from computer and download it

String FilePath = input.PostedFile.FileName;

Hi I notice something. I seems not didnt use any code I have declared in this line.
Do you where I should put?

Sorry Typo. I didnt use*

Very few code required:


Control in html:
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Upload File" />&nbsp;<br />
<br />


Codebehind:

FileUpload1.SaveAs("C:\\Uploads\\" +
FileUpload1.FileName);

I have tried your method...with some code changing..but still unable to get .xlsx...only save in .csv..as my originally filetype is .csv

input.SaveAs("C:\\" + excelApp.ActiveWorkbook+".xlsx");

But i m able to open it successfully...just the extension filetype is csv..
I wish to get .xlsx..is that possible?

when uploading, you need to maintain the original file type, you can not simply change file type and expect the result to be as you expected, unless its a text based file type.

if my upload file type is csv and download file type wish to be .xlsx or .xls..so it is considered simple or advance?

its not a matter of being simple or complicated.. but you cannot just change content type and expect the conversion to happen automatically. The latest link i sent covers the conversion from excel to csv... im sure you can manage to change some of that code to change conversion from csv to excel.

Hi jfarrugia,

I still have the problem of downloading..in fact. I m downloading the webpage not the attachment...I google search a lot..but still the same thing happen..aiks

Guys, is there any opinion with my case?i m stucked for a few days d..hope some 1 could guide me..

My downloaded file still is html page...
I wish to get my data in excel but not html code..

thanks in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.