请选择 进入手机版 | 继续访问电脑版

无忧编程_ASP.NET  / C# / PHP 程序员的软件世界

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 686|回复: 0

CsvHelper--C#关于CSV文件的导入和导出以及转化

[复制链接]

370

主题

400

帖子

1656

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1656
发表于 2018-6-4 14:47:41 | 显示全部楼层 |阅读模式
csvHelper--C#关于CSV文件的导入和导出以及转化
[C#] 纯文本查看 复制代码
class CVShelper  
    {  
        /// <summary>  
        /// 导出报表为Csv  
        /// </summary>  
        /// <param name="dt">DataTable</param>  
        /// <param name="strFilePath">物理路径</param>  
        /// <param name="tableheader">表头</param>  
        /// <param name="columname">字段标题,逗号分隔</param>  
        public static string dt2csv(DataTable dt, string strFilePath, string tableheader, string columname)  
        {  
            try  
            {  
                string strBufferLine = "";  
                StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);  
                //strmWriterObj.WriteLine(tableheader);  
                strmWriterObj.WriteLine(columname);  
                for (int i = 0; i < dt.Rows.Count; i++)  
                {  
                    strBufferLine = "";  
                    for (int j = 0; j < dt.Columns.Count; j++)  
                    {  
                        if (j > 0)  
                            strBufferLine += ",";  
                        strBufferLine += dt.Rows[i][j].ToString();  
                    }  
                    strmWriterObj.WriteLine(strBufferLine);  
                }  
                strmWriterObj.Close();  
                return "备份成功";  
            }  
            catch (Exception ex)  
            {  
                return "备份失败 " + ex.ToString();  
            }  
        }  
  
        /// <summary>  
        /// 将Csv读入DataTable  
        /// </summary>  
        /// <param name="filePath">csv文件路径</param>  
        /// <param name="n">表示第n行是字段title,第n+1行是记录开始</param>  
        public static DataTable csv2dt(string filePath, int n, DataTable dt)  
        {  
  
            StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, false);  
            int i = 0, m = 0;  
            reader.Peek();  
            while (reader.Peek() > 0)  
            {  
                m = m + 1;  
                string str = reader.ReadLine();  
                if (m >= n + 1)  
                {  
                    string[] split = str.Split(',');  
  
                    System.Data.DataRow dr = dt.NewRow();  
                    for (i = 0; i < split.Length; i++)  
                    {  
                        dr[i] = split[i];  
                    }  
                    dt.Rows.Add(dr);  
                }  
            }  
            return dt;  
        }  
  
  
  
    }  

文章转自:https://blog.csdn.net/GoodShot/article/details/44150555
windows .net(C#+MSSQL) linux(php+mysql)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|软件编程 ( 鄂ICP备11006601号鄂公网安备 42011102001337号 |

GMT+8, 2018-10-21 08:37 , Processed in 0.136565 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表