admin管理员组

文章数量:1658441

 在平时打开CSV文件或者将CSV文件导入数据库的时候,经常遇到文档中的中文显示乱码,主要问题就是编码出现了问题。

 在简体中文Windows操作系统中,ANSI 编码代表 GB2312编码,有的CSV文件从网页上导出时默认是ANSI编码,而系统默认打开是UTF8编码,导致打开后中文出现乱码。

遇到这种问题我们不要慌,最简单的方法是将CSV文件用TXT打开,然后另存为,在保存按钮的左侧可以修改CSV文件的编码方式,修改后再用Excel打开CSV,中文就恢复正常了。

但遇到多个文件或文件过大时,就不适合这样一个一个修改了。

经多次摸索,我搞定了C#程序自动转换编码的小程序,可以一次自动转换多个CSV文件或TXT文件的编码,程序界面如下:

点击按钮,可以一次选择多个要转换的CSV或TXT文件,程序将自动转换成目标编码。需要的小伙伴可以免费下载使用。

以下是程序的全部代码,很好理解。

using System.Text;
using System.Windows.Forms;

namespace 编码转换
{
    public partial class 编码转换 : Form
    {
        public 编码转换()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog dialog = new()
            {
                //该值确定是否可以选择多个文件
                Multiselect = true,
                //对话框标题
                Title = "请选择一个或多个txt或csv文件",
                //打开文件的类型
                Filter = "Files|*.txt;*.csv"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                int i = 0;
                try
                {
                    foreach (string s in dialog.FileNames)
                    {
                        string last = s.Substring(s.Length - 3, 3);
                        //获取保存路径
                        string? path = System.IO.Path.GetDirectoryName(s);
                        //获取原文件名称
                        string? filename = System.IO.Path.GetFileNameWithoutExtension(s);
                        string outputFile = path + "/" + filename + "ANSI." + last;

                        // 打开CSV文件流  
                        using (StreamReader reader = new(s, Encoding.Default))
                        {
                            // 创建新的CSV文件流并指定编码为ANSI  
                            using StreamWriter writer = new(outputFile, false, Encoding.GetEncoding("GBK"));
                            // 逐行读取CSV文件内容并写入新的CSV文件  
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                writer.WriteLine(line);
                            }
                        }

                        i++;
                    }
                    MessageBox.Show("转换成功,共转换" + i + "个文件!\r转换后的文件保存在原目录,文件名后加ANSI");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "转换失败");
                }
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new()
            {
                //该值确定是否可以选择多个文件
                Multiselect = true,
                //对话框标题
                Title = "请选择一个或多个txt或csv文件",
                //打开文件的类型
                Filter = "Files|*.txt;*.csv"
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                int i = 0;
                try
                {
                    foreach (string s in dialog.FileNames)
                    {
                        string last = s.Substring(s.Length - 3, 3);
                        //获取保存路径
                        string? path = System.IO.Path.GetDirectoryName(s);
                        //获取原文件名称
                        string? filename = System.IO.Path.GetFileNameWithoutExtension(s);
                        string outputFile = path + "/" + filename + "UTF8." + last;

                        // 打开CSV文件流  
                        using (StreamReader reader = new(s, Encoding.GetEncoding("GBK")))
                        {
                            // 创建新的CSV文件流并指定编码为ANSI  
                            using StreamWriter writer = new(outputFile, false, Encoding.Default);
                            // 逐行读取CSV文件内容并写入新的CSV文件  
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                writer.WriteLine(line);
                            }
                        }
                        i++;
                    }
                    MessageBox.Show("转换成功,共转换" + i + "个文件!\r转换后的文件保存在原目录,文件名后加UTF8");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "转换失败");
                }
            }
        }
    }
}

本文标签: 文档TXTCSVansi