During Asp. NET development, some list items are often exported as Excel for users to view and save, but file names and content are also frequently garbled. Recently, I have also received such a Bug. I have summarized several solutions for your reference. We hope that some of the following methods can solve your problem.
Program Logic and Problems
Click Export Excel at the front end, bind data to the GridView control from the background database, and save the content of the GridView control to a temporary file. Finally, read the data stream from the temporary file and output it to the foreground.
Problem: Garbled characters are displayed after the list is exported to Excel. The file name is garbled under IE6 and displayed normally under IE8.
Focus
In addition to program issues, you must first consider the following aspects:
Operating System
This includes the server and client operating systems. If you are using a Chinese environment, you are advised to directly install a Chinese operating system. For operating systems in English or other languages, you must install the East Asian character set in the region and language options, and select both the language and time zone in China. This can solve the problem that Chinese characters cannot be displayed in the English operating system.
Database
As with the operating system, it is best to directly install the Chinese version of the database. In addition, you should select the field type for running Chinese characters. When inserting Chinese characters into the database, remember to add "N" before the string, as shown in figure
Insert into MyTable (name, nickname) values ('gary Zhang ', n' ');
"Some data content is garbled" because of the images generated in different databases, but the database issue is not thoroughly checked, so we will not discuss it for the moment.
Browser support
Different browsers also have different file name encoding methods. Therefore, you must pay attention to the support of different browsers During encoding, or whether it is because of garbled characters caused by browser problems. For example, common browsers display garbled characters on the names of files containing Chinese characters. At present, this file name in IE6 garbled problem has not been solved, that is, after the UTF-8 Encoded chinese file name in IE6 directly open the file name will show garbled, and save without this problem. In IE8, no problem occurs.
For more information about the support for Content-Description in different browsers, see how to use UTF-8 encoding in download file names.
IE6 |
Attachment; filename = "<URL-encoded UTF-8 File Name>" |
FF3 |
Attachment; filename = "UTF-8 file name" |
Attachment; filename * = "utf8'' <URL-encoded UTF-8 File Name>" |
O9 |
Attachment; filename = "UTF-8 file name" |
Safari3 (Win) |
Seems not supported? None of the above methods works. |
Unified Encoding
The entire process of exporting Excel data files may involve data entry into the database in the early stage-> data filling controls such as GridView or generating corresponding Table tables-> converting to stream and outputting. Throughout the process try to use a unified common encoding methods such as UTF-8, to name in character conversion process leading to garbled characters.
Development Method
After checking that all the above concerns are correct, check the output code of the program and find no problems. Some code of the program is as follows:
HttpResponse response = Page. Response;
Response. Clear ();
Response. ContentType = "application/octet-stream ";
// Use the UTF-8 to encode the file name
Response. AppendHeader ("Content-Disposition", "attachment; filename = \" "+ HttpUtility. UrlEncode (disFileName, System. Text. Encoding. UTF8) + "\"");
Response. ContentType = "application/ms-excel ;";
I tried to set charset and other attributes for the response object (in fact, I used UTF-8 encoding to convert it to stream ). As expected, the results still fail.
Response. Charset = "UTF-8 ";
Response. ContentEncoding = System. Text. Encoding. UTF8;
Response. HeaderEncoding = System. Text. Encoding. UTF8;
Ultimate Solution
Almost all Excel garbled solutions are found. There is no way to try it all over again. Suddenly, when switching the webpage, I found that the Google page can be viewed, but the Excel file I opened with IE that changed the suffix is not readable. Suddenly, you can use <meta> to declare which encoding the browser should use to display the page. Therefore, you can add a statement when exporting data.
In fact, the data exported using the DataGride control is actually a table. It is the same as the HTML Tag. Therefore, since Excel can recognize HTML Table labels, it should also be able to recognize HTML declarations. In this way, Excel will know what character set to use to parse the content. The output is as follows:
<Meta http-equiv = "content-type" content = "application/ms-excel; charset = UTF-8"/>
<Table>
<Tr>
<Td> cbcye@live.com | http://www.cbcye.com </td>...
</Tr>
<Table>