winform中,怎样把gridview的内容导出为excel文件呢? 如题。想添加一个功能把gridview的表格内容导出为excel的文件格式,请问这个怎么做??比如用一个按钮来完成。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://msdn.microsoft.com/zh-cn/architecture/microsoft.office.tools.excel.chart_members.aspx http://hi.baidu.com/yinser/blog/item/87ea5a586f9e068a800a18f1.html 参考:如何使用c#将Winform下DataGridView中内容导出到Excel? DataGrid导出到Excel代码 收藏导出的函数:public void ToExcel(System.Web.UI.WebControls dg){ HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls"); HttpContext.Current.Response.Charset ="UTF-8"; HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default; HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword dg.Page.EnableViewState =false; System.IO.StringWriter tw = new System.IO.StringWriter() ; System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw); dg.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); }调用:ToExcel(DataGrid1); Win 有gridview??直接将DataTable使用第三方控件(myxls)生成Excel http://www.cnblogs.com/lovecherry/archive/2005/03/25/125519.html 导出excelhttp://topic.csdn.net/u/20100804/11/59ba57fe-907a-41ff-96dd-7ee31bc4cfd8.html http://www.shilidata.com/showforum-44.aspxoffice开发的专题,都可以参照 大家注意,是winform中,不是web //导出EXCEL public static void ExportMes(DataGridView dgv) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "导出Excel文件到"; DateTime now = DateTime.Now; saveFileDialog.FileName = now.Year.ToString().PadLeft(2) + "-" + now.Month.ToString().PadLeft(2, '0') + "-" + now.Day.ToString().PadLeft(2, '0') + " " + now.Hour.ToString().PadLeft(2, '0') + "点" + now.Minute.ToString().PadLeft(2, '0') + "分"; saveFileDialog.ShowDialog(); Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312")); string str = ""; try { //写标题 for (int i = 0; i < dgv.ColumnCount; i++) { if (i > 0) { str += "\t"; } str += dgv.Columns[i].HeaderText; } sw.WriteLine(str); //写内容 for (int j = 0; j < dgv.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < dgv.Columns.Count; k++) { if (k > 0) { tempStr += "\t"; } tempStr += dgv.Rows[j].Cells[k].Value.ToString(); } sw.WriteLine(tempStr); } sw.Close(); myStream.Close(); //MessageBox.Show("导出成功"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { sw.Close(); myStream.Close(); } } 高手请教 关于MDIForm的调用问题,请大家帮帮忙! 能不能建立独立用户的静态变量 请教一下各位大侠 为什么调用 myClientSocket.Send会循环发送空消息 附图 关于动态调用某个实例中的属性或方法 请问如何在vs2005中做移动web项目 winfrom如何获取其中控件的绝对位置? 求助:如何将一个数据集保存在类中 C#中是否有这样的方法??? 我想在.net制作安装程序中,加入一个注册表项,记录程序的安装路径,怎么办,谢谢 dotnetbar datagridview赋值 超级菜鸟问个弱智问题?高分!
DataGrid导出到Excel代码 收藏
导出的函数:
public void ToExcel(System.Web.UI.WebControls dg)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
dg.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
dg.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
调用:
ToExcel(DataGrid1);
直接将DataTable使用第三方控件(myxls)生成Excel
http://topic.csdn.net/u/20100804/11/59ba57fe-907a-41ff-96dd-7ee31bc4cfd8.html
public static void ExportMes(DataGridView dgv)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出Excel文件到"; DateTime now = DateTime.Now;
saveFileDialog.FileName = now.Year.ToString().PadLeft(2) + "-" + now.Month.ToString().PadLeft(2, '0') + "-" + now.Day.ToString().PadLeft(2, '0') + " " + now.Hour.ToString().PadLeft(2, '0') + "点" + now.Minute.ToString().PadLeft(2, '0') + "分";
saveFileDialog.ShowDialog(); Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
string str = "";
try
{
//写标题
for (int i = 0; i < dgv.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dgv.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < dgv.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dgv.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dgv.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
//MessageBox.Show("导出成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}