如何创建和使用asp.net下载Excel文档 [英] how to create and download excel document using asp.net

查看:261
本文介绍了如何创建和使用asp.net下载Excel文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建和下载使用asp.net excel文件?

How to create and download excel document using asp.net ?

的目的是使用XML,LINQ或任何通过浏览器的Excel文档发送给客户。

The purpose is to use xml, linq or whatever to send an excel document to a customer via a browser.

编辑:使用情况

用户负荷在浏览器中的GridView(与Ajax框架制造),在GridView直接连接到SQL数据库。
我把一个按钮导出到Excel,让客户节省他的计算机上这个gridview的数据ANSD我想推出一个excel干净下载。

The customer load a gridview ( made with ajax framework ) in a browser, the gridview is directly linked to an sql database. I put a button 'export to excel' to let customer save this gridview data on his computer ansd i would like to launch a clean download of an excel.

这里提出的解决方案是不干净,像发送HTML文档,将头excel文件等,我搜索上codePLEX一个简单的解决方案,现在,我会让你知道。

The solutions proposed here are not clean, like send an html document and change the header to excel document etc, i'm searching a simple solution on codeplex right now, i will let you know.

推荐答案

首先,我已经下载的<一个href=\"http://www.microsoft.com/downloads/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&DisplayLang=en\"相对=nofollow> Open XML格式SDK 2.0

Starter kit

First i have downloaded the Open XML Format SDK 2.0.

它配备了3有用的工具:

It comes with 3 useful tools in :

C:\\ Program Files文件\\ Open XML格式SDK \\ V2.0 \\工具

C:\Program Files\Open XML Format SDK\V2.0\tools


  • DocumentReflector.exe 至极汽车
    生成C#打造
    US preadsheet从code。

  • OpenXmlClassesExplorer.exe 显示
    ECMA规范和类
    文档(使用MSDN风格
    格式)。

  • OpenXmlDiff.exe 图形比较
    可用两个开放式XML文件和搜索
    错误。

  • DocumentReflector.exe wich auto generate the c# to build a spreadsheet from the code.
  • OpenXmlClassesExplorer.exe display Ecma specification and the class documentation (using an MSDN style format).
  • OpenXmlDiff.exe graphically compare two Open XML files and search for errors.

我建议任何人谁开始的改名 * 的.xlsx *的的* *。拉链,所以你可以看到谁推动了S preadsheet(对于例如我们的表是在XL \\工作表)的XML文件。

I suggest anyone who begin to rename *.xlsx *to* *.zip, so you can see the XML files who drive our spreadsheet ( for the example our sheets are in "xl\worksheets" ).

免责声明:我偷了所有的code从的 MSDN技术文章 ; d

Disclaimer : I have stolen all the code from an MSDN technical article ;D

以下code使用*的.xlsx模板我手动进行,以便能够对其进行修改。

The following code use an *.xlsx template i made manually to be able to modify it.

命名空间的引用

using System.IO;
using System.Xml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;


// Database object
        DataClassesDataContext db = new DataClassesDataContext();

        // Make a copy of the template file.
        File.Copy(@"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\livreurs.xlsx", @"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\generated.xlsx", true);

        // Open the copied template workbook. 
        using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(@"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\generated.xlsx", true))
        {
            // Access the main Workbook part, which contains all references.
            WorkbookPart workbookPart = myWorkbook.WorkbookPart;

            // Get the first worksheet. 
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.ElementAt(2);

            // The SheetData object will contain all the data.
            SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

            // Begining Row pointer                       
            int index = 2;

            // Database results
            var query = from t in db.Clients select t;

            // For each item in the database, add a Row to SheetData.
            foreach (var item in query)
            {
                // Cell related variable
                string Nom = item.Nom;

                // New Row
                Row row = new Row();
                row.RowIndex = (UInt32)index;

                // New Cell
                Cell cell = new Cell();
                cell.DataType = CellValues.InlineString;
                // Column A1, 2, 3 ... and so on
                cell.CellReference = "A"+index;

                // Create Text object
                Text t = new Text();
                t.Text = Nom;

                // Append Text to InlineString object
                InlineString inlineString = new InlineString();
                inlineString.AppendChild(t);

                // Append InlineString to Cell
                cell.AppendChild(inlineString);

                // Append Cell to Row
                row.AppendChild(cell);

                // Append Row to SheetData
                sheetData.AppendChild(row);

                // increase row pointer
                index++;                

            }

            // save
            worksheetPart.Worksheet.Save();

        }

我还没有说完呢,我的第二份工作是自动下载在S preadsheet修改后

I havent finished yet, my second job is to auto download the spreadsheet after modification.

最后,我将用户重定向到我的生成S- predsheet(从我的aspx)

Finally, i redirect the user to my generated spredsheet (from my aspx)

 context.Response.Redirect("Oxml-tpl/generated.xlsx");

这篇关于如何创建和使用asp.net下载Excel文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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