• CSV的导入导出


     1 using System;
     2 using System.Data;
     3 using System.IO;
     4 
     5 namespace COMMON
     6 {
     7     public class CSVhelperClass
     8     {
     9         /// <summary>  
    10         /// 导出报表为Csv  
    11         /// </summary>  
    12         /// <param name="dt">DataTable</param>  
    13         /// <param name="strFilePath">物理路径</param>          
    14         /// <param name="columname">字段标题,逗号分隔</param>  
    15         public string dt2csv(DataTable dt, string strFilePath, string columname)
    16         {
    17             try
    18             {
    19                 string strBufferLine = "";
    20                 StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);                 
    21                 strmWriterObj.WriteLine(columname);
    22                 for (int i = 0; i < dt.Rows.Count; i++)
    23                 {
    24                     strBufferLine = "";
    25                     for (int j = 0; j < dt.Columns.Count; j++)
    26                     {
    27                         if (j > 0)
    28                             strBufferLine += ",";
    29                         strBufferLine += dt.Rows[i][j].ToString();
    30                     }
    31                     strmWriterObj.WriteLine(strBufferLine);
    32                 }
    33                 strmWriterObj.Close();
    34                 return "备份成功";
    35             }
    36             catch (Exception ex)
    37             {
    38                 return "备份失败 " + ex.ToString();
    39             }
    40         }
    41 
    42         /// <summary>  
    43         /// 将Csv读入DataTable  
    44         /// </summary>  
    45         /// <param name="filePath">csv文件路径</param>  
    46         /// <param name="n">表示第n行是字段title,第n+1行是记录开始</param>  
    47         public DataTable csv2dt(string filePath, int n, DataTable dt)
    48         {
    49             StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, false);
    50             int i = 0, m = 0;
    51             //返回下一个可用的字符
    52             //返回值表示下一个要读取的字符的整数,或者,如果没有更多的可用字符或该流不支持查找,则为 -1。
    53             reader.Peek();
    54             while (reader.Peek() > 0)
    55             {
    56                 m = m + 1;
    57                 string str = reader.ReadLine();
    58                 if (m >= n + 1)
    59                 {
    60                     string[] lstSplit = str.Split(',');
    61 
    62                     System.Data.DataRow dr = dt.NewRow();
    63                     for (i = 0; i < lstSplit.Length; i++)
    64                     {
    65                         dr[i] = lstSplit[i];
    66                     }
    67                     dt.Rows.Add(dr);
    68                 }
    69                 else
    70                 {
    71                     string[] lstTitle = str.Split(',');
    72                     for (i = 0; i < lstTitle.Length; i++)
    73                     {
    74                         DataColumn columnItem = new DataColumn(lstTitle[i],typeof(string));
    75                         dt.Columns.Add(columnItem);
    76                     }
    77                 }
    78             }
    79             reader.Close();
    80             return dt;
    81         } 
    82     }
    83 }
  • 相关阅读:
    Pycharm创建Django项目示例
    Window下MyCat的下载与安装
    Python中使用xlrd、xlwt、xlutils读写Excel文件
    循环队列(Java实现)
    oracle 创建表
    win10 删除文件卡在99%
    python xx005文件操作
    python xx004集合
    python xx003字典
    不理解
  • 原文地址:https://www.cnblogs.com/Sunflower-/p/5531042.html
Copyright © 2020-2023  润新知