【问题标题】:Import and export data to excel for asp.net mvc using entity framework使用实体框架将数据导入和导出到 excel for asp.net mvc
【发布时间】:2015-05-26 10:30:07
【问题描述】:

我想将 excel 文件数据导入和导出到 SQL Server 使用 ASP.NET MVC 使用实体框架。

我发现的所有代码都在不使用实体框架的情况下解释了这一点。那我该怎么做呢?

【问题讨论】:

    标签: sql-server asp.net-mvc excel entity-framework


    【解决方案1】:

    EPPlus 是一个 .NET 库,它使用 Open Office Xml 格式 (xlsx) 读取和写入 Excel 2007/2010 文件。这是在 ASP.net MVC 中使用此库的示例代码。

    public FileContentResult Download()
    {
    
        var fileDownloadName = String.Format("FileName.xlsx");
        const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
    
        // Pass your ef data to method
        ExcelPackage package = GenerateExcelFile(_db.Contexts.ToList());
    
        var fsr = new FileContentResult(package.GetAsByteArray(), contentType);
        fsr.FileDownloadName = fileDownloadName;
    
        return fsr;
    }
    
    private static ExcelPackage GenerateExcelFile(IEnumerable<Context> datasource)
    {
    
        ExcelPackage pck = new ExcelPackage();
    
        //Create the worksheet 
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet 1");
    
        // Sets Headers
        ws.Cells[1, 1].Value = "Column 1";
        ws.Cells[1, 2].Value = "Column 2";
        ws.Cells[1, 3].Value = "Column 3";
    
        // Inserts Data
        for (int i = 0; i < datasource.Count(); i++)
        {
            ws.Cells[i + 2, 1].Value = datasource.ElementAt(i).Serial;
            ws.Cells[i + 2, 2].Value = datasource.ElementAt(i).WarrantyStart;
            ws.Cells[i + 2, 3].Value = datasource.ElementAt(i).WarrantyEnd;
        }
    
        // Format Header of Table
        using (ExcelRange rng = ws.Cells["A1:C1"])
        {
    
            rng.Style.Font.Bold = true;
            rng.Style.Fill.PatternType = ExcelFillStyle.Solid; //Set Pattern for the background to Solid 
            rng.Style.Fill.BackgroundColor.SetColor(Color.Gold); //Set color to DarkGray 
            rng.Style.Font.Color.SetColor(Color.Black);
        }
        return pck;
    }
    

    并在您的视图中插入此链接,它将下载您的文件。

    @Html.ActionLink("Download Data as Excel", "Download");
    

    同样您可以从 excel 中导入数据,here 是一个入门示例。

    【讨论】:

    • 这里我正在尝试像这样填充数据表 ExcelPackage package = GenerateExcelFile(db.Product.ToList()); 但我收到以下错误 cannot convert from System.Collections.Generic.List&lt;project.Models.Product&gt;' to 'System.Collections.Generic.IEnumerable&lt;Microsoft.Ajax.Utilities.Context&gt;'
    • @kez,将IEnumerable&lt;Context&gt; datasource 重构为IEnumerable&lt;project.Models.Product&gt; datasource
    • 我只是按照你说的改了private static ExcelPackage GenerateExcelFile(IEnumerable&lt;project.Models.Product&gt; datasource) {,然后错误改成了cannot convert from 'System.Collections.Generic.List&lt;project.Models.AB_Product&gt;' to 'System.Collections.Generic.IEnumerable&lt;project.Models.Product&gt;'
    • 这里Context 表示您的收藏类型。所以我想看看异常是project.Models.Product,替换它project.Models.AB_Product,它应该可以解决问题
    • 我可以给这种excel表格自定义标题吗?如果可以,语法是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多