• c# NPOI aspx导出数据


     public static class XSSFWorkbook_Excel
        {
            /// <summary>
            /// GetExcel
            /// </summary>
            /// <param name="list">结果结合</param>
            /// <param name="parms_header">要输出的字段名</param>
            /// <param name="filename">文件名</param>
            /// <returns></returns>
            public static bool GetExcel(IList list, IDictionary<string, string> parms_header, string filename)
            {
                HttpContext curContext = HttpContext.Current;
                try
                {
                    MemoryStream ms = new MemoryStream();
                    EntityListToExcel(list, parms_header, "Sheet1").Write(ms);
                    curContext.Response.Clear();
                    curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xlsx");
                    curContext.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
                    curContext.Response.ContentEncoding = Encoding.UTF8;
                    curContext.Response.ContentType = "application/ms-excel";
                    curContext.Response.BinaryWrite(ms.ToArray());
                    ms.Close();
                    ms.Dispose();
                    curContext.Response.Flush();
                    curContext.Response.End();
                }
                catch (Exception ex)
                {
                    curContext.Response.Flush();
                    curContext.Response.End();
                }
                finally
                {
                    curContext.Response.Flush();
                    curContext.Response.End();
                }
                return true;
            }
    
            /// <summary>
            /// GetExcel
            /// </summary>
            /// <param name="list">结果结合</param>
            /// <param name="parms_header">要输出的字段名</param>
            /// <param name="filename">文件名</param>
            /// <param name="path">下载地址</param>
            /// <returns></returns>
            public static bool GetExcel(IList list, IDictionary<string, string> parms_header, string filename, out string path)
            {
                path = "/Export/" + filename + ".xlsx";
                try
                {
                    using (FileStream stm = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + path))
                    {
                        EntityListToExcel(list, parms_header, "Sheet1").Write(stm);
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            public static XSSFWorkbook EntityListToExcel(IList list, IDictionary<string, string> parms_header, string sheetName)
            {
                try
                {
                    XSSFWorkbook workbook = new XSSFWorkbook();
                    //workbook.SetSheetHidden(,)
                    //HSSFWorkbook workbook = new HSSFWorkbook(); 
                    ISheet sheet = workbook.CreateSheet(sheetName);
                    IRow row = sheet.CreateRow(0);
                    List<string> keys = parms_header.Keys.ToList();
                    for (int i = 0; i < keys.Count; i++)
                    {
                        row.CreateCell(i).SetCellValue(parms_header[keys[i]]);
                    }
                    int rowIndex = 1;
                    foreach (var item in list)
                    {
                        IRow rowTmp = sheet.CreateRow(rowIndex);
                        for (int i = 0; i < keys.Count; i++)
                        {
                            string cellValue = "";
                            object properotyValue = null;
                            System.Reflection.PropertyInfo properotyInfo = null;
    
                            if (keys[i].IndexOf(".") >= 0)
                            {
                                string[] properotyArray = keys[i].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                                string subClassName = properotyArray[0];
                                string subClassProperotyName = properotyArray[1];
                                System.Reflection.PropertyInfo subClassInfo = item.GetType().GetProperty(subClassName);
                                if (subClassInfo != null)
                                {
                                    var subClassEn = item.GetType().GetProperty(subClassName).GetValue(item, null);
                                    properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName);
                                    if (properotyInfo != null)
                                    {
                                        properotyValue = properotyInfo.GetValue(subClassEn, null);
                                    }
                                }
                            }
                            else
                            {
                                properotyInfo = item.GetType().GetProperty(keys[i]);
                                if (properotyInfo != null)
                                {
                                    properotyValue = properotyInfo.GetValue(item, null);
                                }
                            }
                            if (properotyValue != null)
                            {
                                cellValue = properotyValue.ToString();
                                if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
                                {
                                    cellValue = string.Empty;
                                }
                            }
                            rowTmp.CreateCell(i).SetCellValue(cellValue);
                        }
                        rowIndex++;
                    }
    
    
                    for (int i = 0; i <= parms_header.Count(); i++)
                    {
                        sheet.AutoSizeColumn(i);
                    }
    
                    for (int columnNum = 0; columnNum <= parms_header.Count(); columnNum++)
                    {
                        int columnWidth = sheet.GetColumnWidth(columnNum) / 256;
                        for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)
                        {
                            IRow currentRow;
                            if (sheet.GetRow(rowNum) == null)
                            {
                                currentRow = sheet.CreateRow(rowNum);
                            }
                            else
                            {
                                currentRow = sheet.GetRow(rowNum);
                            }
    
                            if (currentRow.GetCell(columnNum) != null)
                            {
                                ICell currentCell = currentRow.GetCell(columnNum);
                                int length = Encoding.Default.GetBytes(currentCell.ToString()).Length;
                                if (columnWidth < length)
                                {
                                    columnWidth = length;
                                }
                            }
                        }
                        sheet.SetColumnWidth(columnNum, columnWidth * 256);
                    }
    
                    return workbook;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    jQuery.download = function (data) {
        var inputs = '';
        Object.keys(data).forEach(function (v) {
            inputs = inputs + '<input type="hidden" name="' + v + '" value="' + data[v] + '" />';
        })
        jQuery('<form action="/xykj/system/" method="post">' + inputs + '</form>')
            .appendTo('body').submit().remove();
    };
            function Export() {
                var parm = {
                    export: 1,
                }
                $.each($("#search").serializeArray(), function () {
                    if (parm[this.name]) {
                        if (!parm[this.name].push) {
                            parm[this.name] = [parm[this.name]];
                        }
                        parm[this.name].push(this.value || '');
                    } else {
                        parm[this.name] = this.value || '';
                    }
                })
                $.download(parm);
            }

    使用Ajax提交会导致 Response.Flush();无效

  • 相关阅读:
    SQL SERVER 2008的元数据视图
    SQL Server 2008 中的 XML 功能
    SQL SERVER 2008的层次结构支持
    C#打包程序
    SQL SERVER 2008的top增强
    SQL SERVER导出数据字典
    SQL SERVER 2008的转置函数PIVOT
    SQL SERVER 2008的SQLCMD模式
    SQL SERVER 2008传递表值参数
    SQL Server 2005导出表中数据的SQL脚本形式(即INSERT语句)
  • 原文地址:https://www.cnblogs.com/WeiYongZhi/p/12077549.html
Copyright © 2020-2023  润新知