首页 > 学院 > 开发设计 > 正文

C#中Datatable转Excel

2019-11-17 04:17:28
字体:
来源:转载
供稿:网友

Datatable另存为Excel的方法,Datatable导出到Excel的方法

在数据程序的开放中,有时需要把数据中的查询结果保存到Excel文件中,下面的过程实现了这个功能.

使用说明:参数excel为要保存的excel文件名;tb为内存中的有数据的Datatable;tbname是插入到excel文件中的工作表的名称.程序中已经对部分数据类型进行了分别处理,如有特殊需要请自己修改!

        static public void TableToExcel(string excel, DataTable tb,string tbname)
        {
            try
            {
                OleDbConnection connex = GetConnFromExcel(excel); //获取到excel文件的oledb连接
                string ct = "CREATE TABLE " + tbname + " (";                //以下生成一个sql命令向excel中插入一个表
                foreach (DataColumn clmn in tb.Columns)
                {
                    switch (clmn.DataType.Name)                                     //根据不同数据类型分别处理
                    {
                        case "Decimal":
                            ct += clmn.ColumnName + " Decimal,";
                            break;
                        case "Double":
                            ct += clmn.ColumnName + " Double,";
                            break;
                        default:
                            ct += clmn.ColumnName + " string,";
                            break;
                    }
                }
                ct = ct.Substring(0, ct.Length - 1) + ")";
                OleDbCommand cmd1 = new OleDbCommand(ct, connex);
                cmd1.ExecuteNonQuery();                                                              //向excel中插入一个表

                foreach (DataRow r in tb.Rows)                                                       //下面向excel中一行一行写入数据
                {
                    string fs = "", vs = "";
                    foreach (DataColumn clmn in tb.Columns)
                    {
                        fs += clmn.ColumnName + ",";
                        if (r[clmn.ColumnName] == DBNull.Value)
                        {
                            vs += "null,";
                            continue;
                        }
                        switch (clmn.DataType.Name)                                                   //根据不同数据类型分别处理
                        {
                            case "Decimal":
                                vs += ((decimal)r[clmn.ColumnName]).ToString("0.00") + ",";
                                break;
                            case "Double":
                                vs += ((double)r[clmn.ColumnName]).ToString("0.00") + ",";
                                break;
                            case "DateTime":
                                vs += "'" + ((DateTime)r[clmn.ColumnName]).ToShortDateString() + "',";
                                break;
                            default:
                                vs += "'" + r[clmn.ColumnName].ToString() + "',";
                                break;
                        }
                    }
                    string sqlstr = "insert into [" + tbname + "$] (" + fs.Substring(0, fs.Length - 1) + ") values (" + vs.Substring(0, vs.Length - 1) + ")";
                    OleDbCommand cmd = new OleDbCommand(sqlstr, connex);
                    cmd.ExecuteNonQuery();                                            //向excel中插入数据
                }
                connex.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表