zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    山东第一届省赛1001 Phone Number(字典树)
    HD2222 Keywords Search(AC自动机入门题)
    POJ 1947Rebuilding Roads(树形DP + 01背包)
    zoj 3946 Highway Project(最短路 + 优先队列)
    HDU5672String(尺标法)
    HDU5671Matrix(矩阵行列交换)
    HDU5670Machine(抽象进制)
    用户体验评价
    团队冲刺第二阶段-6
    第十四周学习进度
  • 原文地址:https://www.cnblogs.com/Sunflower-/p/5531042.html
Copyright © 2011-2022 走看看