封装处理下,以后项目用到可以直接使用,比较简单。
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; }