using System;
using System.Data;
using System.IO;
namespace YG._CommonFunctions
{
/// <summary>
/// 数据转换成csv
/// </summary>
public static class Data2CSV
{
public static string GetColumnsByDataTable(DataTable dt)
{
string strColumns = null;
if (dt.Columns.Count > 0)
{
//int columnNum = 0;
//columnNum = dt.Columns.Count;
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i == 0)
{
strColumns = dt.Columns[i].ColumnName;
}
else
{
strColumns += "," + dt.Columns[i].ColumnName;
}
}
}
return strColumns;
}
/// <summary>
/// 导出报表为Csv
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="strFilePath">物理路径</param>
/// <param name="tableheader">表头</param>
/// <param name="columname">字段标题,逗号分隔</param>
public static string DataTableToCSV(DataTable dt, string strFilePath)
//public static bool DataTableToCsv(DataTable dt, string strFilePath, string tableheader, string columname)
{
try
{
string columname = GetColumnsByDataTable(dt);
string strBufferLine = "";
StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
//StreamWriter strmWriterObj = new StreamWriter(strFilePath);
//strmWriterObj.WriteLine(tableheader);
strmWriterObj.WriteLine(columname);
for (int i = 0; i < dt.Rows.Count; i++)
{
strBufferLine = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j > 0)
strBufferLine += ",";
strBufferLine += dt.Rows[i][j].ToString();
}
strmWriterObj.WriteLine(strBufferLine);
}
strmWriterObj.Close();
return strFilePath;
}
catch(Exception ex)
{
return "";
}
}
public static string DataSetToCSV(DataSet dS, string strFilePath,int index)
//public static bool DataTableToCsv(DataTable dt, string strFilePath, string tableheader, string columname)
{
try
{
string columname = GetColumnsByDataTable(dS.Tables[index]);
string strBufferLine = "";
StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
//StreamWriter strmWriterObj = new StreamWriter(strFilePath);
//strmWriterObj.WriteLine(tableheader);
strmWriterObj.WriteLine(columname);
for (int i = 0; i < dS.Tables[index].Rows.Count; i++)
{
strBufferLine = "";
for (int j = 0; j < dS.Tables[index].Columns.Count; j++)
{
if (j > 0)
strBufferLine += ",";
strBufferLine += dS.Tables[index].Rows[i][j].ToString();
}
strmWriterObj.WriteLine(strBufferLine);
}
strmWriterObj.Close();
return strFilePath;
}
catch (Exception ex)
{
return "" ;
}
}
public static string DataSetToCSV(DataSet dS, string strFilePath, string TableName)
{
try
{
string columname = GetColumnsByDataTable(dS.Tables[TableName]);
string strBufferLine = "";
StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
//StreamWriter strmWriterObj = new StreamWriter(strFilePath);
//strmWriterObj.WriteLine(tableheader);
strmWriterObj.WriteLine(columname);
for (int i = 0; i < dS.Tables[TableName].Rows.Count; i++)
{
strBufferLine = "";
for (int j = 0; j < dS.Tables[TableName].Columns.Count; j++)
{
if (j > 0)
strBufferLine += ",";
strBufferLine += dS.Tables[TableName].Rows[i][j].ToString();
}
strmWriterObj.WriteLine(strBufferLine);
}
strmWriterObj.Close();
return strFilePath;
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// 将Csv读入DataTable
/// </summary>
/// <param name="filePath">csv文件路径</param>
/// <param name="n">表示第n行是字段title,第n+1行是记录开始</param>
public static DataTable CSVToDataTable(string filePath, int n, DataTable dt)
{
StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, false);
int i ,m = 0;
reader.Peek();
while (reader.Peek() > 0)
{
m += 1;
string str = reader.ReadLine();
if (m >= n + 1)
{
string[] split = str.Split(',');
System.Data.DataRow dr = dt.NewRow();
for (i = 0; i < split.Length; i++)
{
dr[i] = split[i];
}
dt.Rows.Add(dr);
}
}
return dt;
}
}
}
C#工具类 将DataTable和DataSet转换成CSV
原创
©著作权归作者所有:来自51CTO博客作者顾杨513323610的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
将SqlDataReader转换成DataTable
将SqlDataReader转换成DataTable由C#转过来的
职场 数据库 DataTable 休闲 SqlDataReader -
将Xml字符串转换成(DataTable || DataSet || XML)对象xml 字符串 xml文件
-
将Txt文件转换成dataset
遇到需要将txt文件转化到内存表dataset中
字段 自定义 txt文件 i++ 线路编码