C說話平安之數組長度與指針實例解析。本站提示廣大學習愛好者:(C說話平安之數組長度與指針實例解析)文章只能為提供參考,不一定能成為您想要的結果。以下是C說話平安之數組長度與指針實例解析正文
本文實例講述了C#操作CSV文件類。分享給年夜家供年夜家參考。詳細剖析以下:
這個C#類用於轉換DataTable為CSV文件、CSV文件轉換成DataTable,假如須要停止CSV和DataTable之間停止轉換,應用這個類異常適合。
using System.Data; using System.IO; namespace DotNet.Utilities { /// <summary> /// CSV文件轉換類 /// </summary> public static class CsvHelper { /// <summary> /// 導出報表為Csv /// </summary> /// <param name="dt">DataTable</param> /// <param name="strFilePath">物理途徑</param> /// <param name="tableheader">表頭</param> /// <param name="columname">字段題目,逗號分隔</param> public static bool 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 true; } catch { return false; } } /// <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; } } }
願望本文所述對年夜家的C#法式設計有所贊助。