c# 操作excel文件(csv、数据库)

1、把csv文件中的数据读到datatable中       

       public static DataTable ImportCSV(string filePath)
        {
              DataTable ds = new DataTable();
                using (StreamReader sw = new StreamReader(filePath,Encoding.Default))
                {

                    string str = sw.ReadLine();
                    string[] columns = str.Split(',');
                    foreach (string name in columns)
                    {
                        ds.Columns.Add(name);
                    }

                    string line = sw.ReadLine();
                    while (line != null)
                    {
                        string[] data = line.Split(',');
                        ds.Rows.Add(data);
                        line = sw.ReadLine();
                    }

                    sw.Close();
                }

                return ds;
        }

 2、把csv的数据导入到数据库中

        public static void ImportCSVToDB(string filePath, string type, string dbTable)
        {

             string sqlString = "BULT insert " + dbTable + " from '" + filePath + "' WITH(FILEDTERMINATOR=',',ROWTERMINATOR='\n')";           

              using (SqlConnection conn = GetConn(conStr))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand(sqlString, conn);
                    return command.ExecuteNonQuery();
                }
        }

 

       3、excel的数据读入datatable中

        public static DataTable ImportXLS(string filePath)
        {
             string fileName = System.IO.Path.GetFileName(filePath);
            string path = System.IO.Path.GetDirectoryName(filePath);
            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties = 'Excel 8.0;HDR=YES;IMEX=1;'";        
            DataTable ds = new DataTable();

            using (OleDbConnection conn = new OleDbConnection(strCon))
            {
                    conn.Open();
                    OleDbDataAdapter adpt = new OleDbDataAdapter("select * from [Sheet1$]", conn);
                    adpt.Fill(ds);
             }

             return ds;
        }

4、导出数据到csv文件中。(这种方式速度快)   

        public static void ExportToCSV(DataTable ds, string filePath)
        {
             using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.Unicode))
            {
                for (int i = 0; i < ds.Columns.Count; i++)
                {
                    sw.Write(ds.Columns[i].ColumnName + "\t");
                }

                sw.Write("\n");
                for (int i = 0; i < ds.Rows.Count; i++)
                {
                    for (int j = 0; j < ds.Columns.Count; j++)
                    {
                        sw.Write(ds.Rows[i][j].ToString() + "\t");
                    }

                    sw.Write("\n");
                }

                sw.Close();
            }

                     return ;
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值