如何在ASP.NET中动态创建Excel文件 [英] How to create Excel file dynamically in ASP.NET

查看:87
本文介绍了如何在ASP.NET中动态创建Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个ASP.NET项目(使用C#)。



我想在运行时在用户面前打开一个空白的Excel文件,以便用户可以根据他的要求创建一个列...



创建列后,他还可以填写Excel文件中的数据..



最后,打开单击保存按钮(仅在.aspx页面上),Excel文件应保存在硬盘上。



如果有任何其他解决方案,请告诉我这种类型的要求,以便用户可以像使用Microsoft Excel一样填充兼容。



任何解决方案都非常值得赞赏。



先谢谢

解决方案

您可以从GridView动态创建Excel文件并将其保存在硬盘上。也许你的客户有足够的GridView功能?

这不是你的问题的准确解决方案,但也可能有助于解决它:



 使用系统; 
使用 System.Data;
使用 System.Configuration;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Web.UI.HtmlControls;

使用 System.IO;
public static class DataGridToExcel
{
public static void 导出( 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;
HttpContext.Current.Response.Write( < head>< meta http-equiv = Content-输入content =: + ' ' + text / html; charset = utf-8 + ' ' + >< / head>);


使用(StringWriter sw = new StringWriter())
{
使用(HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();

if (gv.HeaderRow!= null
{
PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}

foreach ( gv.Rows中的GridViewRow行
{
PrepareControlForExport(row);
table.Rows.Add(row);
}

如果(gv.FooterRow!= null
{
PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
table.GridLines = gv.GridLines;
table.RenderControl(htw);

HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}

private static void PrepareControlForExport(控制控件)
{
for int i = 0 ; i < 控制。 Controls.Count; i ++)
{
Control current = control.Controls [i];
if (当前 LinkBut​​ton)
{
control.Controls卸下摆臂(电流);
control.Controls.AddAt(i, new LiteralControl((当前 as LinkBut​​ton).Text ));
}
else if (当前 ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((当前 as ImageButton).AlternateText ));
}
else if (当前 HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((当前 HyperLink).Text ));
}
else if (当前 DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((当前 as DropDownList).SelectedItem 。文本));
}
else if (当前 CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((当前 as CheckBox).Checked ? True ));
}

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





使用:



 GridView gvExcel =  new  GridView(); 
// 填写gvExcel
DataGridToExcel.Export( FileToSave.xls,gvExcel);


< blockquote class =FQ>

sharadcs05写道:

我想在运行时在用户面前打开一个空的excel文件,这样用户就可以根据他的要求创建Column ..在创建列之后,他还可以填充该excel文件中的数据..

Na最后点击保存按钮(仅在.aspx页面上)excel文件应该是保存在硬盘上...





梦想开启。



主要方式你可以这样做,就是让用户向数据网格添加行,然后允许他们使用office interop下载在服务器上创建的Excel文件,或者只是csv,Excel可以打开。


你好,



我使用此解决方案将文件发送给用户。



问题是页面进行回发。按钮的事件永远不会被触发。



我的意思是......第一次使用onclick()事件触发按钮,服务器将文件发送到用户。然后第二次事件Onclick()永远不会被触发或没有为服务器捕获。



任何人都知道这个的原因吗?



谢谢。


I am working on an ASP.NET project (using C#).

In which I want to open a blank Excel file in front of user at run time, so that the user can create a Column according to his requirements ...

After creating column he can also able to fill data in that Excel file..

Finally, on clicking the save button (which will be on that .aspx page only) that Excel file should be save on the hard disk.

Please tell me if any other solution for this type of requirement, so that the user can fill compatible just like he use Microsoft Excel.

Any solution to this is highly appreciable.

Thanks In Advance

解决方案

You can create Excel file dynamically from GridView and save it on the hard disk. Perhaps your clients have enough GridView's functionality?
This is not quite accurate solution to your problem, but may also help to solve it:

using System;
using System.Data;
using System.Configuration;
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;

using System.IO;
public static class DataGridToExcel
{
    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";
        HttpContext.Current.Response.Write("<head><meta http-equiv=Content-Type content=:" + '"' + "text/html; charset=utf-8" + '"' + "></head>");


        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                Table table = new Table();

                if (gv.HeaderRow != null)
                {
                    PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                foreach (GridViewRow row in gv.Rows)
                {
                    PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                if (gv.FooterRow != null)
                {
                    PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }
                table.GridLines = gv.GridLines;
                table.RenderControl(htw);

                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    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())
            {
                PrepareControlForExport(current);
            }
        }
    }
}



Using:

 GridView gvExcel = new GridView();
//Fill gvExcel
 DataGridToExcel.Export("FileToSave.xls", gvExcel);


sharadcs05 wrote:

In which I want to open a blank excel file in front of user at run time,So that user can create Column according to his requirement..after creating column he can also able to fill data in that excel file..
Na Finally on the click of save button(which will be on that .aspx page only) that excel file should be save on the hard disk...



Dream on.

The main way you could do this, is to let the user add rows to a datagrid, then allow them to download either a file created as an Excel file on the server using office interop, or just a csv, which Excel can open.


Hello,

I used this solution to send a file to the users.

The problem is when the page make the postback. the event of the button is never fired.

I mean..The first time the button is fired with the onclick() Event and the server send it the file to the user. Then the second time the Event Onclick() is never fired or is not catched for the Server.

Any body knows the reason of this?

Thank you.


这篇关于如何在ASP.NET中动态创建Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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