C#中csv文件与DataTable互相导入处理实例解析
程序员文章站
2023-12-17 22:14:46
本文介绍了c#中csv文件与datatable互相导入处理实例解析,主要功能代码封装处理下,相对比较简单。以后项目用到的话可以直接使用。具体方法如下:
1.封装好的类如下...
本文介绍了c#中csv文件与datatable互相导入处理实例解析,主要功能代码封装处理下,相对比较简单。以后项目用到的话可以直接使用。具体方法如下:
1.封装好的类如下:
using system; using system.data; using system.io; using system.text; using csharputilhelpv2; using stringutilhelp; namespace dbutilhelpv2plus { public static class dbtoolv2plus { /// <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> public static bool tocsv(this datatable table, string fullsavepath, string tableheader, string columname) { argumentchecked(table, fullsavepath); //------------------------------------------------------------------------------------ try { string _bufferline = ""; using (streamwriter _writerobj = new streamwriter(fullsavepath, false, encoding.utf8)) { if (!string.isnullorempty(tableheader)) _writerobj.writeline(tableheader); if (!string.isnullorempty(columname)) _writerobj.writeline(columname); for (int i = 0; i < table.rows.count; i++) { _bufferline = ""; for (int j = 0; j < table.columns.count; j++) { if (j > 0) _bufferline += ","; _bufferline += table.rows[i][j].tostring(); } _writerobj.writeline(_bufferline); } return true; } } catch (exception) { return false; } } /// <summary> /// 参数检查 /// </summary> /// <param name="table"></param> /// <param name="fullsavepath"></param> private static void argumentchecked(datatable table, string fullsavepath) { if (table == null) throw new argumentnullexception("table"); if (string.isnullorempty(fullsavepath)) throw new argumentnullexception("fullsavepath"); string _filename = csharptoolv2.getfilenameonly(fullsavepath); if (string.isnullorempty(_filename)) throw new argumentexception(string.format("参数fullsavepath的值{0},不是正确的文件路径!", fullsavepath)); if (!_filename.invalidfilenamechars()) throw new argumentexception(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> public static datatable appendcsvrecord(this datatable table, string filepath, int rowindex) { argumentchecked(table, filepath); if (rowindex < 0) throw new argumentexception("rowindex"); using (streamreader reader = new streamreader(filepath, encoding.utf8, false)) { int i = 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); } } return table; } } } }
2.代码使用测试如下:
using system; using system.data; using dbutilhelpv2; using dbutilhelpv2plus; namespace dbutilhelpv2plustest { class program { static datatable testdb = null; static string fullsavepath = string.format(@"c:\{0}.csv", datetime.now.tostring("yyyymmddhh")); static void main(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(); } } static void createtestdb() { if (testdb == null) { testdb = dbtoolv2.createtable("name,age|int"); for (int i = 1; i <= 10; i++) { datarow _row = testdb.newrow(); _row["name"] = string.format("yanzhiwei_{0}", i); _row["age"] = i; testdb.rows.add(_row); } } } } }
运行效果如下图所示: