日期:2014-05-17  浏览次数:20502 次

求一个实例,将GridView中数据导入到Excel表中(在线等)
如题!要一个完整的实例,能运行!

------解决方案--------------------
看看这个可以吗:http://blog.csdn.net/hhxxcj/archive/2009/01/14/3772552.aspx
------解决方案--------------------
http://www.cnblogs.com/activer/archive/2006/08/27/487749.html
------解决方案--------------------
如果是分页的情况下:

using OWC; 
using System.Data.OleDb; 
using System.IO; 

SpreadsheetClass xlsheet=new SpreadsheetClass(); 
conn.Open(); 
cmd=new OleDbCommand(sql,conn); 
OleDbDataReader reader=cmd.ExecuteReader(); 
int numbercols=reader.FieldCount; 
int row=2; 
int i=0; 
//输出标题 
for(i=0;i <numbercols;i++) 

xlsheet.ActiveSheet.Cells[1, i + 1] = reader.GetName(i).ToString(); 


// 输出字段内容 
while(reader.Read()) 

for(i=0;i <numbercols;i++) 

xlsheet.ActiveSheet.Cells[row, i + 1] = reader.GetValue(i).ToString(); 

row = row + 1; 


reader.Close(); 
conn.Close(); 
try 

xlsheet.ActiveSheet.Export(Server.MapPath( ". ") + "\\mfExcel\\ " + this.xlfile.Text+ ".xls ",OWC.SheetExportActionEnum.ssExportActionNone); 

catch(System.Runtime.InteropServices.COMException e ) 

Response.Write( "错误: " + e.Message); 

 
 
 

------解决方案--------------------
http://www.tzwhx.com/newOperate/html/1/11/111/493.html
------解决方案--------------------

------解决方案--------------------
C# code

public partial class GridViewToExcel : System.Web.UI.Page
{
        GridView gridToExport = null;

 protected void Page_Load(object sender, EventArgs e)
    {

    }



    /// <summary>
    /// 必须override这个方法,否则会报错
    /// 
    /// "Control 'ctl00_MainContent_Gridview1' of type 'GridView' must be placed
    /// inside a form tag with runat=server", 
    /// even though you have the supposedly missing form tag.
    /// 
    /// </summary>
    /// <param name="control"></param>
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        gridToExport = gvwEmployee;
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (gridToExport as GridView != null)
        {
            ExportGridToExcel(gridToExport, "test.xls");
        }
        base.Render(writer);
    }
   

    private void ExportGridToExcel(GridView grid, string filename)
    {
        // 检查是否有文件名
        if (string.IsNullOrEmpty(filename))
        {
            throw new ArgumentException("Export filename is required");
        }
        // 检查是否是excel格式
        if (!filename.EndsWith(".xls"))
        {
            filename += ".xls";
        }
        grid.AllowPaging = false;  // 禁止分页
        grid.AllowSorting = false; // 禁止排序
        grid.DataBind();            // 绑定数据

        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);

        Response.Clear();
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=" + filename);
        Response.Charset = string.Empty;
        Page.EnableViewState = false;
        grid.RenderControl(hw);
        Respo