[C#]CSVHelper

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

关键代码:

usingSystem.Data;usingSystem.IO;usingSystem.Text;namespaceYanZhiwei.DotNet2.Utilities.Common

{/// <summary>/// CSV文件转换类/// </summary>publicstaticclassCSVHelper

{#region导出到csv文件/// <summary>/// 导出到csv文件/// eg:/// CSVHelper.ToCSV(_personInfoView, @"C:\Users\YanZh_000\Downloads\person.csv", "用户信息表", "名称,年龄");/// </summary>/// <param name="table">DataTable</param>/// <param name="filePath">导出路径</param>/// <param name="tableheader">标题</param>/// <param name="columname">列名称,以','英文逗号分隔</param>/// <returns>是否导出成功</returns>publicstaticboolToCSV(thisDataTable table,stringfilePath,stringtableheader,stringcolumname)

{try{if(File.Exists(filePath))

File.Delete(filePath);using(FileStream _stream =newFileStream(filePath, FileMode.Create, Fileaccess.ReadWrite))

{

StreamWriter _writer =newStreamWriter(_stream, Encoding.UTF8);

_writer.WriteLine(tableheader);

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

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

{

_writer.Write(table.Rows[i][j].ToString());

_writer.Write(",");

}

_writer.WriteLine();

}

_writer.Close();returntrue;

}

}catch{returnfalse;

}

}#endregion#region将CSV文件导入到DataTable/// <summary>/// 将CSV文件导入到DataTable/// </summary>/// <param name="table">DataTable</param>/// <param name="filePath">csv文件物理路径</param>/// <param name="startRowIndex">数据导入起始行号</param>/// <returns>DataTable</returns>publicstaticDataTable ImportToTable(thisDataTable table,stringfilePath,intstartRowIndex)

{using(StreamReader reader =newStreamReader(filePath, Encoding.UTF8,false))

{intj = 0;while(reader.Peek() > -1)

{

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

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

DataRow _dataRow = table.NewRow();for(intk = 0; k < table.Columns.Count; k++)

{

_dataRow[k] = _dataArray[k];

}

table.Rows.Add(_dataRow);

}

}returntable;

}

}#endregion}

}

.csharpcode, .csharpcodePRe

{

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; }测试代码:

usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingSystem;usingSystem.Data;usingYanZhiwei.DotNet2.UtilitiesTests;namespaceYanZhiwei.DotNet2.Utilities.Common.Tests

{

[TestClass()]publicclassCSVHelperTests

{privateDataTable TestTable;

[TestMethod()]publicvoidToCSVTest()

{for(Int16 i = 18; i < 28; i++)

{

DataRow _person = TestTable.NewRow();

_person["Name"] ="YanZhiwei"+ i;

_person["Age"] = i;

TestTable.Rows.Add(_person);

}bool_expected =true;bool_actual = CSVHelper.ToCSV(TestTable,@"C:\Users\YanZh_000\Downloads\person.csv","用户信息表","名称,年龄");

Assert.AreEqual(_expected, _actual);

}

[TestInitialize]publicvoidInitTestTable()

{

TestTable =newDataTable();

TestTable.Columns.Add(newDataColumn("Name",typeof(string)));

TestTable.Columns.Add(newDataColumn("Age",typeof(int)));

}

[TestMethod()]publicvoidImportToTableTest()

{

DataTable _personInfoView = TestTable.Clone();

DataTable _expected = TestTable.Clone();for(Int16 i = 18; i < 28; i++)

{

DataRow _person = _expected.NewRow();

_person["Name"] ="YanZhiwei"+ i;

_person["Age"] = i;

_expected.Rows.Add(_person);

}

DataTable _actual = CSVHelper.ImportToTable(_personInfoView,@"C:\Users\YanZh_000\Downloads\person.csv", 2);

Assert.IsTrue(ResultSetComparer.AreIdenticalResultSets(_expected, _actual));

}

[TestCleanup]publicvoidResetTable()

{

TestTable =null;

}

}

}

.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- 王朝網路 版權所有 導航