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

bel*_*laz 11 c# asp.net excel openxml

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

目的是使用xml,linq或其他任何方式通过浏览器向客户发送excel文档.

编辑:用例

客户在浏览器中加载gridview(使用ajax框架),gridview直接链接到sql数据库.我把一个按钮'export to excel'让客户在他的计算机上保存这个gridview数据我希望启动一个excel的干净下载.

这里提出的解决方案并不干净,比如发送一个html文档并将标题更改为excel文档等,我现在正在搜索一个关于codeplex的简单解决方案,我会告诉你.

bel*_*laz 15

入门套件

首先,我已经下载了Open XML Format SDK 2.0.

它配备了3个有用的工具:

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

  • DocumentReflector.exe 自动生成c#以从代码构建电子表格.
  • OpenXmlClassesExplorer.exe 显示Ecma规范和类文档(使用MSDN样式格式).
  • OpenXmlDiff.exe 以图形方式比较两个Open XML文件并搜索错误.

我建议任何开始重命名 .xlsx的 .zip,这样你就可以看到驱动我们的电子表格的XML文件了(例如我们的工作表在"xl\worksheets"中).


代码

免责声明:我从MSDN技术文章中窃取了所有代码; D.

以下代码使用我手动制作的*.xlsx模板来修改它.

命名空间引用

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

        }
Run Code Online (Sandbox Code Playgroud)

我还没有完成,我的第二个工作是修改后自动下载电子表格.


最后,我将用户重定向到我生成的spredsheet(来自我的aspx)

 context.Response.Redirect("Oxml-tpl/generated.xlsx");
Run Code Online (Sandbox Code Playgroud)