English 中文(简体)
(1) 如何用Excel书写记忆,.......2)如何从浏览器下载Excel文档。 Excel和c#
原标题:1) how to write excel to memorystream....2) how to download excel file from browser....using Micosoft.Office.Interop.Excel and c#
//i m using VS 2008, dll as Microsoft.Office.Interop.Excel
//sample code..
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

object misValue = System.Reflection.Missing.Value;                
xlApp = new Excel.ApplicationClass();                
xlWorkBook = xlApp.Workbooks.Add(misValue);//create new work book
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//CREATING ONE Rectangle shape
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 47, 24, 500, 90);
 Microsoft.Office.Interop.Excel.Shape[] myShapes = new Microsoft.Office.Interop.Excel.Shape[1];
 //adding textbox
 myShapes[0] = xlWorkSheet.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 75, 60, 70, 30);
myShapes[0].TextFrame.Characters(misValue, misValue).Text = "Tracking";

浏览器/下载荷选项。

context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("Content-Disposition", "attachment; filename= report.xls");

/ 使用临时文本文档的im。 但无需使用临时档案。

string file = context.Server.MapPath("new.txt");
 Byte[] myByte = File.ReadAllBytes(file);

  context.Response.Clear();
  context.Response.BinaryWrite(myByte);
  context.Response.Flush();
  context.Response.Close();

我们能否利用Ipersiststream界面来挽救记忆流中的物体?

问题回答

如果你有兴趣制作比GridView更具格式的Excel文件,那么你可能想对EPA Plus进行考察:

http://epplus.codeplex.com/

图书馆能够创建Excel文档,能够定制电池、格式等。

If the GridView works for you, go for it, but if not there are other Excel options. You should be able to save the content to a memory stream and output the memory stream to the browser.

这里是我与协会一起使用的档案Result手的样本。 NET MVC。 这一概念可适用于网上表格,但所有这一概念都写明了答复流。 我的班子拥有一个数据表载荷(数据表)的财产,你想要浏览器下载档案(档案Name)和工作书名(著作BookName)。

    protected override void WriteFile(HttpResponseBase response)
    {
        using (ExcelPackage pck = new ExcelPackage())
        {

            ExcelWorksheet ws = pck.Workbook.Worksheets.Add(this.workBookName);

            ws.Cells["A1"].LoadFromDataTable(this.dataTable, true);

            response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            response.AddHeader("content-disposition", "attachment;  filename=" + fileName);
            response.BinaryWrite(pck.GetAsByteArray());

        }
    }

我完全同意John Saunders,你可以这样尝试。

抄录了GredView的数据集,然后下载。

用于从控制中获得价值

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);
            }
        }
    }

如今,将采用从网格中下载数据的方法。

private void ExportX()
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; Filename = ExcelReport.xls"));
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWrite);
        Table table = new Table();

        //  include the gridline settings
        table.GridLines = GridView1.GridLines;
        if (GridView1.HeaderRow != null)
        {
            PrepareControlForExport(GridView1.HeaderRow);
            table.Rows.Add(GridView1.HeaderRow);
        }
        foreach (GridViewRow row in GridView1.Rows)
        {
            PrepareControlForExport(row);
            table.Rows.Add(row);
        }
        if (GridView1.FooterRow != null)
        {
            PrepareControlForExport(GridView1.HeaderRow);
            table.Rows.Add(GridView1.FooterRow);
        }

        table.RenderControl(htmlWriter);
        Response.Write(stringWrite.ToString());
        Response.End();
    }



public override void VerifyRenderingInServerForm(Control control)
    {

    }

你们都需要这样做,即采用上述方法下载数据。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签