如何在angular和asp.net核心项目中导入和导出excel

如何在angular和asp.net核心项目中导入和导出excel,angular,asp.net-core,Angular,Asp.net Core,有谁能帮助我,如何使用Entity core Framework导入和导出angular和asp.net core项目中的excel文件 我有一个Excel文件,它包含10个工作表,当我将Excel工作表导入SQL server时,它将创建10个与工作表名称相关的表。在ASP.NET核心和角度项目中我得到了盖伊的 这是将excel导入ASP.NET Core和Angular Project中的SQL server中 我们已经在项目的WWWROOT文件夹中有(test.xlsx)excel文件,然

有谁能帮助我,如何使用Entity core Framework导入和导出angular和asp.net core项目中的excel文件


我有一个Excel文件,它包含10个工作表,当我将Excel工作表导入SQL server时,它将创建10个与工作表名称相关的表。在ASP.NET核心和角度项目中我得到了盖伊的

这是将excel导入ASP.NET Core和Angular Project中的SQL server中

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

使用Microsoft.AspNetCore.Hosting

使用Microsoft.AspNetCore.Mvc

使用OfficeOpenXml

使用制度

使用System.Data.SqlClient

使用System.IO

命名空间ArchDVS.Controllers

{

[路由(“api/[控制器]”)]
[ApiController]
公共类控制器:ControllerBase
{
私有只读IHostingEnvironment\u hostingEnvironment;
公共控制器(IHostingEnvironment主机环境)
{
_hostingEnvironment=hostingEnvironment;
}
[HttpGet,DisableRequestSizeLimit]
[路线(“进口”)]
公共字符串导入()
{
字符串sWebRootFolder=\u hostingEnvironment.WebRootPath;
//var excelFile=newfileinfo(@“TestFile.xlsx”);
字符串sFileName=@“test.xlsx”;
FileInfo file=newfileinfo(Path.Combine(sWebRootFolder,sFileName));
使用(var con=new SqlConnection(@“Data Source=sivan.nsp\SQLEXPRESS;Initial Catalog=DVS;Integrated Security=True;”)
{
con.Open();
尝试
{
使用(ExcelPackage epPackage=新的ExcelPackage(文件))
{
var-worksheet=epPackage.Workbook.Worksheets[1];

对于(var row=1;row请提供更多信息。您想要实现什么?导入Excel文件是什么意思?上传/下载文件以将其存储在服务器上(虽然不是Excel特定的)或通过EF或s.g.将数据从Excel文件导入数据库?顺便说一句,我只看到它是
[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;
            }
        }
    }
}