c# NPOI aspx导出数据

2021-01-22 18:12

阅读:579

标签:width   pre   ase   stat   pdo   bytes   pat   except   asp   

 public static class XSSFWorkbook_Excel
    {
        /// 
        /// GetExcel
        /// 
        /// 结果结合
        /// 要输出的字段名
        /// 文件名
        /// 
        public static bool GetExcel(IList list, IDictionarystring, 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;
        }

        /// 
        /// GetExcel
        /// 
        /// 结果结合
        /// 要输出的字段名
        /// 文件名
        /// 下载地址
        /// 
        public static bool GetExcel(IList list, IDictionarystring, 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, IDictionarystring, 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);
                Liststring> keys = parms_header.Keys.ToList();
                for (int i = 0; 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 )
                    {
                        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 )
                {
                    sheet.AutoSizeColumn(i);
                }

                for (int columnNum = 0; columnNum )
                {
                    int columnWidth = sheet.GetColumnWidth(columnNum) / 256;
                    for (int rowNum = 1; 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 + ‘;
    })
    jQuery(
‘ + inputs + ‘
) .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();无效

c# NPOI aspx导出数据

标签:width   pre   ase   stat   pdo   bytes   pat   except   asp   

原文地址:https://www.cnblogs.com/WeiYongZhi/p/12077549.html


评论


亲,登录后才可以留言!