zoukankan      html  css  js  c++  java
  • C#使用EPPlus插件导出Excel

    Nuget添加EPPlus插件

    using OfficeOpenXml;
    using OfficeOpenXml.Style;
            private void InitQueryRecordExcel(ICBCQueryRecord[] list)
            {
                ExcelPackage pck = new ExcelPackage();
    
                #region Dealer
    
                var ws = pck.Workbook.Worksheets.Add("QueryRecord");
    
                ws.Cells["A1"].Value = "证件类型";
                ws.Cells["B1"].Value = "证件号码";
                ws.Cells["C1"].Value = "企业名称/客户姓名";
                ws.Cells["D1"].Value = "查询人";
                ws.Cells["E1"].Value = "查询日期";
                ws.Cells["F1"].Value = "查询结果";
                ws.Cells["G1"].Value = "报告路径";
                ws.Cells["A1:G1"].Style.Font.Bold = true;
                ws.Cells[ws.Dimension.Address].AutoFitColumns();
    
                var row = 2;
                foreach (var item in list)
                {
                    var outlineLevel = 0;
                    var reportUrl = string.Empty;
                    if (!string.IsNullOrEmpty(item.RespCode) && !string.IsNullOrEmpty(item.SOURCE_REQUEST))
                    {
                        if (item.RespCode.Equals(BPCResult.ICBC_Query_SuccessCode) && item.SOURCE_REQUEST.Equals(SourceRequestEnum.CCG_FRONTEND.ToString()))
                        {
                            if (string.IsNullOrEmpty(item.ECM_STORAGE_URL))
                            {
                                reportUrl = GetCurApplicationUrl.AbsoluteUri + string.Format("/BPC/Dingding/Report.aspx?qid={0}", item.QUERY_RECORD_ID);
                            }
                            else
                            {
                                reportUrl = item.ECM_STORAGE_URL;
                            }
                        }
                    }
                    //base info
                    ws.Cells[row, 1].Value = (item.IDENTIFY_TYPE.Equals(0) ? "个人" : (item.IDENTIFY_TYPE.Equals(100) ? "企业" : item.IDENTIFY_TYPE.ToString()));
                    ws.Cells[row, 2].Value = item.IDENTIFY_NUMBER;
                    ws.Cells[row, 3].Value = item.APPLICANT_NAME;
                    ws.Cells[row, 4].Value = item.QUERY_EMPLOYEE;
                    ws.Cells[row, 5].Value = item.CREATED_DATETIME;
                    ws.Cells[row, 5].Style.Numberformat.Format = "yyyy-MM-dd HH:mm:ss";
                    ws.Cells[row, 6].Value = ((!string.IsNullOrEmpty(item.RespCode) && !string.IsNullOrEmpty(item.RespMsg)) ? (item.RespCode + "," + item.RespMsg) : (string.Empty));
                    if (!string.IsNullOrEmpty(reportUrl))
                    {
                        ws.Cells[row, 7].StyleName = "HyperLink";
                        ws.Cells[row, 7].Hyperlink = new Uri(reportUrl);
                    }
                    ws.Cells[row, 7].Value = reportUrl;
                    ws.Row(row).OutlineLevel = outlineLevel;
                    row++;
    
                }
                int rowCount = 1 + list.Count();
    
                ws.Cells["A1:G" + rowCount].Style.Border.Top.Style = ExcelBorderStyle.Thin;
                ws.Cells["A1:G" + rowCount].Style.Border.Left.Style = ExcelBorderStyle.Thin;
                ws.Cells["A1:G" + rowCount].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                ws.Cells["A1:G" + rowCount].Style.Border.Right.Style = ExcelBorderStyle.Thin;
    
                ws.OutLineSummaryBelow = false;
    
                #endregion
    
                string fileName = "BPCQueryRecord_" + DateTime.Now.ToString("yyyyMMddHHmmss");
    
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;  filename=" + fileName + ".xlsx");
    
                HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                HttpContext.Current.Response.BinaryWrite(pck.GetAsByteArray());
                HttpContext.Current.Response.End();
            }
  • 相关阅读:
    基本MVVM 和 ICommand用法举例(转)
    WPF C# 命令的运行机制
    628. Maximum Product of Three Numbers
    605. Can Place Flowers
    581. Shortest Unsorted Continuous Subarray
    152. Maximum Product Subarray
    216. Combination Sum III
    448. Find All Numbers Disappeared in an Array
    268. Missing Number
    414. Third Maximum Number
  • 原文地址:https://www.cnblogs.com/hofmann/p/13496825.html
Copyright © 2011-2022 走看看