ASP.Net的导出Excel的快速方法,DataTable导出Excel(亲测,非原创)

//使用方法 ExcelHelper.dataTableToCsv(dt,@"D:\1212.xls");System.Diagnostics.Process.Start(@"c:\1.xls"); //打开excel文件
public static void dataTableToCsv(DataTable table, string file)
{

string title = "";

FileStream fs = new FileStream(file, FileMode.OpenOrCreate);

//FileStream fs1 = File.Open(file, FileMode.Open, FileAccess.Read);

StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);

for (int i = 0; i < table.Columns.Count; i++)
{

title += table.Columns[i].ColumnName + "\t"; //栏位:自动跳到下一单元格

}

title = title.Substring(0, title.Length - 1) + "\n";

sw.Write(title);

foreach (DataRow row in table.Rows)
{

string line = "";

for (int i = 0; i < table.Columns.Count; i++)
{

line += row[i].ToString().Trim() + "\t"; //内容:自动跳到下一单元格

}

line = line.Substring(0, line.Length - 1) + "\n";

sw.Write(line);

}

sw.Close();

fs.Close();

}

你可能感兴趣的:(ASP.Net的导出Excel的快速方法,DataTable导出Excel(亲测,非原创))