Ejemplo n.º 1
0
        public IActionResult ExcelBackupBase(dynamic excelData, string excelName, bool isDynamic = false, string savePath = "")
        {
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            if (Directory.Exists(savePath))
            {
                var    factory = ExcelEntityFactory.GetInstance();
                byte[] result  = null;
                if (!isDynamic)
                {
                    result = factory.CreateWriteToExcel().ExportListToExcel(excelData, null, true);
                }
                else
                {
                    var dtResult = DynamicHelper.ToDataTable(excelData);
                    result = factory.CreateWriteToExcel().ExportDataTableToExcel(dtResult, null, true);
                }
                string fileName = $"{excelName}-{DateTime.Now:yyyyMMddHHmmss}.xlsx";
                System.IO.File.WriteAllBytes($@"{savePath}\{fileName}", result);
            }
            return(new EmptyResult());
        }
        /// <summary>
        /// 从Excel中读取数据,
        /// </summary>
        /// <typeparam name="T">与Excel对应的数据模型</typeparam>
        /// <param name="fileStream">文件Stream</param>
        /// <param name="startCellName">数据开始的单元格,如Key在A列,只有一个标题,则数据开始的单元格为"B1"</param>
        /// <returns></returns>
        public virtual Dictionary <string, T> GetMessageFromExcelWithFileStream <T>() where T : new()
        {
            var factory = ExcelEntityFactory.GetInstance();
            var TPropertyNameDisplayAttributeNameDic = GetTPropertyNameDisplayAttributeNameDic <T>();
            var result = factory.CreateReadFromExcel().ExcelToEntityDictionary <T>(TPropertyNameDisplayAttributeNameDic, fileStream, out StringBuilder errorMesg, null, startCellName, keyColumn);

            return(result);
        }
 /// <summary>
 /// 验证Excel的格式是否为指定格式
 /// </summary>
 /// <returns></returns>
 protected virtual bool ValidExcelFormat()
 {
     if (excelType.Equals(ExcelEntityFactory.GetInstance().Excel2007ContentType))
     {
         return(true);
     }
     return(false);
 }
        private Dictionary <string, DepartmentFromExcelViewModel> GetDepartmentMessageFromExcel(Stream fileStream)
        {
            var factory = ExcelEntityFactory.GetInstance();
            var TPropertyNameDisplayAttributeNameDic = GetTPropertyNameDisplayAttributeNameDic <DepartmentFromExcelViewModel>();
            var result = factory.CreateReadFromExcel().ExcelToEntityDictionary <DepartmentFromExcelViewModel>(TPropertyNameDisplayAttributeNameDic, fileStream, out StringBuilder errorMesg);

            ViewBag.ErrorMessage = errorMesg.ToString();
            return(result);
        }
        public ActionResult TestExportListToExcelWithOutisShowSlNoIsFalse()
        {
            var factory = ExcelEntityFactory.GetInstance();

            var testData  = GetTestData();
            var titleHead = GetTitleList();
            var result    = factory.CreateWriteToExcel().ExportListToExcel <TestA>(testData, null, false);

            return(new FileContentResult(result, factory.CreateWriteToExcel().ExcelContentType));
        }
        /// <summary>
        /// 文件存在,且文件名是以xlsx结尾
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        private bool IsValidFile(List <IFormFile> files)
        {
            bool result = false;

            if (files != null && files.Count > 0)
            {
                if (files.All(a => a.ContentType.Equals(ExcelEntityFactory.GetInstance().Excel2007ContentType)))
                {
                    result = true;
                }
            }
            return(result);
        }
Ejemplo n.º 7
0
        public static FileContentResult ExcelExport(dynamic excelData, string excelName, List <string> heading, bool isShowSlNo, List <string> keySelectors, List <string> tails)
        {
            var factory = ExcelEntityFactory.GetInstance();

            byte[] result = null;
            // var listResult = DynamicHelper.ToDynamicList(DynamicHelper.ToDataTable(excelData));
            DataTable dtResult = DynamicHelper.ToDataTable(excelData);

            result = factory.CreateWriteToExcel().ExportListToExcel(dtResult, heading, isShowSlNo, keySelectors, tails);
            var returnResult =
                new FileContentResult(result, factory.CreateWriteToExcel().ExcelContentType)
            {
                FileDownloadName = $"{excelName}-{DateTime.Now:yyyyMMddHHmmss}.xlsx"
            };

            return(returnResult);
        }
Ejemplo n.º 8
0
        public static FileContentResult ExcelExport <T>(List <T> excelData, string excelName, List <string> heading, bool isShowSlNo, List <string> keySelectors, List <string> tails, bool isGroupTotalExcel = false, List <string> coloumsColor = null, List <string> hideColoums = null)
        {
            var factory = ExcelEntityFactory.GetInstance();

            byte[]    result   = null;
            DataTable dtResult = ListHelper.ToDataTable(excelData, hideColoums);

            result = !isGroupTotalExcel
                    ? factory.CreateWriteToExcel().ExportListToExcel(dtResult, heading, isShowSlNo, keySelectors, tails)
                    : factory.CreateWriteToExcel().ExportGroupListToExcel(dtResult, heading, isShowSlNo, keySelectors, tails, coloumsColor);

            var returnResult =
                new FileContentResult(result, factory.CreateWriteToExcel().ExcelContentType)
            {
                FileDownloadName = $"{excelName}-{DateTime.Now:yyyyMMddHHmmss}.xlsx"
            };

            return(returnResult);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Excel导出,暂时支持实体类和DataTable数据源
        /// </summary>
        /// <param name="excelData"></param>
        /// <param name="excelName"></param>
        /// <param name="isModel"></param>
        /// <returns></returns>
        public static FileContentResult ExcelExport <T>(List <T> excelData, string excelName, bool isDynamic = false, bool isShowSlNo = true)
        {
            var factory = ExcelEntityFactory.GetInstance();

            byte[] result = null;
            if (!isDynamic)
            {
                result = factory.CreateWriteToExcel().ExportListToExcel(excelData, null, isShowSlNo);
            }
            else
            {
                var dtResult = ListHelper.ToDataTable(excelData);
                result = factory.CreateWriteToExcel().ExportDataTableToExcel(dtResult, null, isShowSlNo);
            }
            var returnResult =
                new FileContentResult(result, factory.CreateWriteToExcel().ExcelContentType)
            {
                FileDownloadName = $"{excelName}-{DateTime.Now:yyyyMMddHHmmss}.xlsx"
            };

            return(returnResult);
        }
Ejemplo n.º 10
0
 static ExcelEntityFactory()
 {
     // create the instance only if the instance is null
     Instance = new ExcelEntityFactory();
 }