Two files that are binary identical, yet exhibit different behavior - c#

I'm posting with tags asp.net and excel because that is the origination of my problem, but I'm not really sure this is the right place - ultimately, my problem is that I have two files (served by an ASP.Net application) which are identical based on a binary file compare using
fc /B A.xls B.xls
However, they exhibit different behavior: the first one opens fine in Excel; the second one does not. I conclude, then, that there is something different about the files beyond what the FC utility checks.
I have tried sending these two files to a friend to ask for his help, but discovered that when I do so, the problem file gets "fixed". In fact, if I do just about anything with this file, it gets "fixed". By fixed, I mean that it then opens fine in Excel. For example, if I zip it, then extract it from the zip, it is fine. If I open in Notepad++ and "Save As", it is fine. Same with Wordpad. Using plain old Notepad does NOT fix it.
So, obviously, there is some difference about these two files that I am missing.
I'm not sure if I will have any luck asking people to visit a random website, but if you want to see an example of the behavior, I have created a minimal page to duplicate the problem at http://rodj.me/ExcelTest
Click on the link for "MinimalHtml.aspx", and the app will serve an HTML based xls file using the following in the Page Load:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "filename=MinimalHtml.xls");
}
Depending on your browser and browser settings (my tests have been in Chrome), you may get Excel opened with a blank page. Regardless, you should get the file MinimalHtml.xls downloaded. It is a plain text file. You should find that this file will NOT open in Excel. However, if you zip the file, then extract it from zip, it WILL open.
I'm curious about what other file differences I'm missing when just doing an FC compare, but ultimately, I need to get the ASP.Net application corrected to serve the HTML version of the Excel file correctly. Interestingly, if I create an XML version of the spreadsheet, it downloads/opens fine. That is what the "MinimalXml.aspx" link does.
Can anyone help with either 1) how to figure out what is different about the two files; or 2) what must change in the ASP.Net application to get it to serve the file correctly?

I think your problem might be a Microsoft security patch. See this article:
Infoworld article
When you open the file directly, the patch causes the issue which results in a blank page because the file contents is HTML not Excel. When you download the file in a Zip file and unzip it, it is deemed safe and opens correctly.

Related

Get page count of RTF, TXT and XLS documents by OpenXml in C#

I want to count pages of the file with types of .rtf, .txt & .xls(x) through Open Xml.
One can say to use Office.InterOp assemblies (also to get counts of TXT & RTF and it works too). but I tried already and can't use as it takes so much time specifically on large files.
I've tried a lot but I couldn't or can say didn't get any reference on web.
By digging deep, for Excel files, I found something described on THIS site, but it just setups the page print settings.
Before printing, I want to have page count to be shown to user.
Also, I'm still trying to have any way for TXT & RTF files. Please share if this also can be via Open Xml and how.

Excel opening CSV with wrong encoding

This is partly a question for the Microsoft forums too, but I think there might be some coding involved.
We have a system built in C# .NET that generates CSV files. However, we have problems with special characters "æÆøØåÅ". The thing is, when I open the file in NotePad, everything is correct. But when I open the file in Excel, these characters are wrong. If I open in NotePad and save without actually doing any changes, it works in Excel. But I dont understand why? Is there some hidden information added to the file that can we adjusted in our C# code to make it correct in the first place?
There are other questions like this, but all answers I could find are workarounds for when you already have a wrong CSV file. In our case, we create this file, and the people we send the files too are usually not computer-people capable of changing encoding, etc.
Edit:
Here is the code we tried to use at the end, after generating our result CSV-string:
string result = "some;æøå;string";
byte[] bytes = System.Text.Encoding.GetEncoding(65001).GetBytes(result.ToString());
return System.Text.Encoding.GetEncoding(65001).GetString(bytes);

Upload file to server that is open

So I'm using WebClient to upload a file to a server. It works great except for one problem. If the file is opened by another program then it will not upload. For instance, if it's a word document that's been saved but is still opened by word then it fails to upload. Is there a way to force it to read whatever is there and upload it?
If i understand your question right then you can use this solution to test if the file is open and then close it if it is. It's a useful helper class i've used in the past.
https://stackoverflow.com/a/1247326/4612655

Generating proper CSV files

I'm having a problem programmatically generating a proper CSV file that is then downloaded by the user and opened in excel in my ASP.NET project. Excel seems to open the file properly but when I go to “save as” it defaults to Unicode text. I understand that CSV is basically a text file but if you try creating a CSV in Excel, saving, and then going to save as it will default the save as type to CSV. Therefore I believe something extra is being saved along with the file. I’ve made sure the HTTP header context-type is set to “text/csv” so I am sure that the response is correct to the user.
We generate a lot of CSV where I work, and I've noticed this a lot. There's a really good chance that your file is just fine.
The problem with CSV is that it's not defined by any standard, so every app interprets it slightly different. Excel probably does this for any CSV file which isn't precisely in its preferred format.
Maybe Excel expects CSV to be ASCII, and you've got a UTF BOM in the file which makes it decide tab-delimited "Unicode text" is a better fit.
This should work:
protected void btnDownload_Click(object sender, EventArgs e)
{
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
Response.ContentType = "text/csv";
Response.Write("1;computer;1000");
Response.End();
}
Have you looked at a binary dump of the file to make sure the file being downloaded is identical to the file you're looking at locally? There could be different line terminators being used (e.g. ) that might be causing Excel to tolerantly read it in and display it, but default to saving it as unicode text.
On a Linux (or cygwin) system, using "od -a -x" will tell you how the file is made up.

Enabling Macros in Excel using GemBox 3.1 API

Well recently having a problem with GemBox.SpreadSheet 3.1. Right now the program will write an excel file, but when opening the file it will attempt to open the file saying "Excel found unreadable content" then asks yes or no if you wanna try to recover the document. Click yes, it then says "The file is a macro-free file, but contains macro-enabled content".
However, when the program makes different files without the Macros, it does not say it is corrupt and works just fine.
So my question is, How do you enable the Macro content using GemBox API? Or is there just bad coding somewhere?
EDIT:
I figured out that the program was saving the wrong extension.
I figured out that that in order for Macro-enabled content, the file needs to be stored in XLSM, NOT XLSX!

Categories