zoukankan      html  css  js  c++  java
  • C# 导出Excel表格

    function exportExcel()
            {
                //查询条件
                var startTime = F.ui.startTime.getText() == "" ? null : F.ui.startTime.getText();
                var endTime = F.ui.endTime.getText() == "" ? null : F.ui.endTime.getText();
                var major = F.ui.major.getText() == "" ? "" : F.ui.major.getText();
                var Department = F.ui.Department.getText() == "" ? "" : F.ui.Department.getText();
                var ReportPerson = F.ui.ReportPerson.getText() == "" ? "" : F.ui.ReportPerson.getText();
                var RectPerson = F.ui.RectPerson.getText() == "" ? "" : F.ui.RectPerson.getText();
    
                var data = "startTime=" + startTime + "&endTime=" + endTime + "&major=" + major + "&Department=" + Department + "&ReportPerson=" + ReportPerson + "&RectPerson=" + RectPerson;
                download('Export', data, 'post');           
            }
            function download(url, data, method)
            {
                if (url && data) {
                    data = typeof data == 'string' ? data : jQuery.param(data);
                    var inputs = '';
                    $.each(data.split('&'), function () {
                        var pair = this.split('=');
                        inputs += '<input type="hidden" name = "' + pair[0] + '" value ="' + pair[1] + '">';
                    });
                    $('<form action = "' + url + '" method ="' + (method || 'post') + '">' + inputs + '</form>').appendTo('body').submit().remove();
                }
            }

    后台接收方法添加NOPI引用

     public FileStreamResult Export(HiddenDangerDto input)
            {
                int recordCount = 0;
                var list = HiddenDangerService.GetAll(input, 1, Int32.MaxValue, out recordCount);
                HSSFWorkbook book = ExportExcel.GridToExcelByNPOI<HiddenDangerDto>(list);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                book.Write(ms);
                ms.Seek(0, System.IO.SeekOrigin.Begin);
                var cookie = new HttpCookie("time");
                cookie.Values.Add("time", "1");
                Response.AppendCookie(cookie);
                return File(ms, "application/vnd.ms-excel", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            }

     类必须加[Display(Name = "")]

    public class HiddenDangerDto
        {
            public int ID { get; set; }
            [Display(Name = "时间")]
            public DateTime Time { get; set; }
            [Display(Name = "专业")]
            public string major { get; set; }
            [Display(Name = "部门")]
            public string Department { get; set; }
            [Display(Name = "隐患上报人")]
            public string ReportPerson { get; set; }
            [Display(Name = "整改责任人")]
            public string RectPerson { get; set; }
            [Display(Name = "隐患描述")]
            public string Description { get; set; }
            [Display(Name = "整改措施")]
            public string RectMeasures { get; set; }        
        }
  • 相关阅读:
    simple-LDAP-auth
    User Attributes
    webpack 模块标识符(Module Identifiers)
    详解webpack中的hash、chunkhash、contenthash区别
    [转] 插件兼容CommonJS, AMD, CMD 和 原生 JS
    Exif.js 读取图像的元数据
    [转] 跨域
    [转] 理解Web路由
    [转] React 是什么
    [转] Web MVC简介
  • 原文地址:https://www.cnblogs.com/lcidy/p/10650855.html
Copyright © 2011-2022 走看看