excel extension issue

nofu1

Active member
Joined
Oct 30, 2020
Messages
37
Programming Experience
Beginner
I have a code similar to this below

C#Copy

C#:
using(webclient wc = new WebClient())
{
string fileinfoname = "test_one";
string pathinfo = @"C:\testfolderinfo" + fileinfoname + ".xlsx";

wc.downloadFile("www.testurl.com/report", pathinfo);

}
I am currently experiencing a situation where the file is being downloaded but when I try to utilize excel to open the file, it gives the message

"Excel cannot open the file test_one because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file"

This just started happening, hence how do i get an actual excel extension for excel 2007 and above to replace it above.

Thanks in advance
 
Are you sure you are opening the right file? You seem to be trying to open "C:\testfolderinfotest_one.xlsx". You seem to be missing a backslash in that path. Did you mean to download and open "C:\testfolderinfo\test_one.xlsx" ?

Are you sure that downloading from "www.testurl.com/report" will actually download a Excel 2007 spreadsheet?
 
Are you sure you are opening the right file? You seem to be trying to open "C:\testfolderinfotest_one.xlsx". You seem to be missing a backslash in that path. Did you mean to download and open "C:\testfolderinfo\test_one.xlsx" ?

Are you sure that downloading from "www.testurl.com/report" will actually download a Excel 2007 spreadsheet?
Sorry it was a typo but yes I am sure it is downloading a excel spreadsheet because I tested it manual at the source
 
Can you upload that file that you downloaded?
 
Yes.

Or share the user name and password to that URL where you are downloading from.
 
Yes.

Or share the user name and password to that URL where you are downloading from.

Please see the attached file. Kindly note, I am only trying to download the test file for now since my above code is not working well
 
Last edited:
Sorry. Not seeing an attachment.
 
Sorry. Not seeing an attachment.

I get the message the uploaded file does not have an allowed extension. The following extensions are allowed: .zip, .txt, .pdf, .png, .jpg, .jpeg, .jpe, .gif, .m4v, .mov, .mp4, .mp4v, .mpeg, .mpg, .ogv, .webm, .mp3
 
Put the .xlsx file into a .zip file and then upload the .zip file.
 
So I spent time re-writing the code to use HTTPweb request but unfortunately I am still giving the same error. Please see the other code below

code1:
HttpWebRequest fr = (HttpWebRequest)WebRequest.Create("www.testurl.com/report");

fr.Method = "GET";

HttpWebResponse fresponse = (HttpWebResponse)fr.GetResponse();

StreamReader srd = new StreamReader(fresponse.GetResponseStream());

string webpageinfo = srd.ReadToEnd();

sr.Close();



String urlinfo = "www.testurl.com/report"

HttpWebRequest hrrequest = (HttpWebRequest)WebRequest.Create(urlinfo);

hrrequest.method = "POST";

hrrequest.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";



Stream sdatainfo = hrrequest.GetRequestStream():

sdatainfo.Close();



using(WebResponse resp = hrrequest.GetResponse())

{

   using(sdatainfo = resp.GetResponseStream())

   {

       FileStream fs = File.Open(@"C:\testfolderinfo", FileMode.OpenorCreate, FileAccess.Write);

       sdatainfo.CopyTo(fs);

       fs.Close();

        sdatainfo.Close();

        resp.Close():



   }



}
 
The file in the .ZIP file opened fine for me:
1686087261976.png
 
I mean when you using webclient to download it, it would show up the message it is corrupt, I sent you a manually downloaded file to show it is purely a xlsx file
 
By the way, do you happen to know if there steps to see the details of the files that could help one tweak the code
 

Latest posts

Back
Top Bottom