• 大小: 24.19MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-06-17
  • 语言: C#
  • 标签: c#  .NETCORE  npoi  layui  

资源简介

简单,易懂的ASP.NET Core NPOI导出复杂Word、Excel和Excel数据导入示例讲解

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using YY_Dal;
using YY_Model;
using YY_Utility;

namespace YY_Services
{
    /// 
    /// Excel文档生成并保存和Excel文档中的数据批量导出操作类
    /// 

    public class NpoiExcelOperationService
    {
        private static IHostingEnvironment _environment;

        private readonly SchoolUserInfoContext _shoSchoolUserInfoContext;

        public NpoiExcelOperationService(SchoolUserInfoContext schoolUserInfoContext IHostingEnvironment iEnvironment)
        {
            _shoSchoolUserInfoContext = schoolUserInfoContext;
            _environment = iEnvironment;
        }

        /// 
        /// Excel数据导出简单示例
        /// 

        /// 导出结果
        /// 保存excel文件路径
        /// 
        public bool ExcelDataExport(out string resultMsg out string excelFilePath)
        {
            var result = true;
            excelFilePath = ““;
            resultMsg = “successfully“;
            //Excel导出名称
            string excelName = “人才培训课程表“;
            try
            {
                //首先创建Excel文件对象
                var workbook = new HSSFWorkbook();

                //创建工作表,也就是Excel中的sheet,给工作表赋一个名称(Excel底部名称)
                var sheet = workbook.CreateSheet(“人才培训课程表“);

                //sheet.DefaultColumnWidth = 20;//默认列宽

                sheet.ForceFormulaRecalculation = true;//TODO:是否开始Excel导出后公式仍然有效(非必须)

                #region table 表格内容设置

                #region 标题样式

                //设置顶部大标题样式
                var cellstyleFont = NpoiExcelExportHelper._.Createstyle(workbook HorizontalAlignment.Center VerticalAlignment.Center 20 true 700 “楷体“ true false false true FillPattern.SolidForeground HSSFColor.Coral.Index HSSFColor.White.Index
                    FontUnderlineType.None FontSuperscript.None false);

                //第一行表单
                var row = NpoiExcelExportHelper._.CreateRow(sheet 0 28);

                var cell = row.CreateCell(0);
                //合并单元格 例: 第1行到第2行 第3列到第4列围成的矩形区域

                //TODO:关于Excel行列单元格合并问题
                /**
                  第一个参数:从第几行开始合并
                  第二个参数:到第几行结束合并
                  第三个参数:从第几列开始合并
                  第四个参数:到第几列结束合并
                **/
                CellRangeAddress region = new CellRangeAddress(0 0 0 5);
                sheet.AddMergedRegion(region);

                cell.SetCellValue(“人才培训课程表“);//合并单元格后,只需对第一个位置赋值即可(TODO:顶部标题)
                cell.Cellstyle = cellstyleFont;

                //二级标题列样式设置
                var headTopstyle = NpoiExcelExportHelper._.Createstyle(workbook HorizontalAlignment.Center Ver

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件         16  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\COMMIT_EDITMSG

     文件        423  2020-08-28 02:28  NPOI-ExportWordAndExcel-ImportExcelData\.git\config

     文件         73  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\description

     文件        278  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\FETCH_HEAD

     文件         23  2020-09-08 00:11  NPOI-ExportWordAndExcel-ImportExcelData\.git\HEAD

     文件        478  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\applypatch-msg.sample

     文件        896  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\commit-msg.sample

     文件       3327  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\fsmonitor-watchman.sample

     文件        189  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\post-update.sample

     文件        424  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\pre-applypatch.sample

     文件       1638  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\pre-commit.sample

     文件       1348  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\pre-push.sample

     文件       4898  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\pre-rebase.sample

     文件        544  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\pre-receive.sample

     文件       1492  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\prepare-commit-msg.sample

     文件       3610  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\hooks\update.sample

     文件      32431  2020-09-08 00:11  NPOI-ExportWordAndExcel-ImportExcelData\.git\index

     文件        240  2020-08-25 23:56  NPOI-ExportWordAndExcel-ImportExcelData\.git\info\exclude

     文件       1314  2020-09-08 00:11  NPOI-ExportWordAndExcel-ImportExcelData\.git\logs\HEAD

     文件        728  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\logs\refs\heads\develop-.NetCore3.1

     文件        217  2020-08-26 00:06  NPOI-ExportWordAndExcel-ImportExcelData\.git\logs\refs\heads\master

     文件        294  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\logs\refs\remotes\origin\develop-.NetCore3.1

     文件        217  2020-08-26 00:06  NPOI-ExportWordAndExcel-ImportExcelData\.git\logs\refs\remotes\origin\HEAD

     文件        147  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\ms-persist.xml

     文件      13559  2020-08-28 02:27  NPOI-ExportWordAndExcel-ImportExcelData\.git\objects\00\8cadd20ef1a917c0b064a76a37cf39deb84420

     文件       9914  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\objects\00\abe810812c13ed61b85e67dd2428acb4b713cb

     文件      30022  2020-08-28 02:28  NPOI-ExportWordAndExcel-ImportExcelData\.git\objects\00\b0754412231f804c690dfb44446954fed66130

     文件      10926  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\objects\01\ee061b501144a8ea197bf7302efc934b2a7f40

     文件      10409  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\objects\02\b08e17def0786314e1081272d4f10d1e93664f

     文件      24085  2020-09-08 00:10  NPOI-ExportWordAndExcel-ImportExcelData\.git\objects\02\eb9c0996eccab31fc30c8a9963ab0d95332527

............此处省略1315个文件信息

评论

共有 条评论