Exemplo n.º 1
0
        public async Task ExportTest_导出Excel_正确导出Excel手动观察()
        {
            string curDir  = Environment.CurrentDirectory;
            string fileUrl = Path.Combine(curDir, DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");

            var carDTO = new ExcelCarTemplateDTO()
            {
                Age            = 10,
                CarCode        = "鄂A123456",
                Gender         = GenderEnum.男,
                IdentityNumber = "test",
                Mobile         = "test",
                Name           = "test",
                RegisterDate   = DateTime.Now
            };

            var list = new List <ExcelCarTemplateDTO>();

            for (int i = 0; i < 10; i++)
            {
                list.Add(carDTO);
            }

            var bytes = await _excelExportService.ExportAsync <ExcelCarTemplateDTO>(new ExportOption <ExcelCarTemplateDTO>()
            {
                Data = list
            });

            File.WriteAllBytes(fileUrl, bytes);
        }
Exemplo n.º 2
0
        public async Task ExportTest_NPOI最大条数性能测试_性能测试()
        {
            string curDir  = Environment.CurrentDirectory;
            string fileUrl = Path.Combine(curDir, DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");

            var bytes = await _excelExportService.ExportAsync(_datas);

            File.WriteAllBytes(fileUrl, bytes);
        }
Exemplo n.º 3
0
        public async Task Test_Export()
        {
            var workbook = await _excelImportService.ImportAsync <Barcode>(new ImportOptions()
            {
                FileUrl = "D:\\导入国标码_导入格式.xlsx",
            });

            var result = workbook.GetResult <Barcode>();

            var bytes = await _excelExportService.ExportAsync(new ExportOptions <Barcode>()
            {
                Data = result.ToList()
            });

            await File.WriteAllBytesAsync($"D:\\测试导出_{DateTime.Now:yyyyMMddHHmmss}.xlsx", bytes);
        }
        public async Task Test_Export_DataAnnotations()
        {
            var filePath = GetTestFilePath($"{nameof(Test_Export_DataAnnotations)}.xlsx");

            DeleteFile(filePath);
            var data   = GenFu.GenFu.ListOf <ExportTestDataAnnotations>();
            var result = await _excelExportService.ExportAsync(new ExportOptions <ExportTestDataAnnotations>
            {
                Data = data
            });

            await File.WriteAllBytesAsync(filePath, result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Excel导出
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="fileData">数据源</param>
        /// <param name="fileName">导出文件名称(注意唯一)</param>
        /// <param name="filePath">数据源</param>
        /// <returns></returns>
        public async Task <bool> ExcelExport <T>(List <T> fileData, string fileName, string filePath = "") where T : class, new()
        {
            try
            {
                string curDir = string.Empty;
                // 默认导出路径
                if (String.IsNullOrEmpty(filePath))
                {
                    curDir = AppContext.BaseDirectory + "/Export";
                }
                else
                {
                    curDir = filePath;
                }
                // 文件夹是否存在
                if (Directory.Exists(curDir))
                {
                    Directory.CreateDirectory(curDir);
                }

                string fileUrl = Path.Combine(curDir, fileName + ".xlsx");

                var bytes = await _excelExportService.ExportAsync(new ExportOption <T>()
                {
                    // 数据源
                    Data = fileData,
                    // 数据行起始索引,默认1
                    DataRowStartIndex = 1,
                    // 导出Excel类型,默认xls
                    // ExcelType = Bayantu.Extensions.Office.Enums.ExcelTypeEnum.XLS,
                    // 表头行索引,默认0
                    HeaderRowIndex = 0,
                    // 页签名称,默认sheet1
                    SheetName = "sheet1"
                });

                File.WriteAllBytes(fileUrl, bytes);
                return(true);
            }
            catch (Exception ex)
            {
                NLogHelper.ErrorLog(ex.Message, ex);
                return(false);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="baseDir">基础路径</param>
        /// <param name="fileName">文件名。不包含扩展名</param>
        /// <param name="exportFields">导出字段</param>
        private async Task <ExportResult> CreateFileAsync(string baseDir, string fileName, string exportFields)
        {
            baseDir  = baseDir.TrimEnd('\\').TrimEnd('/');
            baseDir += "/";
            string absFilePath = Common.GetPhysicalPath(baseDir);

            DirectoryHelper.CreateIfNotExists(absFilePath);

            var newFileName = $"{fileName}_{DateTime.Now.ToString("yyyyMMddHHmmss_fff")}.xlsx";

            var filePath = Path.Combine(baseDir, newFileName);

            absFilePath = Path.Combine(absFilePath, newFileName);
            var bytes = await _excelExportService.ExportAsync(_options);

            File.WriteAllBytes(absFilePath, bytes);
            return(new ExportResult()
            {
                FileName = fileName,
                Extension = ".xlsx",
                AbsFilePath = absFilePath,
                FilePath = filePath
            });
        }