• 查看csv文件导出csv文件c#


    public DataTable ReadCSV(string pathcsv,string FileName)
            {
                OleDbConnection OleCon = new OleDbConnection();
                OleDbCommand OleCmd = new OleDbCommand();
                OleDbDataAdapter OleDa = new OleDbDataAdapter();

                DataSet dsCsvData = new DataSet();

                OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathcsv + ";Extended Properties='Text;FMT=Delimited(,);HDR=YES;IMEX=1';";
                //OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='text;FMT=Fixed;HDR=NO;'";
                OleCon.Open();
                DataTable dts1 = OleCon.GetSchema("Tables");
                DataTable dts2 = OleCon.GetSchema("Columns");
                OleCmd.Connection = OleCon;
                OleCmd.CommandText = "select * from " + FileName;
                OleDa.SelectCommand = OleCmd;
                //OleDbDataReader oledr = oleCom.ExecuteReader();
               
                //while (oledr.Read())
                //{
                //    oledr.GetValues(records);//取出一行记录
                //    try
                //    {
                //        redt.Rows.Add(records);
                //    }
                //    catch { continue; }
                //}

                //OleDa.FillSchema(dsCsvData, SchemaType.Mapped);
                OleDa.Fill(dsCsvData, "Csv");
                OleCon.Close();
                return dsCsvData.Tables["Csv"];

            }

     public void ExportToSvc(System.Data.DataTable dt,string strName,string path)

            {

                 string strPath=path+ strName+".csv";

            

                if (File.Exists(strPath))

                 {

                   File.Delete(strPath);

                 }

                //先打印标头

               StringBuilder strColu=new StringBuilder();

               StringBuilder strValue=new StringBuilder();

                int i=0;

       

                try

              {

                   StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));

                   for( i=0;i<=dt.Columns.Count-1;i++)

                   {

                        strColu.Append(dt.Columns[i].ColumnName);

                      strColu.Append(",");

                    }

                   strColu.Remove(strColu.Length-1,1);//移出掉最后一个,字符

                 sw.WriteLine(strColu);

                    foreach(DataRow dr in dt.Rows)

                  {
                       strValue.Remove(0,strValue.Length);//移出

           

                      for(i=0;i<=dt.Columns.Count-1;i++)

                       {

                            strValue.Append(dr[i].ToString());

                            strValue.Append(",");
                        }

                        strValue.Remove(strValue.Length-1,1);//移出掉最后一个,字符

                       sw.WriteLine(strValue);

                   }

                  

                    sw.Close();

               }

                catch(Exception ex)

               {

                    MessageBox.Show(ex.Message);

               }

                System.Diagnostics.Process.Start(strPath);

                  

           }

  • 相关阅读:
    List集合中的对象按照某个字段去重实现
    菜单--微信提醒
    fastTime从后台传过来显示格式的处理
    彻底卸载Oracle
    关于这次安装Oracle
    关于下拉选择删选最基本一例(分享内容)
    马拉松参赛人员显示(瞬逝版)
    马拉松参赛人员旧版本最终版(私藏版)
    win 10 初始环境变量
    AngularJS输出helloworld
  • 原文地址:https://www.cnblogs.com/jinyuttt/p/2103351.html
Copyright © 2020-2023  润新知