当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET Core 导入导出Excel xlsx 文件实例

ASP.NET Core 导入导出Excel xlsx 文件实例

2017年12月12日  | 移动技术网IT编程  | 我要评论

琪琪黄鳝门视频,朴有天尹恩惠恋爱,狱乐营

asp.net core 使用epplus.core导入导出excel xlsx 文件,epplus.core支持excel 2007/2010 xlsx文件导入导出,可以运行在windows, linux和mac。

epplus.core 是基于epplus 更改而来,在linux 下需要安装libgdiplus 。

epplus:

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

@{ 
}
<h2>asp.net core 导入导出excel xlsx 文件</h2>
<a asp-action="export">导出excel</a>

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

 

导入xlsx文件

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

index.cshtml

@{ 
}
<h2>asp.net core 导入导出excel xlsx 文件</h2>
<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://www.lhsxpumps.com/_localhost:5000/xlsx

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

asp.net core简单的导入导出excel 功能也就完成了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网