在asp.net中,怎么把gridview中的数据导出到excel中,有没有例子,参考一下
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=sx.xls");
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;......
答案就在这里:GridView导出数据到Excel
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。
引用的别人的:
Gridview导出为Excel
尝试了一下Gridview导出为Excel,原本以为很简单,可是真正应用起来还是不太好弄的,呵呵,所想非所得。总结了一下应该注意下面几点:
1.由于gridview的内容可能是分页显示的,因此,这里在每次导出excel时,先将gridview的allowpaging属性设置为false,然后databind()一下,确保搂到所有数据;
2.不用单独设置导出的路径,导出时会弹出对话框让你确认保存位置;
3.要写一个空的VerifyRenderingInServerForm方法(必须写),以确认在运行时为指定的ASP.NET 服务器控件呈现HtmlForm 控件;
4.导出后别忘记再重新设置其allowpaging属性;
当我把这些都设置好以后,点击[导出],出现了 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render(); ) 的错误,又检查代码,没发现问题啊,搞了一会弄不出来,然后搜索了一下,发现了解决办法:
修改你的aspx文件中的:
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="SysUser.aspx.cs" Inherits="Autho_SysUser2" %>
增加红色的部分就ok了。
下面是代码和截图:
#region 导出为Excel
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private void toExcelClk()
{
gvSysUser.AllowPaging = false;
gvSysUser.AllowSorting = false;
gvSysUser.DataBind();
ToExcel(gvSysUser, "OFS_Data.xls");
gvSysUser.AllowPaging = true;
gvSysUser.AllowSorting = true;
gvSysUser.DataBind();
}
#endregion 求采纳
分享一个DataGridViewToExcel类
首先引用添加Excel Library15.0(我的VS是2015的 OFFICE是2013比较新)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace DataBaseTools
{
///
/// DatagridView导出成EXCEL类
///
public class DataGridViewToExcel
{
///
/// DatagridView导出成EXCEL
///
/// 源数据DataGridView
/// 进度条
/// 状态条
public void DataToExcel(DataGridView dgv, ToolStripProgressBar tempProgressBar, ToolStripStatusLabel toolstrip)
{
if (dgv.Rows.Count == 0)
{
MessageBox.Show("无数据"); return;
}
MessageBox.Show("开始生成要导出的数据", "导出提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = false;
for (int i = 0; i < dgv.ColumnCount; i++)
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
tempProgressBar.Visible = true;
tempProgressBar.Minimum = 1;
tempProgressBar.Maximum = dgv.RowCount;
tempProgressBar.Step = 1;
toolstrip.Visible = true;
for (int i = 0; i < dgv.RowCount; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
}
}
toolstrip.Text = "|| 状态:正在生成第 " + i + "/" + dgv.RowCount + " 个";
tempProgressBar.Value = i + 1;
}
toolstrip.Text = "|| 状态:生成成功!";
MessageBox.Show("生成成功,请保存。", "生成提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
excel.Visible = true;
}
}
}