【问题标题】:How to import and export excel in angular and asp.net core project如何在angular和asp.net core项目中导入导出excel
【发布时间】:2018-12-10 10:09:38
【问题描述】:

谁能帮助我,如何使用 Entity Core Framework 在 Angular 和 asp.net 核心项目中导入和导出 excel 文件?

我有一个 Excel 文件,它包含 10 个工作表,当我将 excel 工作表导入 SQL 服务器时,它将创建 10 个与工作表名称相关的表。在 ASP.NET CORE 和 Angular 项目中

【问题讨论】:

  • 请提供更多信息。你想达到什么目标?导入 Excel 文件是什么意思?上传/下载文件以将它们存储在服务器上(不是特定于 excel)或通过 EF 将 Excel 文件中的 data 导入数据库?或 s.g.别的?顺便说一句,我只是看到它是 stackoverflow.com/questions/51041960/… 的副本

标签: angular asp.net-core


【解决方案1】:

我知道了

这是在 ASP.NET Core 和 Angular 项目中将 excel 导入 SQL 服务器

我们已经在项目的 WWWROOT 文件夹中有 (test.xlsx)excel 文件,然后执行以下代码

使用 Microsoft.AspNetCore.Hosting;

使用 Microsoft.AspNetCore.Mvc;

使用 OfficeOpenXml;

使用系统;

使用 System.Data.SqlClient;

使用 System.IO;

命名空间 ArchDVS.Controllers

{

[Route("api/[controller]")]

[ApiController]

public class ExcelController : ControllerBase

{
    private readonly IHostingEnvironment _hostingEnvironment;


    public ExcelController(IHostingEnvironment hostingEnvironment)

    {
        _hostingEnvironment = hostingEnvironment;
    }
    [HttpGet, DisableRequestSizeLimit]
    [Route("Import")]
    public string Import()
    {
        string sWebRootFolder = _hostingEnvironment.WebRootPath;
        //var excelFile = new FileInfo(@"TestFile.xlsx");
        string sFileName = @"test.xlsx";
        FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));

        using (var con = new SqlConnection(@"Data Source=sivan.nsp\SQLEXPRESS;Initial Catalog=DVS;Integrated Security=True;"))
        {
            con.Open();
            try
            {
                using (ExcelPackage epPackage = new ExcelPackage(file))
                {
                    var worksheet = epPackage.Workbook.Worksheets[1];
                    for (var row = 1; row <= worksheet.Dimension.End.Row; row++)
                    {
                        var rowValues = worksheet.Cells[row, 1, row, worksheet.Dimension.End.Column];
                        var cmd = new SqlCommand("INSERT INTO test(ID, Name, Gender, Salary) VALUES (@contactid, @firstname, @secondname, @age)", con);
                        cmd.Parameters.AddWithValue("@contactid", rowValues["A1"].Value);
                        cmd.Parameters.AddWithValue("@firstname", rowValues["B1"].Value);
                        cmd.Parameters.AddWithValue("@secondname", rowValues["C1"].Value);
                        cmd.Parameters.AddWithValue("@age", rowValues["D1"].Value);
                        cmd.ExecuteNonQuery();

                    }
                }
                return "sucessfully uploded";
            }
            catch (System.Exception ex)
            {
                return "Some error occured while importing." + ex.Message;
            }
        }
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多