C# 导出Excel方法 先引用下System.IO;System.data;
具体函数如下:
public static bool ExportCSV(DataTable dt, string fileNmae)
{
bool Msg = false;
string con = "";
foreach (DataColumn dc in dt.Columns)
{
con += dc.ColumnName + ",";
}
con = con.TrimEnd(',') + Environment.NewLine; for (int i = ; i < dt.Rows.Count; i++)
{
for (int j = ; j < dt.Columns.Count; j++)
{
con += dt.Rows[i][j].ToString().Replace("\n", " ").Replace("\r\n", " ").Replace(",", ",") + ",";
}
con = con.TrimEnd(',') + Environment.NewLine;
}
try
{
FileStream fs = new FileStream(fileNmae, FileMode.Create);
byte[] b = Encoding.GetEncoding("gb2312").GetBytes(con);
fs.Write(b, , b.Length);
fs.Close();
Msg = true;
}
catch (Exception ex)
{
Log.WriteLog(LogFile.Error, ex.Message);
}
return Msg;
}
手工作品:使用请道明来源: http://www.cnblogs.com/benpao/p/3722161.html