如何使用c#将数据表记录绑定到excel [英] how to bind the data table records into excel using c#

查看:67
本文介绍了如何使用c#将数据表记录绑定到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下



private void Preseaintake()

{

试试

{

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText =Presea_Intakerpts + dtfromdate.Text.Trim()+','+ dttodate.Text.Trim()+';

SqlDataAdapter adp = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

adp.Fill(dt);

string filename =E:\\Preseaintake \\10032014_PreseaIntakedetails .xls;

System.IO.StreamWriter file = new System.IO.StreamWriter(filename);

file.Close();



}



catch(例外情况)

{

MessageBox。展(ex.ToSt ring(),Error,MessageBoxButtons.OK);

return;

}



} < br $>


i写了一个商店程序,该商店程序值已使用数据表绑定。





现在我想把数据表记录绑定到excel。



我怎么能用c#。



请帮帮我。



问候,

Narasiman P.

My code as follows

private void Preseaintake()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Presea_Intakerpts '" + dtfromdate.Text.Trim() + "', '" + dttodate.Text.Trim() + "'";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
string filename = "E:\\Preseaintake\\10032014_PreseaIntakedetails.xls";
System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
file.Close();

}

catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
return;
}

}

i have written a store procecdure, that store procedure value has been binded using datatable.


Now i wan to bind the datatable records into excel.

for that how can i do using c#.

Please help me.

regards,
Narasiman P.

推荐答案

看看下面的解决方案,它将数据表导出为Excel支持的csv格式,您只需更改 contenttype 即可导出它是Excel格式。





使用C#导出DataTable
Do have a look at the following solution, which exports the datatable to csv format which is supported by Excel, You can just change the contenttype to export it to Excelformat.


Export DataTable using C#


这篇关于如何使用c#将数据表记录绑定到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆