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
 
Please zip up the file that you downloaded.
 
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

Is that your complete code? How are you authenticating when the server responds with a 401? Are you sure you are not accidentally downloading the 401 authentication challenge page?

In your newer code, you indicate the content type that you expect. Where do you do that in your old code?

As an aside, the HttpClient uses the HttpWebRequest internally. You could have stuck with your original code which is the modern way of doing things. The HttpWebRequest is the older way.

As for tools, all .XLSX files are just ZIP compressed files. If you change the file extension from .XLSX to .ZIP, you can then extract or expand the contents. You should see some .XML files inside.

To validate that the file you first downloaded is a ZIP file, you can use any hex editor, or use PowerShell's Format-Hex command. That first few characters should be "PKZIP" if I remember correctly.
 
Normally this happens because people write code that calls some url, and saves the file, then then run it and “bingo! A file is saved!”

Then they open it in excel and get a “content doesn’t match extension”

When advised to open it in a text editor or (better) a hex editor, we usually see a block of HTML saying “you are not logged in; log in in order to access this file” or similar

Easy to forget that your browser is logged in, submitting cookies etc, you retrieve the url of the file from the browser, paste the url into the code, run the code, it downloaded something! Yeah, but it’s not the right thing.. C# just blindly saves the data the server sent, which doesn’t have to be the file you wanted

Need to make the code behave like the browser, and that might be submitting login details, retrieving a cookie, submitting the cookie with the request for the file..

Whatever the browser does (use F12 dev tools to see what it does) for starters we need to mimic that in code
 
thanks cjard and skydiver those are good points, hence can someone please guide me on how to call a http SOAP request, get an http SOAP response as a excel, all help is appreciated
 
Sounds like you need to use something like Postman to see what exactly is being used to create the HTTP request and then replicate that through code. That's the approach I always used and it hasn't failed me yet. Once you have the corret HTTP request and get Postman to download the correct file then you have a blueprint to code from.
 
I usually use Fiddler or the browser DevTools to watch the traffic, then I use PostMan to verify that I can replicate the traffic, and only then move on to using code. The only time I skip the PostMan replication step is if I figure out that there is a very short time to live for any of the response tokens, and I can't do any of the follow-up manually quickly enough.
 
Postman, Insomnia and other rest tools might well generate C# for you (I know insomnia does; i moved away from Postman years ago)

I was looking for an example but I will see what I can create by writing the C# code
 
If it's a soap service it may have a description document that can be used to programmatically generate all the c# you need. Take care not to buy a dog and bark yourself
 
If it's a soap service it may have a description document that can be used to programmatically generate all the c# you need. Take care not to buy a dog and bark yourself

Sorry I don't think I follow or maybe it is due to the fact I have limited knowledge about SOAP, I know a soap is a a combination of XML and a HTTP request, I assumed if one can get the http request and http response working, then the only add-on is the XML envelop which contains all required parameters will be sent as a byte stream in the getrequeststream method but then again i do have limited knowledge
 
Let's step back. Where did you get the URL "https://www.testurl.com/report"? Where did you get the user name and password for logging on to that site? They should have the information about that URL and whether it exposes a SOAP or REST API.
 
Let's step back. Where did you get the URL "https://www.testurl.com/report"? Where did you get the user name and password for logging on to that site? They should have the information about that URL and whether it exposes a SOAP or REST API.


Ok if it has information like this below, how would one approach things, kindly note I am referencing a tutorial I am currently reading on SOAP




Soap request


requestinfo:
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPrice>
    <m:StockName>IBM</m:StockName>
  </m:GetStockPrice>
</soap:Body>

</soap:Envelope>

soap response

responseinfo:
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPriceResponse>
    <m:Price>34.5</m:Price>
  </m:GetStockPriceResponse>
</soap:Body>

</soap:Envelope>
 
This seems to be an exercise in misdirection; that SOAP request appears to be an example from the w3schools tutorial. It is definitely not a SOAP dialogue that involves the requesting and receipt of an excel binary file

We're trying to help you with the actual problem you're having, so that means you need to post details about it specifically.. not paste in things completely unrelated to it
 
Back
Top Bottom