今天网上找了N久基本全部是那个ASP.NET 里面的GRIDVIEW 变EXCEL, 基本没看见说WINFORM里面导出的,很是纳闷,现在特地在此送分给那位兄弟姐妹有方法介绍个小弟我看,,,坚决抵制那些在网上搜索到的ASP.NET帖给我...请注意噢,是C# WINDOW FORM里面的GRIDVIEW 不是网页的
解决方案 »
- c# 如何立刻刷新桌面
- 怎么以最快的方法熟悉一个框架。
- 请教一个正则
- 使用showModelDialog传递一个textbox的值的参数打开一个窗口,关闭子窗口时,刷新父窗口,怎么办
- 打包时要从注册表里检查某个组件是否存在,应该查找哪个键的那些信息?如何先安装一个组件?
- 大家帮忙看看 这个东西怎么获得 值呢?
- 1.听说有人用C#操作硬件了,C#能操作硬件吗??? 2.现在用C#搞C/S的多吗?这种情况要持续多久啊!!!
- 怎样做POS打印, 打印格式控制最好呢?
- 怎么读取内存地址的数据?
- 不会不懂不知道
- 我的毕业设计题目是 基于WEB的在线点餐系统,请提点意见
- CDO发送邮件时,如果把姓名显示出来
private void button3_Click(object sender, System.EventArgs e)
{//导出excel
//导出execl
if(ds!=null)
{
doExport(ds,"未知名文档!");
}
else
{
MessageBox.Show("导出内容不能为空!","警告!",MessageBoxButtons.OK,MessageBoxIcon.Stop);
}
}
private void doExport(DataSet ds,string strExcelFileName)
{ //导出execl
try
{
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable table=ds.Tables[0] ;
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=false;
excel.Save(strExcelFileName);
excel.Quit();
excel=null;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"警告!",MessageBoxButtons.OK,MessageBoxIcon.Stop);
}
GC.Collect();//垃圾回收
}
{ string attachment = "attachment; filename=" + filename + ".xls"; HttpResponse Response = HttpContext.Current.Response; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid HtmlForm frm = new HtmlForm(); GridView1.Parent.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(GridView1);
frm.RenderControl(htw); //GridView1.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); }