C#中将DataTable中数据导出到csv文件中
来源:互联网 发布:mysql中的like语句 编辑:程序博客网 时间:2024/04/30 08:15
在上一篇博文【C#读取CSV文件的方法】中,我介绍了读取csv文件中数据的方法,现在我来介绍下将datatable中的数据导出到csv文件中的方法,废话不
多说了,deme程序的代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("test1");
dt.Columns.Add("test2");
dt.Columns.Add("test3");
dt.Columns.Add("test4");
dt.Columns.Add("test5");
for (int i = 0; i < 6; i++)
{
dt.Rows.Add();
dt.Rows[i][0] = "CN"+i.ToString();
dt.Rows[i][1] = "EN"+i.ToString();
dt.Rows[i][2] = "JN"+i.ToString();
dt.Rows[i][3] = "HK"+i.ToString();
dt.Rows[i][4] = "TW"+i.ToString();
}
ExportDataGridToCSV(dt);
}
/// <summary>
/// Export the data from datatable to CSV file
/// </summary>
/// <param name="grid"></param>
public void ExportDataGridToCSV(DataTable dt)
{
string strFile = "";
string path = "";
//File info initialization
strFile = "test";
strFile = strFile + DateTime.Now.ToString("yyyyMMddhhmmss");
strFile = strFile + ".csv";
path = Server.MapPath(strFile);
System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
//Tabel header
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].ColumnName);
sw.Write("\t");
}
sw.WriteLine("");
//Table body
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
sw.Write(DelQuota(dt.Rows[i][j].ToString()));
sw.Write("\t");
}
sw.WriteLine("");
}
sw.Flush();
sw.Close();
DownLoadFile(path);
}
private bool DownLoadFile(string _FileName)
{
try
{
System.IO.FileStream fs = System.IO.File.OpenRead(_FileName);
byte[] FileData = new byte[fs.Length];
fs.Read(FileData, 0, (int)fs.Length);
Response.Clear();
Response.AddHeader("Content-Type", "application/notepad");
string FileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(_FileName));
Response.AddHeader("Content-Disposition", "inline;filename=" + System.Convert.ToChar(34) + FileName + System.Convert.ToChar(34));
Response.AddHeader("Content-Length", fs.Length.ToString());
Response.BinaryWrite(FileData);
fs.Close();
System.IO.File.Delete(_FileName);
Response.Flush();
Response.End();
return true;
}
catch (Exception ex)
{
ex.Message.ToString();
return false;
}
}
/// <summary>
/// Delete special symbol
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string DelQuota(string str)
{
string result = str;
string[] strQuota ={ "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", ".", "/", ":", "/,", "<", ">", "?" };
for (int i = 0; i < strQuota.Length; i++)
{
if (result.IndexOf(strQuota[i]) > -1)
result = result.Replace(strQuota[i], "");
}
return result;
}
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#中将DataTable中数据导出到csv文件中
- C#导出DataTable数据到CSV文件中
- c#将DataTable中数据写入到CSV文件中
- C# - 数据备份-datatable数据写入到CSV文件中
- C# 导出DataTable数据到Excel文件中
- NET(C#)中将DATATABLE内容写入到CSV文件 与 将CSV文件内容读取到DATATABLE中
- C++中将csv文件中的数据存储到数组中
- 网页端中将表格数据和JSON数据读取(导出)到本地的excel和csv文件中
- c#中将数据库数据导出到EXCEL中
- C#中将ListView中数据导出到Excel代码
- 如何读取CSV数据到DataTable中
- java中将数据和图片导出到Excel文件中
- Sql2008R2中将数据库数据导出到sql文件中
- C#中如何将DataTable中的数据导出到Excel
- C++虚析构函数
- jtest_9.0_win32_eclipse_plugin
- 进程间通信之共享内存篇
- 亡灵序曲
- 1701. Cannonball Pyramids
- C#中将DataTable中数据导出到csv文件中
- 观《致命ID》有感
- IT行业展望
- ubuntu下开启telnet
- windows7下开启telnet
- 关于使用ETL工具Kettle的简单介绍(三)
- MySQL从外部文件导入数据
- 底心爱着藏
- Sqlite的C API函数返回码的意义