Output Response in ANSI Characters format in ASP.net - asp.net

I am writing the data to the output browser using Response.write(some byte arrary)
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.OutputStream.Write(report, 0, report.Length);
Response.Flush();
Response.Close();
In my file I am having ANSI characters I need to write the information in the format ANSI when
i open my excel file.
Do we need to add anything.
I have encoded it to ANSI but its not working.

You say you're writing a byte array, but there's no such thing as an "ANSI" byte. A byte is a byte is a byte. You only need ANSI or any other encoding if you're writing strings or chars.
So if your report is a byte[] representing ANSI text, how have you encoded it?

Related

Qt - How detect possible invalid data loss with QTextStream readAll?

I am reading in a file that should be UTF-8 encoded using QTextStream::readAll(). If I attempt to open a corrupt UTF-8 file (or a binary file) I want to know that the data was not valid UTF-8.
I tried checking the status() after the read, but it did not indicate any abnormal condition.
I know I could read the whole file in binary mode and write a routine to check it myself, but it seems there should be an easier way, since the read has done all that UTF-8 conversion already.
You can use QTextCodec for this.
QTextCodec * QTextCodec::codecForUtfText(const QByteArray & ba, QTextCodec * defaultCodec)
From documentation:
Tries to detect the encoding of the provided snippet ba by using the
BOM (Byte Order Mark) and returns a QTextCodec instance that is
capable of decoding the text to unicode. If the codec cannot be
detected from the content provided, defaultCodec is returned.

Danish characters are replaced in CSV

My code to create a CSV file that contains Danish characters
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/csv;charset=utf-8";
Response.Charset = "utf-8";
Response.Write(sb.ToString());
Response.End();
Don't know why but when result CSV is created danish characters are replaced with some special characters like ??j.Can any one give me any clue?
Is the source text in the StringBuilder UTF-8? If not there is a chance that .NET gets confused and writes the wrong characters. If it isn't in UTF-8 try running Encoding.Convert before writing to the client.

FSO OpenTextFile with french characters

Using ASP's file system object (FSO), I'm trying to read a txt file with OpenTextFile that contains French characters (e and a with accents for e.g). Those characters come out wrong.
I tried specifying the format to TristateTrue to open the file as Unicode but to no avail.
I've been reading about using the ADO Stream object instead but I hoped there would be a way with FSO. Does anyone have any ideas?
Most likely the file is saved in UTF-8 encoding. The FileSystemObject does not handle UTF-8.
Either have the file saved as Unicode or use the ADODB.Stream object. The ADODB.Stream has a LoadFromFile method and does support UTF-8.
Dim s
Dim stream : Set stream = CreateObject("ADODB.Stream")
stream.CharSet = "UTF-8"
stream.LoadFromFile Server.MapPath("yourfile.txt")
s = stream.ReadAll
stream.Close

How to convert the encoding type of a stream in .NET?

I ve the following stream but I don't know its encoding type because the stream reader detects encoding from byte order marks
Dim reader As StreamReader = New StreamReader(respStream, True)
so how can I detect the encoding type AND convert it to another type? (for ex. from ASCII to UTF8)
PS:
What is the difference with the
following line?
Dim reader As StreamReader = New StreamReader(respStream,
Encoding.ASCII, True)
Thank You in advance,
Max
You can check the StreamReader's CurrentEncoding property, and you can write the string to a StreamWriter with a different Encoding.
To answer your unrelated question, that explicitly specifies the encoding.

How to add encoding information to the response stream in ASP.NET?

I have following piece of code:
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "text/rtf; charset=UTF-8";
context.Response.Charset = "UTF-8";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AddHeader("Content-disposition", "attachment;filename=lista_obecnosci.csv");
context.Response.Write("ąęćżźń󳥌ŻŹĆŃŁÓĘ");
}
When I try to open generated csv file, I get following behavior:
In Notepad2 - everything is fine.
In Word - conversion wizard opens and asks to convert the text. It suggest UTF-8, which is somehow ok.
In Excel - I get real mess. None of those Polish characters can be displayed.
I wanted to write those special encoding-information characters in front of my string, i.e.
context.Response.Write((char)0xef);
context.Response.Write((char)0xbb);
context.Response.Write((char)0xbf);
but that won't do any good. The response stream is treating that as normal data and converts it to something different.
I'd appreciate help on this one.
I ran into the same problem, and this was my solution:
context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
context.Response.Write("ąęćżźń󳥌ŻŹĆŃŁÓĘ");
What you call "encoding-information" is actually a BOM. I suspect each of those "characters" is getting encoded separately. To write the BOM manually, you have to write it as three bytes, not three characters. I'm not familiar with the .NET I/O classes, but there should be a method available to you that takes a byte or byte[] parameter and writes them directly to the file.
By the way, the UTF-8 BOM is optional; in fact, its use is discouraged by the Unicode Consortium. If you don't have a specific reason for using it, save yourself some hassle and leave it out.
EDIT: I just remembered you can also write the actual BOM character, '\uFEFF', and let the encoder handle it:
context.Response.Write('\uFEFF');
I think the problem is with Excel based on Microsoft Excel mangles Diacritics in .csv files. To prove this, copy your sample output string of ąęćżźń󳥌ŻŹĆŃŁÓĘ and paste into a test file using your favorite editor, and save as a UTF-8 encoded .csv file. Open in Excel and see the same issues.
The answer from Alan Moore
translated to VB:
Context.Response.Write(""c)

Resources