温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET Core怎么导入导出Excel xlsx文件

发布时间:2021-09-03 09:57:35 来源:亿速云 阅读:170 作者:chen 栏目:开发技术

本篇内容介绍了“ASP.NET Core怎么导入导出Excel xlsx文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Linux和Mac。

EPPlus.Core 是基于EPPlus 更改而来,在Linux 下需要安装libgdiplus 。

EPPlus:http://epplus.codeplex.com/

EPPlus.Core:https://github.com/VahidN/EPPlus.Core

下面在ASP.NET Core 中导入导出Excel xlsx 文件。

新建项目

新建一个ASP.NET Core Web Application 项目ASPNETCoreExcel,选择Web 应用程序 不进行身份验证。

然后添加EPPlus.Core 引用。

使用NuGet 命令行:

Install-Package EPPlus.Core

也可以使用NuGet包管理器安装。

导出xlsx文件

新建一个XlsxController ,添加Export 操作。

 public class XlsxController : Controller
 {
  private IHostingEnvironment _hostingEnvironment;

  public XlsxController(IHostingEnvironment hostingEnvironment)
  {
   _hostingEnvironment = hostingEnvironment;
  }
  public IActionResult Index()
  {
   return View();
  }

  public IActionResult Export()
  {
   string sWebRootFolder = _hostingEnvironment.WebRootPath;
   string sFileName = $"{Guid.NewGuid()}.xlsx";
   FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
   using (ExcelPackage package = new ExcelPackage(file))
   {
    // 添加worksheet
    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("aspnetcore");
    //添加头
    worksheet.Cells[1, 1].Value = "ID";
    worksheet.Cells[1, 2].Value = "Name";
    worksheet.Cells[1, 3].Value = "Url";
    //添加值
    worksheet.Cells["A2"].Value = 1000;
    worksheet.Cells["B2"].Value = "LineZero";
    worksheet.Cells["C2"].Value = "http://www.cnblogs.com/linezero/";

    worksheet.Cells["A3"].Value = 1001;
    worksheet.Cells["B3"].Value = "LineZero GitHub";
    worksheet.Cells["C3"].Value = "https://github.com/linezero";
    worksheet.Cells["C3"].Style.Font.Bold = true;

    package.Save(); 
   }
   return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  }
 }

通过依赖注入获取HostingEnvironment,对应可以获取程序的相关目录及属性。

然后添加Index 视图增加一个链接导出Excel

@{ 
}
<h3>ASP.NET Core 导入导出Excel xlsx 文件</h3>
<a asp-action="Export">导出Excel</a>

点击导出文件,打开结果如下。

 ASP.NET Core怎么导入导出Excel xlsx文件

导入xlsx文件

在index视图中添加一个上传文件,添加Import操作。

Index.cshtml

@{ 
}
<h3>ASP.NET Core 导入导出Excel xlsx 文件</h3>
<a asp-action="Export">导出Excel</a>
<hr />
<form enctype="multipart/form-data" method="post" asp-action="Import">
 <input type="file" name="excelfile" />
 <input type="submit" value="上传" />
</form>
 [HttpPost]
  public IActionResult Import(IFormFile excelfile)
  {
   string sWebRootFolder = _hostingEnvironment.WebRootPath;
   string sFileName = $"{Guid.NewGuid()}.xlsx";
   FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
   try
   {
    using (FileStream fs = new FileStream(file.ToString(), FileMode.Create))
    {
     excelfile.CopyTo(fs);
     fs.Flush();
    }
    using (ExcelPackage package = new ExcelPackage(file))
    {
     StringBuilder sb = new StringBuilder();
     ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
     int rowCount = worksheet.Dimension.Rows;
     int ColCount = worksheet.Dimension.Columns;
     bool bHeaderRow = true;
     for (int row = 1; row <= rowCount; row++)
     {
      for (int col = 1; col <= ColCount; col++)
      {
       if (bHeaderRow)
       {
        sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
       }
       else
       {
        sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
       }
      }
      sb.Append(Environment.NewLine);
     }
     return Content(sb.ToString());
    }
   }
   catch (Exception ex)
   {
    return Content(ex.Message);
   }
  }

运行程序打开http://localhost:5000/xlsx

ASP.NET Core怎么导入导出Excel xlsx文件

上传对应文件,显示如下。

ASP.NET Core怎么导入导出Excel xlsx文件

“ASP.NET Core怎么导入导出Excel xlsx文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI