求导出excel类

h_m_cai 2007-03-25 06:56:40
最好有一个完整的类,可以传递sql语句、datagrid、gridview、dataset、sqldatareader直接导出里面的数据到excel里,拜托大家有的提供一下,我百度好久没看到比较完整的,谢谢!
...全文
332 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
job_2006 2007-03-26
  • 打赏
  • 举报
回复
收藏
belldandy11 2007-03-26
  • 打赏
  • 举报
回复
MARK
nyzfl 2007-03-26
  • 打赏
  • 举报
回复
路过 学习
leixueqiyi 2007-03-26
  • 打赏
  • 举报
回复
honkerhero(孤独的流浪)不错
你可以借鉴
还可以调用Aspose.Excel.dll
在调用savefiledialog
luck0235 2007-03-26
  • 打赏
  • 举报
回复
Aspose.Excel.dll
honkerhero 2007-03-26
  • 打赏
  • 举报
回复
/// <summary>
/// 导出成Excel报表
/// </summary>
/// <param name="strXml">XML数据串</param>
/// <param name="strXslPath">Xslt转换样式文件的路径</param>
/// <param name="httpResponse">回写客户端</param>
public static void XMLToExcel(string strXml,string strXslPath,System.Web.HttpResponse httpResponse)
{
try
{
#region 数据验证

if( null == strXml )
{
throw new Exception("XML字符串为NULL");
}

if( null == strXslPath || "" == strXslPath.Trim() )
{
throw new Exception("XSLPATH字符串为空");
}

if( null == httpResponse )
{
throw new Exception("HttpResponse流为NULL");
}
#endregion

#region 输出到Excel

XmlDocument xmlDoc=new XmlDocument();

// .Regex r=new .Regex("^<?xml");//正则表达式验证是文件路径还是XML字符串
xmlDoc.LoadXml(strXml);

XslTransform xslTran=new XslTransform();
xslTran.Load(strXslPath);

StringWriter sw=new StringWriter();
xslTran.Transform(xmlDoc,null,sw,null);

string strExcel=sw.ToString();
strExcel=strExcel.Substring(strExcel.LastIndexOf("?>")+2);

httpResponse.ContentType="application/vnd.ms-excel";
httpResponse.ContentEncoding=System.Text.Encoding.UTF7;
httpResponse.Write(strExcel);
httpResponse.Flush();
httpResponse.End();

#endregion
}
catch(Exception Ex)
{
throw Ex;
}
}


设计思想请看
http://blog.csdn.net/honkerhero/archive/2007/03/14/1528850.aspx
仅供参考
h_m_cai 2007-03-26
  • 打赏
  • 举报
回复
再顶
Donny_zhang 2007-03-25
  • 打赏
  • 举报
回复
学习!
h_m_cai 2007-03-25
  • 打赏
  • 举报
回复
再帮忙看看~~
wangzhaoli1982 2007-03-25
  • 打赏
  • 举报
回复
this.DataGrid1.AllowPaging = false;
DB();//绑定的方法 你可以在这里写好数据源 再打印
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
DataGrid1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
C_1979 2007-03-25
  • 打赏
  • 举报
回复
顶~!
h_m_cai 2007-03-25
  • 打赏
  • 举报
回复
顶~!
burn3tt 2007-03-25
  • 打赏
  • 举报
回复
虽然,sql server中的DTS也能将数据倒入Excel,但不如使用程序灵活,
本程序主要代码在按钮函数内。可适应于报表开发的读取数据部分:)
我删除了原程序的很多垃圾代码,只留主要起作用的代码

//加入名称空间
using System.Data;
using System.Data.SqlClient;


//定义方法GetData(),返回一个数据表
private System.Data.DataTable GetData()
{
SqlConnection conn= new SqlConnection(@"Server=PXGD2;Initial Catalog=pingxiang;Uid=sa;Pwd=;");
SqlDataAdapter adapter= new SqlDataAdapter("select username 用户名,catalyst_port 占用端口,home_address 住宅地址,ip_address

ip地址,phone 电话,addtime 开通日期 from userinfo where catalyst_port=1 or catalyst_port='' order by ip_address desc",conn);

DataSet ds= new DataSet();
try
{
adapter.Fill(ds,"Customer");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ds.Tables[0];
}

//按钮
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0;

excel.Application.Workbooks.Add(true);

DataTable table=GetData();

//将所得到的表的列名,赋值给单元格
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}

//同样方法处理数据
foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
//不可见,即后台处理
excel.Visible=true;
}
burn3tt 2007-03-25
  • 打赏
  • 举报
回复
http://cache.baidu.com/c?word=excel%2Cc%23&url=http%3A//www%2Eddo5%2Ecom/NET/Csharp/200606/10688%2Ehtml&p=86769a46d3d503e708e2957e5007bb&user=baidu
h_m_cai 2007-03-25
  • 打赏
  • 举报
回复
来这第一次求助,大家帮帮忙吧~~

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧