从C#在Excel中保存数据的问题 [英] problem with saving data in Excel from c#

查看:69
本文介绍了从C#在Excel中保存数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用C#(窗口形式)将Excel中的名称和Surnme列表保存时遇到问题
像ë"这样的字符存在问题,我没有在Excel中用Word遇到的问题.

例如,我正在使用该代码:

I have a problem with saving a list of name and surnme in excel from c# (window form)
there are problem with some character like ''ë'', I dont have that problem i Word just in excel.

ex I am using that code:

string Text= File.ReadAllText(textbox.Text, System.Text.Encoding.Default);



Excel中的消息是这样的:您尝试打开".xls"的文件的格式与扩展名指定的格式不同..


我该怎么办......


那不是功课,请理解我...!

上帝保佑你



the message in Excel is this: the file you trying to open ".xls" is in different format than specified by extension..


what should I do....


that''s not homework, understand me please...!

God bless you

推荐答案

尝试使用此

Try using this

string Text= File.ReadAllText(textbox.Text, System.Text.Encoding.Unicode);


//fxn that exports content of a datagridview object to an excel file

//note: excel_file represents the complete physical address of excel file eg C:/myexcel.xls


        public  void export_datagridview_to_excel(DataGridView dgv, string excel_file)
        {
            int cols;
            //open file
            StreamWriter wr = new StreamWriter(excel_file);

            //determine the number of columns and write columns to file
            cols = dgv.Columns.Count;
            for (int i = 0; i < cols; i++)
            {
                wr.Write(dgv.ColumnsIdea.Name.ToString().ToUpper() + "\t");
            }

            wr.WriteLine();

            //write rows to excel file
            for (int i = 0; i < (dgv.Rows.Count - 1); i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    if (dgv.RowsIdea.Cells[j].Value != null)
                        wr.Write(dgv.RowsIdea.Cells[j].Value + "\t");
                    else
                    {
                        wr.Write("\t");
                    }
                }

                wr.WriteLine();
            }

            //close file
            wr.Close();


}


}


这篇关于从C#在Excel中保存数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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