在asp.net应用程序中使用office(主要是excel) [英] using office(excel mainly) in asp.net app

查看:95
本文介绍了在asp.net应用程序中使用office(主要是excel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从GridView数据创建一个excel文件并提示用户下载或保存文件.我很清楚如何在asp.net中使用response.addheader ...来做到这一点,但是如果我这样做的话,那就是excel文件中的白色背景,没有显示行和列边框,但仅显示了gridview数据的边框.所以我正在考虑使用Office对象库来创建excel文件并独立下载版本,就像任何excel版本一样可以... .codeproject专家请帮忙.....

I want to create a excel file from GridView data and prompt user to download or save file.i know well how to do it using response.addheader... in asp.net.but if i do this way then there is a white background in the excel file and no row and column border shown but shown border of gridview data only.so i am thinking to work with office object library to create excel file and download with version independently like any excel version will work....codeproject experts kindly help me.....

推荐答案



我正在使用Matt Berseth解决方案.他的网页不再可用,因此我将在此处进行代码...
助手类
Hi,

I''m using Matt Berseth solution. His web page is not available anymore so I''ll c/p code here...
Helper class
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// GridView Export Utilities
/// </summary>
public class GridViewExportUtil
{
    /// <summary>
    /// Exports GridView to excel
    /// </summary>
    /// <param name="fileName">File name</param>
    /// <param name="gv">GridView to export</param>
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
    }
}


使带有网格的ASP.NET页并在Button单击上调用export


Make ASP.NET page with grid and call export on Button click

GridViewExportUtil.Export("Customers.xls", this.gvCustomers);


浏览以下链接

http://netoffice.codeplex.com/ [ ^ ]

http://msdn.microsoft.com/en-us/library/aa701256 (v = office.11​​).aspx [
Go through the below links

http://netoffice.codeplex.com/[^]

http://msdn.microsoft.com/en-us/library/aa701256(v=office.11).aspx[^]

Thanks
--RA


这篇关于在asp.net应用程序中使用office(主要是excel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆