将希腊字符写入excel文件使用C# [英] Write greeks characters to excel file use C#

查看:118
本文介绍了将希腊字符写入excel文件使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

=

<pre>           string attachment = "attachment; filename=download.xls";
                Response.ClearContent();
                Response.AddHeader("content-disposition", attachment);
                Response.ContentType = "application/vnd.xls";

                //Response.BinaryWrite(UTF8Encoding.UTF8.GetBytes(attachment)); 

                Response.Charset = UnicodeEncoding.UTF8.WebName;
                Response.HeaderEncoding = UnicodeEncoding.UTF8;
                Response.ContentEncoding = UnicodeEncoding.UTF8;

                string tab = " ";
                foreach (DataColumn dc in dt.Columns)
                {
                    Response.Write(tab + dc.ColumnName);
                    tab = "\t";
                }
                Response.Write("\n");
                int i;
             
                UTF8Encoding utf8 = new UTF8Encoding();
                foreach (DataRow dr in dt.Rows)
                {
                   
                    tab = "";
                    for (i = 0; i < dt.Columns.Count - 1; i++)
                    {
                        
                        Console.WriteLine(dr[i].ToString());
                        tab = "\t";
                    }
                    Response.Write("\n");

                }
                Response.End()





我的尝试:



我想用希腊文字符导出excel文件...为什么不工作???



What I have tried:

I want to export an excel file with greeks characters ...why not working???

推荐答案

我找到..







I am find..



string attachment = "attachment; filename=download.xls";
              Response.ClearContent();
              Response.AddHeader("content-disposition", attachment);
              Response.ContentEncoding = System.Text.Encoding.UTF32 ;
              Response.BinaryWrite(System.Text.Encoding.UTF32.GetPreamble());
              Response.ContentType = "application/vnd.xls";







谢谢:)




thanks :)


At一个猜测,没有运行代码,我会说你使用不正确的编码。你可以在这里阅读: .NET中的字符编码Microsoft Docs [ ^ 。我认为你需要使用UTF16或UTF32。
At a guess, without running the code, I would say that you are using the incorrect encoding. You can read about it here: Character Encoding in .NET | Microsoft Docs[^]. I would think that you will need to use either UTF16 or UTF32.


写一个BOM( Byte订单标记 - 维基百科 [ ^ ])在任何文本之前以便接收应用程序(如Excel)知道内容是UTF-8编码的:

Write a BOM (Byte order mark - Wikipedia[^]) before any text so that the receiving application (like Excel) knows that the content is UTF-8 encoded:
Response.BinaryWrite(UnicodeEncoding.UTF8.GetPreamble());

您还要创建一个TAB分隔文本文件(一个CSV文件,其中TAB为分隔符而不是逗号)。然后,您不应将其声明为Excel文件( application / vnd.xls )并使用 .xls 文件扩展名。改为使用 application / octet-stream .txt 扩展名。



另请注意,您实际上并非将内容写入流但是写入控制台。

You are also creating a TAB separated text file (a CSV file with the TAB as separator instead of the comma). Then you should not declare it as Excel file (application/vnd.xls) and use the .xls file extension. Use application/octet-stream and a .txt extension instead.

Note also that you are actually not writing the content to the stream but to the console.


这篇关于将希腊字符写入excel文件使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆