1.获取到需要导出的数据   因为这个对象是PagedResulDto类型的   所以封装成Table的时候  传pageList.Items就可以了

            PagedResultDto<SearchSatisfactionUnfeedbackTaskLineOutput> pageList = await feedbackService.SearchSatisfactionUnfeedbackTaskLine(input);
            DataTable dt = SatisfactionDataTable(pageList.Items);
            return ExportExcel(dt);

2.把数据放到Table中

        private DataTable SatisfactionDataTable(IReadOnlyList<SearchSatisfactionUnfeedbackTaskLineOutput> list)
        {
            DataTable dt = new DataTable("满意度问卷调查");
            //添加列
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.FeedbackTime), Caption = "回访日期", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.CustomerName), Caption = "客户姓名", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.Telephone), Caption = "联系电话", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.AccountModelProvinceName), Caption = "省", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.AccountModelCityName), Caption = "市", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.QualityScore), Caption = "产品实物质量(50%)", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.TimelinessScore), Caption = "交付及时性(20%)", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.ServiceScore), Caption = "服务质量(15%)", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.AppearanceScore), Caption = "产品外观(15%)", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.QuestionnaireContent), Caption = "综合评价及建议", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.remarks), Caption = "备注", DataType = typeof(string) });
            dt.Columns.Add(new DataColumn() { ColumnName = nameof(SearchSatisfactionUnfeedbackTaskLineOutput.FeedbackData), Caption = "得分", DataType = typeof(string) });
            foreach (var model in list)
            {
                //添加行
                DataRow row = dt.NewRow();
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.FeedbackTime)] = model.FeedbackTime.Value.ToLongDateString();
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.CustomerName)] = model.CustomerName;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.Telephone)] = Publics.MaskTelephone(model.Telephone);
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.AccountModelProvinceName)] = model.AccountModelProvinceName;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.AccountModelCityName)] = model.AccountModelCityName;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.QualityScore)] = model.QualityScore;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.TimelinessScore)] = model.TimelinessScore;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.ServiceScore)] = model.ServiceScore;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.AppearanceScore)] = model.AppearanceScore;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.QuestionnaireContent)] = model.QuestionnaireContent;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.remarks)] = model.remarks;
                row[nameof(SearchSatisfactionUnfeedbackTaskLineOutput.FeedbackData)] = model.FeedbackData;
                dt.Rows.Add(row);
            }
            return dt;

        }

3.把table中的数据放到xlsx工作表中  创建XSSFWorkbook对象和ISheet对象 需要引用NPOI包 可以在官网下载

        private FileResult ExportExcel(DataTable dt)
        {
            //操作xlsx表的对象
            XSSFWorkbook book = new XSSFWorkbook();
            //创建一个工作表
            ISheet sheet = book.CreateSheet(dt.TableName);
            ;
            //给xlsx工作表创建第0行
            IRow headerRow = sheet.CreateRow(rowIndex);
            rowIndex++;
            //设置样式
            ICellStyle style = book.CreateCellStyle();
            style.VerticalAlignment = VerticalAlignment.Center;
            //循环table中的列名  放到xlsx中当列名
            ; i < dt.Columns.Count; i++)
            {
                //获取当前列 创建第i个单元格 把列名放到单元格中
                DataColumn column = dt.Columns[i];
                ICell cell = headerRow.CreateCell(i);
                cell.SetCellValue(column.Caption);
            }
            //循环table中的数据
            foreach (DataRow dataRow in dt.Rows)
            {
                //在xlsx工作表中创建第rowIndex行
                IRow row = sheet.CreateRow(rowIndex);
                ; i < dt.Columns.Count; i++)
                {
                    //每次都创建一个cell单元格 来接收当前列的数据值
                    DataColumn column = dt.Columns[i];
                    ICell cell = row.CreateCell(i);
                    if (column.DataType == typeof(string))
                    {
                        if (dataRow[column] != null)
                        {
                            cell.SetCellValue(dataRow[column].ToString());
                        }
                    }
                    else if (column.DataType == typeof(int))
                    {
                        if (dataRow[column] != null)
                        {
                            cell.SetCellValue(double.Parse(dataRow[column].ToString()));
                        }
                    }
                    else if (column.DataType == typeof(DateTime))
                    {
                        if (dataRow[column] != null)
                        {
                            //cell.SetCellValue(DateTime.Parse(dataRow[column].ToString()));
                            cell.SetCellValue(dataRow[column].ToString());
                        }
                    }
                }
                rowIndex++;
            }
            ; i < dt.Columns.Count; i++)
            {
                //设置第i列宽度自适应
                sheet.AutoSizeColumn(i);
            }

            var ms = new NPOIMemoryStream();
            book.Write(ms);
            ms.Flush();
            ms.Position = ;
            return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", dt.TableName + ".xlsx");
        }

4.导出需要用到的一个对象

    public class NPOIMemoryStream : MemoryStream
    {
        /// <summary>
        /// 获取流是否关闭
        /// </summary>
        public bool IsColse
        {
            get;
            private set;
        }

        public NPOIMemoryStream(bool colse = false)
        {
            IsColse = colse;
        }

        public override void Close()
        {
            if (IsColse)
            {
                base.Close();
            }
        }
    }

5.返回是调用File()  第一个参数 当前流  第二个参数 book(当前xlsx工作表的地址  可以调试看到)  第三个参数要下载的xlsx的名字

关于ISheet接口中的属性方法介绍

https://github.com/dotnetcore/NPOI/blob/master/src/NPOI/SS/UserModel/Sheet.cs

关于Stream、FileStream、MemoryStream的区别

***********************************

把查询的数据导出到elsx表 关于流的概念的更多相关文章

  1. 数据库查询的数据导出到xls表,集合数据导出到xls表

    //实体类package com.outxls; public class Student { private Integer studentId; private String studentNam ...

  2. 使用SSM框架实现Sql数据导出成Excel表

    SSM框架实现SQL数据导出Excel 思路 首先在前端页面中添加一个导出功能的button,然后与后端controller进行交互. 接着在相应的controller中编写导出功能方法. 方法体: ...

  3. java中的数据导出到Excel表中

    整个项目中导出数据到.Excel的源码 import java.io.BufferedOutputStream; import java.io.FileInputStream; import java ...

  4. 将数据导出成excel表

    /// <summary> /// 生成excel表 /// </summary> /// <param name="dt">数据表</p ...

  5. jsp页面查询的数据导出到excel

    java导入导出excel操作(jxl) jxl.jar 包下载地址:http://www.andykhan.com/jexcelapi/真实下载地址:http://www.andykhan.com/ ...

  6. Python 把数据库的数据导出到excel表

    import io,xlwt def export_excel(request): """导出数据到excel表""" list_obj = ...

  7. python 用xlwt包把数据导出到excel表中

    def write_excel(): f = xlwt.Workbook() #创建工作簿 ''' 创建第一个sheet: sheet1 ''' sheet1 = f.add_sheet(u'shee ...

  8. 【ITOO 1】将List数据导出Excel表

    需求描述:在课表导入的时候,首先给用户提供模板(excel),然后将用户填写好的数据读取到list集合中.再进行判空处赋值处理,以及去重处理.这篇博客,主要介绍读取excel表和导出excel表的方法 ...

  9. 将C1Chart数据导出到Excel

    大多数情况下,当我们说将图表导出到Excel时,意思是将Chart当成图片导出到Excel中.如果是这样,你可以参考帮助文档中保存和导出C1Chart章节. 不过,也有另一种情况,当你想把图表中的数据 ...

随机推荐

  1. (Linux)动态度的编写

    动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...

  2. 腾讯云 网站开启HTTPS

    下图是我站点的初始化样子,可以看到只是输出一个字符串,啥也没有,并且没有https. 这无所谓,因为我们的重点是https,而不是网站内容 接下来就是配置https的关键步骤了,其实只需要三步而已: ...

  3. P1288 取数游戏II

    luogu原题 最近刚学了博弈论,拿来练练手qwq 其实和数值的大小并没有关系 我们用N/P态来表示必胜/必败状态 先在草稿纸上探究硬币♦在最左侧(其实左右侧是等价的)的一条长链的N/P态,设链长为n ...

  4. 微信小程序细节

    微信小程序开发几个细节: 1.界面传值 ①全局参数传值 <!--结果--> <view wx:for="{{data}}" class="case pr ...

  5. 12: nginx原理及常用配置

    1.1 nginx基本介绍 1.nginx高并发原理( 多进程+epoll实现高并发 ) 1. Nginx 在启动后,会有一个 master 进程和多个相互独立的 worker 进程. 2. 每个子进 ...

  6. cscope for golang

    从 https://gist.github.com/bopjiang/11146574 下载, 做了修改. cscope-go.sh #!/bin/bash # generate cscope ind ...

  7. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  8. centos 7.2 安装gitlab汉化

    ####################你如果搜到我的这个博客,你的系统得是centos 7的   80端口没有占用.  QQ:1394466404   这个博客维护1年 #### 多地方第一个是百度 ...

  9. topcoder srm 335 div1

    problem1 link 直接模拟即可. import java.util.*; import java.math.*; import static java.lang.Math.*; public ...

  10. Ubuntu 系统学习

    apt 命令 apt-get remove [app] # 删除已安装的软件包(保留配置文件),不会删除依赖软件包,且保留配置文件 apt-get remove --pure [app] # 删除已安 ...