[C#]CSVHelper
2020-12-13 14:18
标签:style blog http io color os ar for sp 关键代码:using System.Data;
using System.IO;
using System.Text;
namespace YanZhiwei.DotNet2.Utilities.Common
{
///
(int j = 0; j ",");
}
_writer.WriteLine();
}
_writer.Close();
return true;
}
}
catch
{
return false;
}
}
#endregion
#region 将CSV文件导入到DataTable
///
测试代码:
using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Data; using YanZhiwei.DotNet2.UtilitiesTests; namespace YanZhiwei.DotNet2.Utilities.Common.Tests { [TestClass()] public class CSVHelperTests { private DataTable TestTable; [TestMethod()] public void ToCSVTest() { for (Int16 i = 18; i "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] public void InitTestTable() { TestTable = new DataTable(); TestTable.Columns.Add(new DataColumn("Name", typeof(string))); TestTable.Columns.Add(new DataColumn("Age", typeof(int))); } [TestMethod()] public void ImportToTableTest() { DataTable _personInfoView = TestTable.Clone(); DataTable _expected = TestTable.Clone(); for (Int16 i = 18; i "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] public void ResetTable() { TestTable = null; } } }
测试结果:
[C#]CSVHelper
标签:style blog http io color os ar for sp
原文地址:http://www.cnblogs.com/Yan-Zhiwei/p/4060925.html