[C#]csv文件与DataTable互相导入处理

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

封装处理下,以后项目用到可以直接使用,比较简单。

1.首先看封装好的类

usingSystem;usingSystem.Data;usingSystem.IO;usingSystem.Text;usingCSharpUtilHelpV2;usingStringUtilHelp;namespaceDBUtilHelpV2Plus

{publicstaticclassDBToolV2Plus

{/// <summary>/// 将DataTable导出到CSV./// </summary>/// <param name="table">DataTable</param>/// <param name="fullSavePath">保存路径</param>/// <param name="tableheader">标题信息</param>/// <param name="columname">列名称『eg:姓名,年龄』</param>/// <returns>导出成功true;导出失败false</returns>publicstaticboolToCSV(thisDataTable table,stringfullSavePath,stringtableheader,stringcolumname)

{

ArgumentChecked(table, fullSavePath);//------------------------------------------------------------------------------------try{string_bufferLine ="";using(StreamWriter _writerObj =newStreamWriter(fullSavePath,false, Encoding.UTF8))

{if(!string.IsNullOrEmpty(tableheader))

_writerObj.WriteLine(tableheader);if(!string.IsNullOrEmpty(columname))

_writerObj.WriteLine(columname);for(inti = 0; i < table.Rows.Count; i++)

{

_bufferLine ="";for(intj = 0; j < table.Columns.Count; j++)

{if(j > 0)

_bufferLine +=",";

_bufferLine += table.Rows[i][j].ToString();

}

_writerObj.WriteLine(_bufferLine);

}returntrue;

}

}catch(Exception)

{returnfalse;

}

}/// <summary>/// 参数检查/// </summary>/// <param name="table"></param>/// <param name="fullSavePath"></param>PRivatestaticvoidArgumentChecked(DataTable table,stringfullSavePath)

{if(table ==null)thrownewArgumentNullException("table");if(string.IsNullOrEmpty(fullSavePath))thrownewArgumentNullException("fullSavePath");string_fileName = CSharpToolV2.GetFileNameOnly(fullSavePath);if(string.IsNullOrEmpty(_fileName))thrownewArgumentException(string.Format("参数fullSavePath的值{0},不是正确的文件路径!", fullSavePath));if(!_fileName.InvalidFileNameChars())thrownewArgumentException(string.Format("参数fullSavePath的值{0},包含非法字符!", fullSavePath));

}/// <summary>/// 将CSV文件数据导入到Datable中/// </summary>/// <param name="table"></param>/// <param name="filePath">DataTable</param>/// <param name="rowIndex">保存路径</param>/// <returns>Datable</returns>publicstaticDataTable AppendCSVRecord(thisDataTable table,stringfilePath,introwIndex)

{

ArgumentChecked(table, filePath);if(rowIndex < 0)thrownewArgumentException("rowIndex");using(StreamReader reader =newStreamReader(filePath, Encoding.UTF8,false))

{inti = 0, j = 0;

reader.Peek();while(reader.Peek() > 0)

{

j = j + 1;string_line = reader.ReadLine();if(j >= rowIndex + 1)

{string[] _split = _line.Split(',');

DataRow _row = table.NewRow();for(i = 0; i < _split.Length; i++)

{

_row[i] = _split[i];

}

table.Rows.Add(_row);

}

}returntable;

}

}

}

}

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp{ background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }2.代码使用测试

usingSystem;usingSystem.Data;usingDBUtilHelpV2;usingDBUtilHelpV2Plus;namespaceDBUtilHelpV2PlusTest

{classProgram

{staticDataTable testDb =null;staticstringfullSavePath =string.Format(@"C:\{0}.csv", DateTime.Now.ToString("yyyyMMddHH"));staticvoidMain(string[] args)

{try{

CreateTestDb();

Console.WriteLine(string.Format("DataTable导出到CSV文件{0}.", testDb.ToCSV(fullSavePath,"姓名,年龄","人员信息表") ==true?"成功":"失败"));

testDb.Rows.Clear();

Console.WriteLine(string.Format("清空数据,当前{0}条数据.", testDb.Rows.Count));

testDb = testDb.AppendCSVRecord(fullSavePath, 2);

Console.WriteLine(string.Format("CSV文件导入到Datable,导入{0}条数据.", testDb.Rows.Count));

}catch(Exception ex)

{

Console.WriteLine(ex.Message);

}finally{

Console.ReadLine();

}

}staticvoidCreateTestDb()

{if(testDb ==null)

{

testDb = DBToolV2.CreateTable("Name,Age|int");for(inti = 1; i <= 10; i++)

{

DataRow _row = testDb.NewRow();

_row["Name"] =string.Format("YanZhiwei_{0}", i);

_row["Age"] = i;

testDb.Rows.Add(_row);

}

}

}

}

}

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航