按模板生成下载数据【NPOI】

注意:NPOI单独下载的包不可和Magicodes.IE共同引用,会导致Magicodes.IE无法导出,要共用的话需下载以下包才可以共用
如需共同引用,NPOI需要引用这个
导出的效果如图:
在这里插入图片描述

下面是实现的代码:(通过位置查找替换)

        /// <summary>
        /// 按模板生成下载数据
        /// </summary>
        /// <param name="input"></param>
        [HttpPost("printexcle2")]
        [UnitOfWork]//事务开启工作单元
        [ApiExplorerSettings(IgnoreApi = false)]//接口可见性
        public async Task<IActionResult> PrintExcle2(QueryePerformanceAssessorsInput input)
        {
            //return null;
            try
            {
                var httpQueryData = (await _performanceAssessorsRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt<PerformanceAssessorsInput>();
                if (httpQueryData == null) throw Oops.Oh(ErrorCode.D1012);
                var prints = await _performanceItemsRep.DetachedEntities.Where(a => a.DbId == input.Id).ProjectToType<PerformanceItemsOutput>().ToListAsync();
                string path = "D:\\绩效考核模板\\绩效考核表.xlsx";//模板路径
                Random random = new Random();
                string week = httpQueryData.StartTime.Year + "年" + httpQueryData.StartTime.Month + "月第" + (httpQueryData.StartTime.Day / 7 + (httpQueryData.StartTime.Day % 7 == 0 ? 0 : 1)) + "周";
                var localTemplatPath = @"D:\\" + httpQueryData.BelongName + week + random.Next(100000, 999999) + "绩效考核表.xlsx";//新生成文件路径以及文件名与格式
                FileInput2 model = new FileInput2();
                model.DbId = input.Id;
                model.Module = httpQueryData.BelongName;
                //var fs11 = System.IO.File.OpenRead(path);
                string extension11 = Path.GetExtension(path);
                //if (fs11 != null)
                //{
                //上传附件
                var fileInformation = await _ISysFileService.UniversalUpload3(extension11, 0, model, path, week);
                localTemplatPath = fileInformation.Path;//生成服务器路径
                //}
                //System.IO.File.Copy(path, localTemplatPath);
                //fs11.Close();
                if (localTemplatPath.IsNullOrEmpty()) throw Oops.Oh(ErrorCode.D4001);
                using (var fs = System.IO.File.OpenRead(localTemplatPath))
                {
                    string extension = Path.GetExtension(localTemplatPath);
                    //此处应该再判断文件的格式是xls还是xlsx,如果是xlsx则使用XSSFWorkbook
                    //HSSFWorkbook:是操作Excel2003以前(包括2003)的版本、XSSFWorkbook:是操作Excel2007的版本
                    IWorkbook workbook = null;
                    if (extension.Equals(".xls"))
                    {
                        //把xls文件中的数据写入workbook中
                        workbook = new HSSFWorkbook(fs);
                    }
                    else
                    {
                        //把xlsx文件中的数据写入workbook中
                        workbook = new XSSFWorkbook(fs);
                    }
                    fs.Close();
                    var sheet = workbook.GetSheetAt(0);
                    var rowSource = sheet.GetRow(5);
                    var Mycellstyle1 = rowSource.RowStyle;
                    var MyFont1 = workbook.CreateFont();//字体设置
                    MyFont1.IsBold = false;
                    MyFont1.FontName = "宋体";
                    Mycellstyle1.SetFont(MyFont1);//设置单元格格字体
                    //基本信息(Row和Cell分别代表着行、列,计数都是从0开始,比如说Excel里面第一个格子就是0,0)
                    //此处代码是根据自己的模板去写GetRow与GetCell的值
                    var rowIndex = 0;
                    sheet.GetRow(rowIndex).GetCell(0).SetCellValue("(" + httpQueryData.DeptName + ")-" + week + "绩效考核表");
                    rowIndex = 1;
                    sheet.GetRow(rowIndex).GetCell(3).SetCellValue(httpQueryData.BelongName);
                    sheet.GetRow(rowIndex).GetCell(5).SetCellValue("xxx");
                    sheet.GetRow(rowIndex).GetCell(8).SetCellValue(httpQueryData.DeptName);
                    //sheet.GetRow(rowIndex).GetCell(12).SetCellValue("开发工程师");
                    rowIndex = 2;
                    // sheet.GetRow(rowIndex).GetCell(3).SetCellValue(DateTime.Now.ToString("yyyy-MM-dd"));
                    sheet.GetRow(rowIndex).GetCell(5).SetCellValue(httpQueryData.StartTime.ToString("yyyy-MM-dd") + "-" + httpQueryData.EndTime.ToString("yyyy-MM-dd"));
                    sheet.GetRow(rowIndex).GetCell(8).SetCellValue(DateTime.Now.ToString("yyyy-MM-dd"));
                    sheet.GetRow(rowIndex).GetCell(12).SetCellValue(((double)httpQueryData.SynthesisScore));
                    //****动态列表信息,先加行,在填入信息
                    List<PerformanceItemsOutput> prints1=  prints.Where(a => a.Project == "KPI指标(80%)").ToList();
                    if (prints1.Count > 0)
                    {
                        //插入行,并复制第6行的样式
                        for (int i = 0; i < prints1.Count - 1; i++)
                        {
                            var newCell = sheet.CopyRow(6, 7 + i);
                        }
                        rowIndex = 6;
                        for (int i = 0; i < prints1.Count; i++)
                        {
                            var curRow = sheet.GetRow(rowIndex);
                            var data = prints1[i];
                            curRow.GetCell(0).SetCellValue(data.Project);
                            curRow.GetCell(1).SetCellValue(i + 1);
                            curRow.GetCell(2).SetCellValue(data.AssessmentIndicators);
                            curRow.GetCell(4).SetCellValue(data.DefinitionIndicators);
                            //curRow.GetCell(3).SetCellValue(data.ExpectedCompletionTime.ToString());
                            curRow.GetCell(7).SetCellValue(data.ScoringCriteria);
                            curRow.GetCell(8).SetCellValue(data.Weight + "%");
                            curRow.GetCell(9).SetCellValue(data.DataSources);
                            curRow.GetCell(10).SetCellValue(data.ScoreTransaction);
                            curRow.GetCell(13).SetCellValue(data.SuperiorRating == null ? 0 : (double)data.SuperiorRating);
                            curRow.GetCell(14).SetCellValue(data.ScoreItem == null ? 0 : (double)data.ScoreItem);
                            rowIndex = rowIndex + 1;
                        }
                        rowIndex = rowIndex + prints1.Count + 1;
                        sheet.GetRow(rowIndex).GetCell(9).SetCellValue((double)httpQueryData.KPIWeightedSubtotal);
                        CellRangeAddress cellRangeAddress = new CellRangeAddress(6, rowIndex - 1, 0, 0); // 合并第1行的第1个单元格到第3个单元格
                        sheet.AddMergedRegion(cellRangeAddress);
                        var prints2 = prints.Where(a => a.Project == "工作能力与态度(20%)").ToList();
                        var pmodel = prints2.Where(a => a.Number == 1 && a.DefinitionIndicators == "有很强的责任心,从不推卸责任,积极主动地圆满完成工作").FirstOrDefault();
                        if (pmodel != null)
                        {
                            sheet.GetRow(rowIndex + 2).GetCell(9).SetCellValue(pmodel.ScoreTransaction);
                            sheet.GetRow(rowIndex + 2).GetCell(13).SetCellValue(pmodel.SuperiorRating == null ? 0 : (double)pmodel.SuperiorRating);
                            sheet.GetRow(rowIndex + 2).GetCell(14).SetCellValue(pmodel.ScoreItem == null ? 0 : (double)pmodel.ScoreItem);
                        }
                        var pmodel2 = prints2.Where(a => a.Number == 2 && a.DefinitionIndicators == "执行力强,能创造性的开展工作,提前并保质保量完成目标任务").FirstOrDefault();
                        if (pmodel2 != null)
                        {
                            sheet.GetRow(rowIndex + 6).GetCell(9).SetCellValue(pmodel2.ScoreTransaction);
                            sheet.GetRow(rowIndex + 6).GetCell(13).SetCellValue(pmodel2.SuperiorRating == null ? 0 : (double)pmodel2.SuperiorRating);
                            sheet.GetRow(rowIndex + 6).GetCell(14).SetCellValue(pmodel2.ScoreItem == null ? 0 : (double)pmodel2.ScoreItem);
                        }
                        var pmodel3 = prints2.Where(a => a.Number == 3 && a.DefinitionIndicators == "持续地积极努力工作,并以此带动其他人的工作").FirstOrDefault();
                        if (pmodel3 != null)
                        {
                            sheet.GetRow(rowIndex + 10).GetCell(9).SetCellValue(pmodel3.ScoreTransaction);
                            sheet.GetRow(rowIndex + 10).GetCell(13).SetCellValue(pmodel3.SuperiorRating == null ? 0 : (double)pmodel3.SuperiorRating);
                            sheet.GetRow(rowIndex + 10).GetCell(14).SetCellValue(pmodel3.ScoreItem == null ? 0 : (double)pmodel3.ScoreItem);
                        }
                        var pmodel4 = prints2.Where(a => a.Number == 4 && a.DefinitionIndicators == "学习能力很强,善于从不同渠道学习多专业知识,并能很好的吸收,并触类旁通").FirstOrDefault();
                        if (pmodel4 != null)
                        {
                            sheet.GetRow(rowIndex + 14).GetCell(9).SetCellValue(pmodel4.ScoreTransaction);
                            sheet.GetRow(rowIndex + 14).GetCell(13).SetCellValue(pmodel4.SuperiorRating == null ? 0 : (double)pmodel4.SuperiorRating);
                            sheet.GetRow(rowIndex + 14).GetCell(14).SetCellValue(pmodel4.ScoreItem == null ? 0 : (double)pmodel4.ScoreItem);
                        }
                        sheet.GetRow(rowIndex + 18).GetCell(9).SetCellValue((double)httpQueryData.WorkWeightedSubtotal);
                        var pmodel5 = prints.Where(a => a.Number == 1 && a.Project == "调整分数项").FirstOrDefault();
                        if (pmodel5 != null)
                        {
                            sheet.GetRow(rowIndex + 19).GetCell(14).SetCellValue(pmodel5.ScoreItem == null ? 0 : (double)pmodel5.ScoreItem);
                        }
                        var pmodel6 = prints.Where(a => a.Number == 2 && a.Project == "调整分数项").FirstOrDefault();
                        if (pmodel6 != null)
                        {
                            sheet.GetRow(rowIndex + 20).GetCell(14).SetCellValue(pmodel6.ScoreItem == null ? 0 : (double)pmodel6.ScoreItem);
                        }
                        sheet.GetRow(rowIndex + 21).GetCell(9).SetCellValue((double)httpQueryData.SynthesisScore);
                        sheet.GetRow(rowIndex + 23).GetCell(8).SetCellValue((double)httpQueryData.SynthesisScore);
                    }
                    byte[] buffer = new byte[1024 * 2];
                    FileStream fs1 = new FileStream(localTemplatPath, FileMode.Create, FileAccess.ReadWrite);
                    workbook.Write(fs1);
                    fs1.Close();
                    //var memoryStream = new MemoryStream(buffer);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        workbook.Write(ms);
                        buffer = ms.ToArray();
                        ms.Close();
                        var memoryStream = new MemoryStream(buffer);
                        return File(buffer, "application/msword");
                        new FileExtensionContentTypeProvider().TryGetContentType(localTemplatPath, out string contentType);
                        //return await _npoiFile.Action(buffer, contentType, (httpQueryData.DeptName + httpQueryData.BelongName + week + "绩效考核表.xlsx"));
                        return await Task.FromResult(new FileStreamResult(memoryStream, contentType)
                        {
                            FileDownloadName = System.Web.HttpUtility.UrlEncode((httpQueryData.DeptName + httpQueryData.BelongName + week + "绩效考核表.xlsx"))
                        });
                    }

                    //return null;
                }
            }
            catch (Exception ex)
            {

                throw Oops.Oh(ex.Message);
            }
        }

欢迎使用Markdown编辑器

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

新的改变

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 全新的界面设计 ,将会带来全新的写作体验;
  2. 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
  3. 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
  4. 全新的 KaTeX数学公式 语法;
  5. 增加了支持甘特图的mermaid语法1 功能;
  6. 增加了 多屏幕编辑 Markdown文章功能;
  7. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
  8. 增加了 检查列表 功能。

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G

合理的创建标题,有助于目录的生成

直接输入1次#,并按下space后,将生成1级标题。
输入2次#,并按下space后,将生成2级标题。
以此类推,我们支持6级标题。有助于使用TOC语法后生成一个完美的目录。

如何改变文本的样式

强调文本 强调文本

加粗文本 加粗文本

标记文本

删除文本

引用文本

H2O is是液体。

210 运算结果是 1024.

插入链接与图片

链接: link.

图片: Alt

带尺寸的图片: Alt

居中的图片: Alt

居中并且带尺寸的图片: Alt

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// An highlighted block
var foo = 'bar';

生成一个适合你的列表

  • 项目
    • 项目
      • 项目
  1. 项目1
  2. 项目2
  3. 项目3
  • 计划任务
  • 完成任务

创建一个表格

一个简单的表格是这么创建的:

项目Value
电脑$1600
手机$12
导管$1

设定内容居中、居左、居右

使用:---------:居中
使用:----------居左
使用----------:居右

第一列第二列第三列
第一列文本居中第二列文本居右第三列文本居左

SmartyPants

SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:

TYPEASCIIHTML
Single backticks'Isn't this fun?'‘Isn’t this fun?’
Quotes"Isn't this fun?"“Isn’t this fun?”
Dashes-- is en-dash, --- is em-dash– is en-dash, — is em-dash

创建一个自定义列表

Markdown
Text-to- HTML conversion tool
Authors
John
Luke

如何创建一个注脚

一个具有注脚的文本。2

注释也是必不可少的

Markdown将文本转换为 HTML

KaTeX数学公式

您可以使用渲染LaTeX数学表达式 KaTeX:

Gamma公式展示 Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ(n)=(n1)!nN 是通过欧拉积分

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=0tz1etdt.

你可以找到更多关于的信息 LaTeX 数学表达式here.

新的甘特图功能,丰富你的文章

2014-01-07 2014-01-09 2014-01-11 2014-01-13 2014-01-15 2014-01-17 2014-01-19 2014-01-21 已完成 进行中 计划一 计划二 现有任务 Adding GANTT diagram functionality to mermaid
  • 关于 甘特图 语法,参考 这儿,

UML 图表

可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图:

张三 李四 王五 你好!李四, 最近怎么样? 你最近怎么样,王五? 我很好,谢谢! 我很好,谢谢! 李四想了很长时间, 文字太长了 不适合放在一行. 打量着王五... 很好... 王五, 你怎么样? 张三 李四 王五

这将产生一个流程图。:

链接
长方形
圆角长方形
菱形
  • 关于 Mermaid 语法,参考 这儿,

FLowchart流程图

我们依旧会支持flowchart的流程图:

Created with Raphaël 2.3.0 开始 我的操作 确认? 结束 yes no
  • 关于 Flowchart流程图 语法,参考 这儿.

导出与导入

导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。


  1. mermaid语法说明 ↩︎

  2. 注脚的解释 ↩︎

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值