/// <summary>
        /// 根据现有的既定行数,导出Excel文件
        /// </summary>
        /// <param name="allRows"></param>
        /// <param name="excelFileCount"></param>
        /// <param name="exportFileInfo"></param>
        /// <param name="tablesCountDic"></param>
        /// <param name="querySqlPart"></param>
        /// <param name="whereSqlPart"></param>
        /// <param name="token"></param>
        /// <param name="exportProgressAction"></param>
        /// <param name="exportFileCompletedAction"></param>
        public void ExportExcel(int allRows, int excelFileCount, ExportFileInfo exportFileInfo, Dictionary <string, int> tablesCountDic, string querySqlPart, string whereSqlPart, CancellationToken token, Action <int> exportProgressAction, Action <string> exportFileCompletedAction)
        {
            List <string> tableNameList  = tablesCountDic.Keys.ToList();
            List <int>    tableCountList = tablesCountDic.Values.ToList();
            int           sheetCount     = allRows / Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT + allRows % Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT == 0 ? 0 : 1;

            Func <int, string> getCurrectTableName = (int tableIndex) =>
            {
                return(tableNameList[tableIndex]);
            };

            using (MySqlConnection conn = SqlHelper.GetConnection())
            {
                conn.Open();

                int tableIndex = 0;
                int currectTableOccupancyRows = tableCountList[tableIndex]; //当前表的剩余记录数
                int currectTableStartRowIdx   = 0;                          //当前表起始行号
                int dispId = 0;

                for (int fileIndex = 1; fileIndex <= excelFileCount; fileIndex++)
                {
                    string filePath = ExcelFileHelper.GetExportFileName(exportFileInfo.ExcelTitle, exportFileInfo.SaveDirectory, fileIndex, excelFileCount);


                    QueryDataReaderWriteExcel(conn, allRows, sheetCount, exportFileInfo, filePath, token, querySqlPart, tableNameList, tableCountList, whereSqlPart, ref tableIndex, ref currectTableOccupancyRows, ref currectTableStartRowIdx, ref dispId, exportProgressAction);
                    if (exportFileCompletedAction != null)
                    {
                        exportFileCompletedAction(filePath);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 简单实现导出到bytes
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public async Task <byte[]> ExportBytesByTemplate <T>(T data, string template) where T : class
        {
            var file   = new ExportFileInfo(template, "text/html");
            var result = await ExportByTemplate(data, template);

            return(Encoding.UTF8.GetBytes(result));
        }
Esempio n. 3
0
        /// <summary>
        ///     根据模板导出列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="dataItems"></param>
        /// <param name="htmlTemplate"></param>
        /// <returns></returns>
        public async Task <ExportFileInfo> ExportListByTemplate <T>(string fileName, ICollection <T> dataItems,
                                                                    string htmlTemplate = null) where T : class
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException(Resource.FileNameMustBeFilled, nameof(fileName));
            }
            var exporter   = new HtmlExporter();
            var htmlString = await exporter.ExportListByTemplate(dataItems, htmlTemplate);

            using (var generatedDocument = new MemoryStream())
            {
                using (var package =
                           WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
                {
                    var mainPart = package.MainDocumentPart;
                    if (mainPart == null)
                    {
                        mainPart = package.AddMainDocumentPart();
                        new Document(new Body()).Save(mainPart);
                    }

                    var converter = new HtmlConverter(mainPart);
                    converter.ParseHtml(htmlString);

                    mainPart.Document.Save();
                }

                File.WriteAllBytes(fileName, generatedDocument.ToArray());
                var fileInfo = new ExportFileInfo(fileName,
                                                  "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                return(fileInfo);
            }
        }
Esempio n. 4
0
        private async Task Initialize(CancellationToken cancellationToken)
        {
            // Each resource type can have multiple files. We need to keep track of the latest file.
            foreach (KeyValuePair <string, List <ExportFileInfo> > output in _exportJobRecord.Output)
            {
                int            maxSequence = -1; // sequence can start with 0
                ExportFileInfo latestFile  = null;
                foreach (ExportFileInfo file in output.Value)
                {
                    if (file.Sequence > maxSequence)
                    {
                        maxSequence = file.Sequence;
                        latestFile  = file;
                    }
                }

                _resourceTypeToFileInfoMapping.Add(output.Key, latestFile);

                // If there are entries in ExportJobRecord Output before FileManager gets initialized,
                // it means we are "restarting" an export job. We have to make sure that each file
                // has been opened on the ExportDestinationClient.
                await _exportDestinationClient.OpenFileAsync(latestFile.FileUri, cancellationToken);
            }

            _isInitialized = true;
        }
Esempio n. 5
0
        private async Task ProcessSearchResultsAsync(IEnumerable <SearchResultEntry> searchResults, string partId, IAnonymizer anonymizer, CancellationToken cancellationToken)
        {
            foreach (SearchResultEntry result in searchResults)
            {
                ResourceWrapper resourceWrapper = result.Resource;

                string resourceType = resourceWrapper.ResourceTypeName;

                // Check whether we already have an existing file for the current resource type.
                if (!_resourceTypeToFileInfoMapping.TryGetValue(resourceType, out ExportFileInfo exportFileInfo))
                {
                    // Check whether we have seen this file previously (in situations where we are resuming an export)
                    if (_exportJobRecord.Output.TryGetValue(resourceType, out exportFileInfo))
                    {
                        // A file already exists for this resource type. Let us open the file on the client.
                        await _exportDestinationClient.OpenFileAsync(exportFileInfo.FileUri, cancellationToken);
                    }
                    else
                    {
                        // File does not exist. Create it.
                        string fileName;
                        if (_exportJobRecord.StorageAccountContainerName.Equals(_exportJobRecord.Id, StringComparison.OrdinalIgnoreCase))
                        {
                            fileName = $"{resourceType}.ndjson";
                        }
                        else
                        {
                            string dateTime = _exportJobRecord.QueuedTime.UtcDateTime.ToString("s")
                                              .Replace("-", string.Empty, StringComparison.OrdinalIgnoreCase)
                                              .Replace(":", string.Empty, StringComparison.OrdinalIgnoreCase);
                            fileName = $"{dateTime}-{_exportJobRecord.Id}/{resourceType}.ndjson";
                        }

                        Uri fileUri = await _exportDestinationClient.CreateFileAsync(fileName, cancellationToken);

                        exportFileInfo = new ExportFileInfo(resourceType, fileUri, sequence: 0);

                        // Since we created a new file the JobRecord Output also needs to know about it.
                        _exportJobRecord.Output.TryAdd(resourceType, exportFileInfo);
                    }

                    _resourceTypeToFileInfoMapping.Add(resourceType, exportFileInfo);
                }

                ResourceElement element = _resourceDeserializer.Deserialize(resourceWrapper);

                if (anonymizer != null)
                {
                    element = anonymizer.Anonymize(element);
                }

                // Serialize into NDJson and write to the file.
                byte[] bytesToWrite = _resourceToByteArraySerializer.Serialize(element);

                await _exportDestinationClient.WriteFilePartAsync(exportFileInfo.FileUri, partId, bytesToWrite, cancellationToken);

                // Increment the file information.
                exportFileInfo.IncrementCount(bytesToWrite.Length);
            }
        }
Esempio n. 6
0
        public async Task GivenExportJobRecordV1WithFilesInOutput_WhenWriteToFile_ThenAllFilesAreOpened()
        {
            InitializeManagerWithV1ExportJobRecord();

            string         resourceType = "Patient";
            ExportFileInfo file1        = new ExportFileInfo(resourceType, new Uri("https://localhost/Patient.ndjson"), sequence: 0);

            string         resourceType2 = "Observation";
            ExportFileInfo file2         = new ExportFileInfo(resourceType2, new Uri("https://localhost/Observation.ndjson"), sequence: 0);

            _exportJobRecord.Output.Add(resourceType, new List <ExportFileInfo>()
            {
                file1
            });
            _exportJobRecord.Output.Add(resourceType2, new List <ExportFileInfo>()
            {
                file2
            });

            await _exportFileManager.WriteToFile("Claim", "partId", new byte[] { 1 }, _cancellationTokenNone);

            await _exportDestinationClient.Received(1).OpenFileAsync(Arg.Is(file1.FileUri), Arg.Is(_cancellationTokenNone));

            await _exportDestinationClient.Received(1).OpenFileAsync(Arg.Is(file2.FileUri), Arg.Is(_cancellationTokenNone));
        }
Esempio n. 7
0
        /// <summary>
        ///     获取文档转换配置
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="htmlString"></param>
        /// <returns></returns>
        private async Task <ExportFileInfo> ExportPdf(string fileName,
                                                      PdfExporterAttribute pdfExporterAttribute,
                                                      string htmlString)
        {
            var objSettings = new ObjectSettings
            {
#if !NET461
                HtmlContent = htmlString,
                Encoding    = Encoding.UTF8,
                PagesCount  = pdfExporterAttribute.IsEnablePagesCount ? true : (bool?)null,
#else
                HtmlText   = htmlString,
                CountPages = pdfExporterAttribute.IsEnablePagesCount ? true : (bool?)null,
#endif
                WebSettings = { DefaultEncoding = Encoding.UTF8.BodyName },
            };

            if (pdfExporterAttribute?.HeaderSettings != null)
            {
                objSettings.HeaderSettings = pdfExporterAttribute?.HeaderSettings;
            }

            if (pdfExporterAttribute?.FooterSettings != null)
            {
                objSettings.FooterSettings = pdfExporterAttribute?.FooterSettings;
            }

            var htmlToPdfDocument = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    PaperSize   = pdfExporterAttribute?.PaperKind,
                    Orientation = pdfExporterAttribute?.Orientation,

#if !NET461
                    //Out = fileName,
                    ColorMode      = ColorMode.Color,
#else
                    ProduceOutline = true,
#endif
                    DocumentTitle  = pdfExporterAttribute?.Name
                },
                Objects =
                {
                    objSettings
                }
            };
            var result = PdfConverter.Convert(htmlToPdfDocument);

#if NETSTANDARD2_1
            await File.WriteAllBytesAsync(fileName, result);
#else
            File.WriteAllBytes(fileName, result);
#endif

            var fileInfo = new ExportFileInfo(fileName, "application/pdf");
            return(await Task.FromResult(fileInfo));
        }
Esempio n. 8
0
        /// <summary>
        /// 导出HTML文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        /// <param name="htmlTemplate"></param>
        /// <returns></returns>
        public async Task <ExportFileInfo> ExportByTemplate <T>(string fileName, T data,
                                                                string htmlTemplate) where T : class
        {
            var file   = new ExportFileInfo(fileName, "text/html");
            var result = await ExportByTemplate(data, htmlTemplate);

            File.WriteAllText(fileName, result, Encoding.UTF8);
            return(file);
        }
        /// <summary>
        /// 将Bytes导出为Csv文件
        /// </summary>
        /// <param name="bytes">字节数组</param>
        /// <param name="fileName">文件路径</param>
        /// <returns></returns>
        public static ExportFileInfo ToCsvExportFileInfo(this byte[] bytes, string fileName)
        {
            fileName.CheckCsvFileName();
            File.WriteAllBytes(fileName, bytes);

            var file = new ExportFileInfo(fileName,
                                          "text/csv");

            return(file);
        }
Esempio n. 10
0
        /// <summary>
        /// 将Bytes导出为Excel文件
        /// </summary>
        /// <param name="bytes">字节数组</param>
        /// <param name="fileName">文件路径</param>
        /// <returns></returns>
        public static ExportFileInfo ToExcelExportFileInfo(this byte[] bytes, string fileName)
        {
            fileName.CheckExcelFileName();
            File.WriteAllBytes(fileName, bytes);

            var file = new ExportFileInfo(fileName,
                                          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            return(file);
        }
Esempio n. 11
0
        /// <summary>
        ///     创建Excel
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="creator"></param>
        /// <returns></returns>
        public static ExportFileInfo CreateExcelPackage(string fileName, Action <ExcelPackage> creator)
        {
            var file = new ExportFileInfo(fileName,
                                          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            using (var excelPackage = new ExcelPackage())
            {
                creator(excelPackage);
                Save(excelPackage, file);
            }

            return(file);
        }
Esempio n. 12
0
        private async Task <ExportFileInfo> CreateNewFileAndUpdateMappings(string resourceType, int fileSequence, CancellationToken cancellationToken)
        {
            string fileName;

            if (_exportJobRecord.SchemaVersion == 1)
            {
                fileName = $"{_exportJobRecord.ExportFormat}.ndjson";
            }
            else
            {
                fileName = $"{_exportJobRecord.ExportFormat}-{fileSequence}.ndjson";
            }

            string dateTime = _exportJobRecord.QueuedTime.UtcDateTime.ToString("s")
                              .Replace("-", string.Empty, StringComparison.OrdinalIgnoreCase)
                              .Replace(":", string.Empty, StringComparison.OrdinalIgnoreCase);

            fileName = fileName.Replace(ExportFormatTags.Timestamp, dateTime, StringComparison.OrdinalIgnoreCase);
            fileName = fileName.Replace(ExportFormatTags.Id, _exportJobRecord.Id, StringComparison.OrdinalIgnoreCase);
            fileName = fileName.Replace(ExportFormatTags.ResourceName, resourceType, StringComparison.OrdinalIgnoreCase);

            Uri fileUri = await _exportDestinationClient.CreateFileAsync(fileName, cancellationToken);

            var newFile = new ExportFileInfo(resourceType, fileUri, sequence: fileSequence);

            // Since we created a new file the ExportJobRecord Output also needs to be updated.
            if (_exportJobRecord.Output.TryGetValue(resourceType, out List <ExportFileInfo> fileList))
            {
                fileList.Add(newFile);
            }
            else
            {
                _exportJobRecord.Output.Add(resourceType, new List <ExportFileInfo>()
                {
                    newFile
                });
            }

            // Update internal mapping with new file for the resource type.
            if (_resourceTypeToFileInfoMapping.ContainsKey(resourceType))
            {
                _resourceTypeToFileInfoMapping[resourceType] = newFile;
            }
            else
            {
                _resourceTypeToFileInfoMapping.Add(resourceType, newFile);
            }

            return(newFile);
        }
Esempio n. 13
0
        /// <summary>
        ///     获取文档转换配置
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="pdfExporterAttribute"></param>
        /// <param name="htmlString"></param>
        /// <returns></returns>
        private async Task <ExportFileInfo> ExportPdf(string fileName,
                                                      PdfExporterAttribute pdfExporterAttribute,
                                                      string htmlString)
        {
            var result = await ExportPdf(pdfExporterAttribute, htmlString);

#if NETSTANDARD2_1
            await File.WriteAllBytesAsync(fileName, result);
#else
            File.WriteAllBytes(fileName, result);
#endif

            var fileInfo = new ExportFileInfo(fileName, "application/pdf");
            return(await Task.FromResult(fileInfo));
        }
Esempio n. 14
0
        /// <summary>
        /// 导出HTML文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        /// <param name="htmlTemplate"></param>
        /// <returns></returns>
        public async Task <ExportFileInfo> ExportByTemplate <T>(string fileName, T data,
                                                                string htmlTemplate) where T : class
        {
            //var file = new ExportFileInfo(fileName, "text/html");
            //var result = await ExportByTemplate(data, htmlTemplate);

            //File.WriteAllText(fileName, result, Encoding.UTF8);
            //return file;
            // var fileName = "E:\\Project\\Person\\OpenSource\\Magicodes.IE\\src\\Magicodes.ExporterAndImporter.Tests\\bin\\Debug\\net461\\ExportHtmlBytesByTemplate_Test.html";
            var file   = new ExportFileInfo(fileName, "text/html");
            var result = await ExportByTemplate(data, htmlTemplate);

            File.WriteAllText(fileName, result, Encoding.UTF8);
            return(file);
        }
Esempio n. 15
0
        /// <summary>
        ///     根据模板导出
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        /// <param name="htmlTemplate"></param>
        /// <returns></returns>
        public async Task <ExportFileInfo> ExportByTemplate <T>(string fileName, T data, string htmlTemplate)
            where T : class
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException(Resource.FileNameMustBeFilled, nameof(fileName));
            }
            var result = await ExportBytesByTemplate(data, htmlTemplate);

            File.WriteAllBytes(fileName, result);
            var fileInfo = new ExportFileInfo(fileName,
                                              "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

            return(fileInfo);
        }
Esempio n. 16
0
        /// <summary>Creates a package manifest.</summary>
        /// <param name="exportJob">The export job.</param>
        /// <param name="exportFileInfo">The export file info.</param>
        /// <param name="summary">The summary.</param>
        public void CreatePackageManifest(ExportImportJob exportJob, ExportFileInfo exportFileInfo, ImportExportSummary summary)
        {
            var filePath    = Path.Combine(ExportFolder, exportJob.Directory, Constants.ExportManifestName);
            var portal      = PortalController.Instance.GetPortal(exportJob.PortalId);
            var packageInfo = new ImportPackageInfo
            {
                Summary     = summary,
                PackageId   = exportJob.Directory,
                Name        = exportJob.Name,
                Description = exportJob.Description,
                ExporTime   = exportJob.CreatedOnDate,
                PortalName  = portal?.PortalName,
            };

            Util.WriteJson(filePath, packageInfo);
        }
Esempio n. 17
0
        public async Task GivenMultipleFilesOutOfOrderForResourceTypeInOutput_WhenWriteToFile_ThenWritesToLatestFile()
        {
            string         resourceType = "Patient";
            ExportFileInfo file1        = new ExportFileInfo(resourceType, new Uri("https://localhost/Patient-1.ndjson"), sequence: 1);
            ExportFileInfo file2        = new ExportFileInfo(resourceType, new Uri("https://localhost/Patient-2.ndjson"), sequence: 2);
            ExportFileInfo file3        = new ExportFileInfo(resourceType, new Uri("https://localhost/Patient-3.ndjson"), sequence: 3);

            _exportJobRecord.Output.Add(resourceType, new List <ExportFileInfo>()
            {
                file2, file3, file1
            });

            await _exportFileManager.WriteToFile(resourceType, "partId", new byte[] { 1 }, _cancellationTokenNone);

            await _exportDestinationClient.Received(1).OpenFileAsync(Arg.Is(file3.FileUri), Arg.Is(_cancellationTokenNone));
        }
Esempio n. 18
0
        /// <summary>
        /// List导出到Excel
        /// </summary>
        /// <param name="allRows"></param>
        /// <param name="excelFileCount"></param>
        /// <param name="exportFileInfo"></param>
        /// <param name="list"></param>
        /// <param name="token"></param>
        /// <param name="exportProgressAction"></param>
        /// <param name="exportFileCompletedAction"></param>
        public void ExportListToExcel(int allRows, int excelFileCount, ExportFileInfo exportFileInfo, List <T> list, CancellationToken token, Action <int> exportProgressAction, Action <string> exportFileCompletedAction)
        {
            int sheetCount = allRows / Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT + (allRows % Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT == 0 ? 0 : 1);

            int currectListOccupancyRows = list.Count; //当前表的剩余记录数
            int dispId = 0;

            for (int fileIndex = 1; fileIndex <= excelFileCount; fileIndex++)
            {
                string filePath = ExcelFileHelper.GetExportFileName(exportFileInfo.ExcelTitle, exportFileInfo.SaveDirectory, fileIndex, excelFileCount);

                ListWriteExcelFile(allRows, sheetCount, exportFileInfo, filePath, list, token, ref currectListOccupancyRows, ref dispId, exportProgressAction);
                if (exportFileCompletedAction != null)
                {
                    exportFileCompletedAction.Invoke(filePath);
                }
            }
        }
Esempio n. 19
0
    //文件夹打包
    private static void SetAllDForF(string path)
    {
        string[] allFiles = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);

        string folderPath = ConvertPath(path);

        foreach (var filePath in allFiles)
        {
            string tmpFilePath = ConvertPath(filePath);

            if (!_allExportFileDict.ContainsKey(tmpFilePath))
            {
                _allExportFileDict[tmpFilePath] = new ExportFileInfo(tmpFilePath, true);
            }
            _allExportFileDict[tmpFilePath].isURLF       = true;
            _allExportFileDict[tmpFilePath].realPackPath = folderPath;
        }
    }
Esempio n. 20
0
    /// <summary>
    /// 将该文件夹内的所有文件,都打成一个包
    /// </summary>
    /// <param name="path"></param>
    private static void SetAllDForA(string path)
    {
        string[] allFiles = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

        foreach (var filePath in allFiles)
        {
            string tmpFilePath = ConvertPath(filePath);

            string reslutPath = GetFileABPath(tmpFilePath);

            if (!_allExportFileDict.ContainsKey(tmpFilePath))
            {
                _allExportFileDict[tmpFilePath] = new ExportFileInfo(tmpFilePath, true);
            }
            _allExportFileDict[tmpFilePath].isURLA       = true;
            _allExportFileDict[tmpFilePath].URLA         = reslutPath;
            _allExportFileDict[tmpFilePath].realPackPath = reslutPath;
        }
    }
Esempio n. 21
0
        private async Task ProcessSearchResultsAsync(IEnumerable <SearchResultEntry> searchResults, string partId, CancellationToken cancellationToken)
        {
            foreach (SearchResultEntry result in searchResults)
            {
                ResourceWrapper resourceWrapper = result.Resource;

                string resourceType = resourceWrapper.ResourceTypeName;

                // Check whether we already have an existing file for the current resource type.
                if (!_resourceTypeToFileInfoMapping.TryGetValue(resourceType, out ExportFileInfo exportFileInfo))
                {
                    // Check whether we have seen this file previously (in situations where we are resuming an export)
                    if (_exportJobRecord.Output.TryGetValue(resourceType, out exportFileInfo))
                    {
                        // A file already exists for this resource type. Let us open the file on the client.
                        await _exportDestinationClient.OpenFileAsync(exportFileInfo.FileUri, cancellationToken);
                    }
                    else
                    {
                        // File does not exist. Create it.
                        string fileName = resourceType + ".ndjson";
                        Uri    fileUri  = await _exportDestinationClient.CreateFileAsync(fileName, cancellationToken);

                        exportFileInfo = new ExportFileInfo(resourceType, fileUri, sequence: 0);

                        // Since we created a new file the JobRecord Output also needs to know about it.
                        _exportJobRecord.Output.TryAdd(resourceType, exportFileInfo);
                    }

                    _resourceTypeToFileInfoMapping.Add(resourceType, exportFileInfo);
                }

                // Serialize into NDJson and write to the file.
                byte[] bytesToWrite = _resourceToByteArraySerializer.Serialize(resourceWrapper);

                await _exportDestinationClient.WriteFilePartAsync(exportFileInfo.FileUri, partId, bytesToWrite, cancellationToken);

                // Increment the file information.
                exportFileInfo.IncrementCount(bytesToWrite.Length);
            }
        }
Esempio n. 22
0
        /// <summary>
        ///     根据模板导出
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        /// <param name="htmlTemplate"></param>
        /// <returns></returns>
        public async Task <ExportFileInfo> ExportByTemplate <T>(string fileName, T data, string htmlTemplate)
            where T : class
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException("文件名必须填写!", nameof(fileName));
            }

            var exporterAttribute = GetExporterAttribute <T>();
            var exporter          = new HtmlExporter();
            var htmlString        = await exporter.ExportByTemplate(data, htmlTemplate);

            if (exporterAttribute.IsWriteHtml)
            {
                File.WriteAllText(fileName + ".html", htmlString);
            }

            var doc = GetHtmlToPdfDocumentByExporterAttribute(fileName, exporterAttribute, htmlString);

            PdfConverter.Convert(doc);
            var fileInfo = new ExportFileInfo(fileName, "application/pdf");

            return(fileInfo);
        }
Esempio n. 23
0
        private async Task ProcessResourceWrapperAsync(ResourceWrapper resourceWrapper, uint partId, CancellationToken cancellationToken)
        {
            string resourceType = resourceWrapper.ResourceTypeName;

            // Check whether we already have an existing file for the current resource type.
            if (!_resourceTypeToFileInfoMapping.TryGetValue(resourceType, out ExportFileInfo exportFileInfo))
            {
                string fileName = resourceType + ".ndjson";

                Uri fileUri = await _exportDestinationClient.CreateFileAsync(fileName, cancellationToken);

                exportFileInfo = new ExportFileInfo(resourceType, fileUri, sequence: 0);

                _resourceTypeToFileInfoMapping.Add(resourceType, exportFileInfo);
            }

            // Serialize into NDJson and write to the file.
            byte[] bytesToWrite = _resourceToByteArraySerializer.Serialize(resourceWrapper);

            await _exportDestinationClient.WriteFilePartAsync(exportFileInfo.FileUri, partId, bytesToWrite, cancellationToken);

            // Increment the file information.
            exportFileInfo.IncrementCount(bytesToWrite.Length);
        }
Esempio n. 24
0
        public async Task WriteToFile(string resourceType, string partId, byte[] data, CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNullOrWhiteSpace(resourceType, nameof(resourceType));

            if (!_isInitialized)
            {
                await Initialize(cancellationToken);
            }

            ExportFileInfo fileInfo = await GetFile(resourceType, cancellationToken);

            // We need to create a new file if the current file has exceeded a certain limit.
            // File size limits are valid only for schema versions starting from 2.
            if (_exportJobRecord.SchemaVersion > 1 &&
                _approxMaxFileSizeInBytes > 0 &&
                fileInfo.CommittedBytes >= _approxMaxFileSizeInBytes)
            {
                fileInfo = await CreateNewFileAndUpdateMappings(resourceType, fileInfo.Sequence + 1, cancellationToken);
            }

            await _exportDestinationClient.WriteFilePartAsync(fileInfo.FileUri, partId, data, cancellationToken);

            fileInfo.IncrementCount(data.Length);
        }
Esempio n. 25
0
        /// <summary>Runs the export job.</summary>
        /// <param name="exportJob">The export job.</param>
        /// <param name="result">The result.</param>
        /// <param name="scheduleHistoryItem">The schedule history item.</param>
        public void Export(ExportImportJob exportJob, ExportImportResult result, ScheduleHistoryItem scheduleHistoryItem)
        {
            var exportDto = JsonConvert.DeserializeObject <ExportDto>(exportJob.JobObject);

            if (exportDto == null)
            {
                exportJob.CompletedOnDate = DateUtils.GetDatabaseUtcTime();
                exportJob.JobStatus       = JobStatus.Failed;
                return;
            }

            this.timeoutSeconds = GetTimeoutPerSlot();
            var dbName = Path.Combine(ExportFolder, exportJob.Directory, Constants.ExportDbName);
            var finfo  = new FileInfo(dbName);

            dbName = finfo.FullName;

            var checkpoints = EntitiesController.Instance.GetJobChekpoints(exportJob.JobId);

            // Delete so we start a fresh export database; only if there is no previous checkpoint exists
            if (checkpoints.Count == 0)
            {
                if (finfo.Directory != null && finfo.Directory.Exists)
                {
                    finfo.Directory.Delete(true);
                }

                // Clear all the files in finfo.Directory. Create if doesn't exists.
                finfo.Directory?.Create();
                result.AddSummary("Starting Exporting Repository", finfo.Name);
            }
            else
            {
                if (finfo.Directory != null && finfo.Directory.Exists)
                {
                    result.AddSummary("Resuming Exporting Repository", finfo.Name);
                }
                else
                {
                    scheduleHistoryItem.AddLogNote("Resuming data not found.");
                    result.AddSummary("Resuming data not found.", finfo.Name);
                    return;
                }
            }

            exportJob.JobStatus = JobStatus.InProgress;

            // there must be one parent implementor at least for this to work
            var implementors   = Util.GetPortableImplementors().ToList();
            var parentServices = implementors.Where(imp => string.IsNullOrEmpty(imp.ParentCategory)).ToList();

            implementors = implementors.Except(parentServices).ToList();
            var nextLevelServices = new List <BasePortableService>();
            var includedItems     = GetAllCategoriesToInclude(exportDto, implementors);

            if (includedItems.Count == 0)
            {
                scheduleHistoryItem.AddLogNote("Export NOT Possible");
                scheduleHistoryItem.AddLogNote("<br/>No items selected for exporting");
                result.AddSummary("Export NOT Possible", "No items selected for exporting");
                exportJob.CompletedOnDate = DateUtils.GetDatabaseUtcTime();
                exportJob.JobStatus       = JobStatus.Failed;
                return;
            }

            scheduleHistoryItem.AddLogNote($"<br/><b>SITE EXPORT Preparing Check Points. JOB #{exportJob.JobId}: {exportJob.Name}</b>");
            this.PrepareCheckPoints(exportJob.JobId, parentServices, implementors, includedItems, checkpoints);

            scheduleHistoryItem.AddLogNote($"<br/><b>SITE EXPORT Started. JOB #{exportJob.JobId}: {exportJob.Name}</b>");
            scheduleHistoryItem.AddLogNote($"<br/>Between [{exportDto.FromDateUtc ?? Constants.MinDbTime}] and [{exportDto.ToDateUtc:g}]");
            var firstIteration = true;

            AddJobToCache(exportJob);

            using (var ctx = new ExportImportRepository(dbName))
            {
                ctx.AddSingleItem(exportDto);
                do
                {
                    foreach (var service in parentServices.OrderBy(x => x.Priority))
                    {
                        if (exportJob.IsCancelled)
                        {
                            exportJob.JobStatus = JobStatus.Cancelled;
                            break;
                        }

                        if (implementors.Count > 0)
                        {
                            // collect children for next iteration
                            var children =
                                implementors.Where(imp => service.Category.Equals(imp.ParentCategory, IgnoreCaseComp));
                            nextLevelServices.AddRange(children);
                            implementors = implementors.Except(nextLevelServices).ToList();
                        }

                        if ((firstIteration && includedItems.Any(x => x.Equals(service.Category, IgnoreCaseComp))) ||
                            (!firstIteration && includedItems.Any(x => x.Equals(service.ParentCategory, IgnoreCaseComp))))
                        {
                            var serviceAssembly = service.GetType().Assembly.GetName().Name;
                            service.Result                  = result;
                            service.Repository              = ctx;
                            service.CheckCancelled          = CheckCancelledCallBack;
                            service.CheckPointStageCallback = this.CheckpointCallback;
                            service.CheckPoint              = checkpoints.FirstOrDefault(cp => cp.Category == service.Category && cp.AssemblyName == serviceAssembly);

                            if (service.CheckPoint == null)
                            {
                                service.CheckPoint = new ExportImportChekpoint
                                {
                                    JobId        = exportJob.JobId,
                                    Category     = service.Category,
                                    AssemblyName = serviceAssembly,
                                    StartDate    = DateUtils.GetDatabaseUtcTime(),
                                };

                                // persist the record in db
                                this.CheckpointCallback(service);
                            }
                            else if (service.CheckPoint.StartDate == Null.NullDate)
                            {
                                service.CheckPoint.StartDate = DateUtils.GetDatabaseUtcTime();
                            }

                            try
                            {
                                service.ExportData(exportJob, exportDto);
                            }
                            finally
                            {
                                this.AddLogsToDatabase(exportJob.JobId, result.CompleteLog);
                            }

                            scheduleHistoryItem.AddLogNote("<br/>Exported: " + service.Category);
                        }
                    }

                    firstIteration = false;
                    parentServices = new List <BasePortableService>(nextLevelServices);
                    nextLevelServices.Clear();
                    if (implementors.Count > 0 && parentServices.Count == 0)
                    {
                        // WARN: this is a case where there is a broken parent-children hierarchy
                        //      and/or there are BasePortableService implementations without a known parent.
                        parentServices = implementors;
                        implementors.Clear();
                        scheduleHistoryItem.AddLogNote(
                            "<br/><b>Orphaned services:</b> " + string.Join(",", parentServices.Select(x => x.Category)));
                    }
                }while (parentServices.Count > 0 && !this.TimeIsUp);

                RemoveTokenFromCache(exportJob);
            }

            if (this.TimeIsUp)
            {
                result.AddSummary(
                    $"Job time slot ({this.timeoutSeconds} sec) expired",
                    "Job will resume in the next scheduler iteration");
            }
            else if (exportJob.JobStatus == JobStatus.InProgress)
            {
                // Create Export Summary for manifest file.
                var summary = new ImportExportSummary();
                using (var ctx = new ExportImportRepository(dbName))
                {
                    BaseController.BuildJobSummary(exportJob.Directory, ctx, summary);
                }

                DoPacking(exportJob, dbName);

                // Complete the job.
                exportJob.JobStatus = JobStatus.Successful;
                SetLastJobStartTime(scheduleHistoryItem.ScheduleID, exportJob.CreatedOnDate);

                var exportController = new ExportController();
                var exportFileInfo   = new ExportFileInfo
                {
                    ExportPath = exportJob.Directory,
                    ExportSize = Util.FormatSize(GetExportSize(Path.Combine(ExportFolder, exportJob.Directory))),
                };

                summary.ExportFileInfo = exportFileInfo;
                exportController.CreatePackageManifest(exportJob, exportFileInfo, summary);
            }
        }
Esempio n. 26
0
        private void ListWriteExcelFile(int allRows, int sheetCount, ExportFileInfo exportFileInfo, string filePath, List <T> list, CancellationToken token, ref int currectListOccupancyRows, ref int dispId, Action <int> exportProgressAction)
        {
            int currectSheetOccupancyRows = Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT;
            int sheetIdx          = 1;
            int currectExcelIdx   = 0; //当前excel操作行下标
            int currectExcelCount = 0;
            var workbook          = new HSSFWorkbook();

            try
            {
                var sheet = CreateSheet(workbook, sheetCount, sheetIdx, exportFileInfo.ExcelTitle, out currectExcelIdx);

                int singleExcelMaxCount = Math.Min(Pub.DEFAULT_EXCEL_MAXCOUNT_FORINFO, allRows);
                while (currectExcelCount < singleExcelMaxCount)
                {
                    token.ThrowIfCancellationRequested();
                    int readerCount = Math.Min(Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT, currectListOccupancyRows);
                    readerCount = Math.Min(readerCount, currectSheetOccupancyRows);

                    if (readerCount > 0)
                    {
                        var readList   = list.GetRange(0, readerCount);
                        int writeCount = 0;
                        sheet = ListToSheet(workbook, sheet, readList, ref dispId, ref currectExcelIdx, exportProgressAction, token, out writeCount);
                        list.RemoveRange(0, readerCount);

                        singleExcelMaxCount      -= readerCount;
                        currectListOccupancyRows -= readerCount;

                        currectExcelCount += writeCount;
                    }

                    #region state update

                    if (currectListOccupancyRows <= 0 || readerCount == 0)
                    {
                        break;
                    }

                    if (singleExcelMaxCount >= Pub.DEFAULT_EXCEL_MAXCOUNT_FORINFO)
                    {
                        //Excel满了,退出当前Excel文件
                        break;
                    }

                    //切换Sheet
                    if ((singleExcelMaxCount > 0 && singleExcelMaxCount % Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT == 0) || (currectSheetOccupancyRows == 0))
                    {
                        if (sheetIdx == sheetCount)
                        {
                            break;
                        }

                        sheet.ForceFormulaRecalculation = true;
                        sheetIdx++;
                        sheet = CreateSheet(workbook, sheetCount, sheetIdx, exportFileInfo.ExcelTitle, out currectExcelIdx);
                        currectSheetOccupancyRows = Pub.DEFAULT_EXCEL_MAXCOUNT_FORINFO - singleExcelMaxCount;
                    }
                    #endregion
                }

                WriteWorkbookToFile(workbook, filePath);
            }
            finally
            {
                workbook.Clear();
                workbook = null;
                GC.Collect();
            }
        }
Esempio n. 27
0
    /// <summary>
    /// 打包核心代码
    /// </summary>
    public static void BuildCore(string packDir, string outputhPath, string versionPath)
    {
        //先删除文件夹的AB包
        DeleteFileOrFolder(outputhPath);
        _allExportFileDict.Clear();
        _tmpExportFileDict.Clear();
        _renewalFileList.Clear();

        bool isAllPack = false;

        if (packDir == _PackDir)
        {
            isAllPack = true;
        }

        //判断文件夹路径是否存在,不存在则创建
        if (!Directory.Exists(outputhPath))
        {
            Directory.CreateDirectory(outputhPath);
        }

        //遍历Resource的文件,全部资源都设置AB的文件名
        //找出所有依赖
        //过滤依赖, 分类,A,B,P.E,F
        //存入字典
        //目录下的所有文件和文件夹
        string[]      files         = Directory.GetFiles(packDir, "*.*", SearchOption.AllDirectories);
        string[]      directorys    = Directory.GetDirectories(packDir, "*", SearchOption.AllDirectories);
        List <string> directoryList = new List <string>();

        directoryList.Add(packDir);
        directoryList.AddRange(directorys);

        foreach (var path in files)
        {
            string ext = Path.GetExtension(path);
            if (!IsNoPack(ext) && !IsEPackPath(path))
            {
                string conv_path = ConvertPath(path);
                _allExportFileDict[conv_path] = new ExportFileInfo(conv_path, true);
                _tmpExportFileDict[conv_path] = new ExportFileInfo(conv_path, true);
            }
        }

        //查找Resource资源下的所有依赖

        //1.在resources目录下,如果依赖只有一个,且不是IsOutResourceExt的类型,则设置它info.dependUrl为空,表示该资源部打包,则引用它的父体和该资源合成一个ab包
        //例如:A、B都在resources目录下,只有A引用到了B,B的引用次数为1,则会把B的名称设为空,表示不打包。然后打包A的时候,就自动把B打包进A。
        //2.如果一个在resources目录下,一个在其他目录,大体原则上是合包,但是满足IsOutResourceExt条件,则分离包。

        foreach (var d in _tmpExportFileDict)
        {
            string         path     = d.Key;
            ExportFileInfo fileInfo = d.Value;
            if (!IsNoPack(fileInfo.ext))
            {
                string[] depends = AssetDatabase.GetDependencies(path);

                if (depends != null && depends.Length > 0)
                {
                    foreach (var depend in depends)
                    {
                        if (path != depend)
                        {
                            //如果包含了,即在resources目录下
                            if (_tmpExportFileDict.ContainsKey(depend))
                            {
                                _tmpExportFileDict[depend].AddReadPackTime();
                                _allExportFileDict[depend] = _tmpExportFileDict[depend];
                            }
                            else//不在resources目录下
                            {
                                //原则上是resourceslib里的都要跟resource合成一个包,除了IsOutResourceExt条件,满足IsOutResourceExt条件的则分离
                                if (!_allExportFileDict.ContainsKey(depend) && IsOutResourceExt(Path.GetExtension(depend)))
                                {
                                    ExportFileInfo info = new ExportFileInfo(depend, false);

                                    _allExportFileDict[depend] = info;
                                }
                            }
                        }
                    }
                }
            }
        }

        //过滤依赖
        foreach (var d in _allExportFileDict)
        {
            ExportFileInfo info = d.Value;
            if (info.readPackTime == 1 && !IsOutResourceExt(info.ext))
            {
                info.realPackPath = "";
            }
        }

        //设置AB名字
        //分类,A,B,P.E,F
        foreach (var direPath in directoryList)
        {
            string path = ConvertPath(direPath);
            if (IsTypeF(path))
            {
                SetAllDForF(direPath);
            }
            else if (IsTypeA(path))
            {
                SetAllDForA(direPath);
            }
        }

        //调用API,设置AB名字
        foreach (var d in _allExportFileDict)
        {
            ExportFileInfo info = d.Value;

            if (!IsNoPack(info.ext))//脚本不打包
            {
                if (info.realPackPath != "")
                {
                    string basePathGUID = AssetDatabase.AssetPathToGUID(info.realPackPath) + _AbExtName;

                    AssetImporter importer = AssetImporter.GetAtPath(info.path);
                    if (importer)
                    {
                        if (importer.assetBundleName != "" || basePathGUID != "")
                        {
                            importer.assetBundleName = basePathGUID;
                        }
                    }
                }
            }
        }

        //刷新
        AssetDatabase.Refresh();

        //是否强制重新打包输出
        bool isForceRebuild = false;

        //生成AB包

        BuildTarget target;

#if UNITY_STANDALONE_WIN
        target = BuildTarget.StandaloneWindows;
#elif UNITY_ANDROID
        target = BuildTarget.Android;
#elif UNITY_IPHONE
        target = BuildTarget.iPhone;
#else
        target = BuildTarget.StandaloneWindows;
#endif

        BuildAssetBundleOptions options = BuildAssetBundleOptions.UncompressedAssetBundle;

        //是否强制打包
        if (isForceRebuild)
        {
            BuildPipeline.BuildAssetBundles(outputhPath, options | BuildAssetBundleOptions.ForceRebuildAssetBundle, target);
        }
        else
        {
            BuildPipeline.BuildAssetBundles(outputhPath, options, target);
        }

        //_merger_source = "";
        //_merger_Mapping = "";
        //把散图打包到Resources下对应的图集预设(适用于UGUI)
        foreach (var d in _mergerDict)
        {
            SetSource2AtlasPrefab(Application.dataPath + "/" + d.Key, Application.dataPath + "/" + d.Value);
        }

        //刷新
        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
        Debug.Log("AB包打包成");

        //更新文件
        //设置路径和需要生成的带所有文件的更新文件
        //从abmanifest中获取hash值
        //更新文件的数据
        foreach (var d in _allExportFileDict)
        {
            ExportFileInfo info = d.Value;
            if (!IsNoPack(info.ext))
            {
                string base_GuidName = AssetDatabase.AssetPathToGUID(info.realPackPath) + _AbExtName;
                if (!_renewalFileList.ContainsKey(base_GuidName))
                {
                    RenewalFileInfoTmp renewal = new RenewalFileInfoTmp();
                    _renewalFileList[base_GuidName] = renewal;
                }
                _renewalFileList[base_GuidName].SetData(info.realPackPath, base_GuidName);
            }
        }

        //加载总的manifest文件
        AssetBundle ab = null;
        if (string.IsNullOrEmpty(_TargFol))
        {
            ab = AssetBundle.LoadFromFile(outputhPath + "StreamingAssets");
        }
        else
        {
            ab = AssetBundle.LoadFromFile(outputhPath + "/" + _TargFol);
        }

        if (ab != null)
        {
            AssetBundleManifest manifest = ab.LoadAsset <AssetBundleManifest>("assetBundlemanifest");

            string[] allAssetBundles = manifest.GetAllAssetBundles();

            for (int i = 0; i < allAssetBundles.Length; ++i)
            {
                string assetbundle = allAssetBundles[i];
                if (_renewalFileList.ContainsKey(assetbundle))
                {
                    RenewalFileInfoTmp renewal = _renewalFileList[assetbundle];
                    renewal.abHash = manifest.GetAssetBundleHash(assetbundle);
                }
                else
                {
                    //因为manifest文件是递增的添加信息,所以当分步打包的时候,manifest包含有以前的包信息,而_renewalFileList只包含当前分步的包信息
                    //即会出现不包含的情况,但是是正常的
                    //但是全部打包的时候,出现非法打包路径,就不正常
                    if (isAllPack)//是否是全部打包
                    {
                        Debug.Log(packDir + "非法打包路径" + assetbundle + ",请检查");
                    }
                }
            }

            //没有对应目录,则创建
            if (!Directory.Exists(versionPath))
            {
                Directory.CreateDirectory(versionPath);
            }

            //写入ab信息
            if (isAllPack || !File.Exists(versionPath + "/" + _VersionName))
            {
                string strBulid = "";
                strBulid += BulidRule(_TargFol, "0", _TargFol);
                //生成更新列表txt
                foreach (var d in _renewalFileList)
                {
                    RenewalFileInfoTmp renewal = d.Value;
                    strBulid += BulidRule(renewal.guidName, renewal.abHash.ToString(), renewal.realPackPath);
                }
                File.WriteAllText(versionPath + "/" + _VersionName, strBulid.ToString());

                string verPath = _TargDir;
                if (!string.IsNullOrEmpty(_TargFol))
                {
                    verPath = verPath + "/" + _TargFol;
                }
                File.WriteAllText(verPath + "/" + _VersionName, strBulid.ToString());
            }
            else
            {
                //不是全部打包且不存在文件信息,则追加信息
                Dictionary <string, RenewalFileInfoTmp> tmpDict = new Dictionary <string, RenewalFileInfoTmp>();
                foreach (var d in _renewalFileList)
                {
                    RenewalFileInfoTmp renewal = d.Value;

                    tmpDict[renewal.realPackPath] = renewal;
                }

                //读取原来的信息
                string       strBulid = "";
                StreamReader sr       = new StreamReader(versionPath + "/" + _VersionName, Encoding.UTF8);
                string       line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] abInfo    = line.Split(_ConfigRule2.ToCharArray());
                    string[] contrasts = abInfo[abInfo.Length - 1].Split(_ConfigRule1.ToCharArray());
                    string   contrast  = contrasts[contrasts.Length - 1];

                    if (tmpDict.ContainsKey(contrast))
                    {
                        RenewalFileInfoTmp renewal = tmpDict[contrast];
                        strBulid += BulidRule(renewal.guidName, renewal.abHash.ToString(), renewal.realPackPath);
                        tmpDict.Remove(contrast);
                    }
                    else
                    {
                        strBulid += line + Environment.NewLine;
                    }
                }
                sr.Close();

                foreach (var d in tmpDict)
                {
                    RenewalFileInfoTmp renewal = d.Value;
                    strBulid += BulidRule(renewal.guidName, renewal.abHash.ToString(), renewal.realPackPath);
                }

                File.WriteAllText(versionPath + "/" + _VersionName, strBulid.ToString());

                string verPath = _TargDir;
                if (!string.IsNullOrEmpty(_TargFol))
                {
                    verPath = verPath + "/" + _TargFol;
                }
                File.WriteAllText(verPath + "/" + _VersionName, strBulid.ToString());
            }

            //释放内存
            ab.Unload(true);
        }
        else
        {
            Debug.Log("没有assetBundleManifest文件");
        }

        if (_isDeleteManifest == "1")
        {
            //删除AB.Manifest
            string[] ab_files = Directory.GetFiles(outputhPath, "*.*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < ab_files.Length; ++i)
            {
                string path = ab_files[i].Replace(@"\", @"/");
                if (File.Exists(path))
                {
                    if (Path.GetExtension(path) == ".manifest" || path.Contains("manifest.meta"))
                    {
                        File.Delete(path);
                    }
                }
            }
        }

        Debug.Log("生成更新文件成功");

        //生成版本号文件
        File.WriteAllText(_VersionTimePath + "/" + _VersionTimeName, _Version);

        //压缩
        //......

        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
    }
Esempio n. 28
0
 private static void Save(ExcelPackage excelPackage, ExportFileInfo file)
 {
     excelPackage.SaveAs(new FileInfo(file.FileName));
 }
Esempio n. 29
0
        private void QueryDataReaderWriteExcel(MySqlConnection conn, int allRows, int sheetCount, ExportFileInfo exportFileInfo, string filePath, CancellationToken token, string querySqlPart, List <string> tableNameList, List <int> tableCountList, string whereSqlPart, ref int tableIndex, ref int currectTableOccupancyRows, ref int currectTableStartRowIdx, ref int dispId, Action <int> exportProgressAction)
        {
            int currectSheetOccupancyRows = Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT;
            int sheetIdx          = 1; //sheet下标
            int currectExcelIdx   = 0; //操作的excel操作行下标
            int currectExcelCount = 0;
            var workbook          = new HSSFWorkbook();

            try
            {
                var sheet = CreateSheet(workbook, sheetCount, sheetIdx, exportFileInfo.ExcelTitle, out currectExcelIdx);


                int singleExcelMaxCount = Math.Min(Pub.DEFAULT_EXCEL_MAXCOUNT_FORINFO, allRows);
                while (currectExcelCount < singleExcelMaxCount)
                {
                    token.ThrowIfCancellationRequested();
                    int readerCount = Math.Min(Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT, currectSheetOccupancyRows);
                    readerCount = Math.Min(readerCount, currectTableOccupancyRows);

                    if (readerCount > 0)
                    {
                        string commandText = CreateQuerySqlStrAction(querySqlPart, tableNameList[tableIndex], whereSqlPart, currectTableStartRowIdx, readerCount);

                        int writeCount;
                        sheet = DataReaderToSheet(workbook, sheet, conn, commandText, ref dispId, ref currectExcelIdx, exportProgressAction, token, out writeCount);

                        singleExcelMaxCount       -= readerCount;
                        currectTableOccupancyRows -= readerCount;

                        currectTableStartRowIdx += writeCount;
                        currectExcelCount       += writeCount;
                    }

                    #region state update

                    if (currectTableOccupancyRows <= 0 || readerCount == 0)
                    {
                        //切换table
                        currectTableStartRowIdx = 0;
                        if (tableIndex == tableCountList.Count - 1)
                        {
                            break;
                        }
                        currectTableOccupancyRows = tableCountList[++tableIndex];
                    }

                    if (singleExcelMaxCount >= Pub.DEFAULT_EXCEL_MAXCOUNT_FORINFO)
                    {
                        //Excel满了,退出当前Excel文件
                        break;
                    }

                    //切换Sheet
                    if ((singleExcelMaxCount > 0 && singleExcelMaxCount % Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT == 0) || (currectSheetOccupancyRows == 0))
                    {
                        if (sheetIdx == sheetCount)
                        {
                            break;
                        }

                        sheet.ForceFormulaRecalculation = true;
                        sheetIdx++;
                        sheet = CreateSheet(workbook, sheetCount, sheetIdx, exportFileInfo.ExcelTitle, out currectExcelIdx);
                        currectSheetOccupancyRows = Pub.DEFAULT_EXCEL_MAXCOUNT_FORINFO - singleExcelMaxCount;
                    }
                    #endregion
                }

                WriteWorkbookToFile(workbook, filePath);
            }
            finally
            {
                workbook.Clear();
                workbook = null;
                GC.Collect();
            }
        }
Esempio n. 30
0
        /// <summary>
        /// 消息消费者:具体导出操作
        /// </summary>
        /// <param name="ieDto"></param>
        /// <returns></returns>
        public async Task <string> ExcelExport(ExcelIEDto ieDto)
        {
            string downLoadUrl = string.Empty, excelFilePath = string.Empty;
            var    dataTable = new DataTable();
            var    fileInfo  = new ExportFileInfo();
            //获取配置信息
            var sysConfig = ConfigHelper.GetValue <SysConfig>();
            var ossConfig = ConfigHelper.GetValue <OssConfig>();

            ieDto.TaskWatch.Start();
            try
            {
                //切换数据库连接
                await _dbService.ChangeConnectionString(ieDto);

                _logger.LogInformation("开始导入!");
                #region 保存路径和模板路径初始化和处理
                var root      = Directory.GetCurrentDirectory() + "\\";
                var rootPath  = root + ExcelIEConsts.ExcelIESufStr;
                var fileName  = ieDto.Template.TemplateName + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ExcelIEConsts.ExcelSufStr;
                var excelPath = rootPath + ExcelIEConsts.ExportSufStr;
                excelFilePath = excelPath + fileName;

                //模板文件路径
                var excelTemplatePath = rootPath + ExcelIEConsts.TemplateSufStr + ieDto.Template.TemplateName + ExcelIEConsts.ExcelSufStr;
                //拷备模板路径
                var excelTemplateCopyPath = rootPath + ExcelIEConsts.TemplateSufStr + fileName;
                //导出前删除文件
                if (File.Exists(excelFilePath))
                {
                    File.Delete(excelFilePath);
                }
                //创建文件夹
                if (!Directory.Exists(excelPath))
                {
                    Directory.CreateDirectory(excelPath);
                }
                #endregion

                #region 导出记录数据收集
                var templateLog = await _excelIEDomainService.GetFirstExcelLogModelAsync(o => o.Id == ieDto.TemplateLog.Id);

                if (templateLog.Id != Guid.Empty)
                {
                    ieDto.TemplateLog = templateLog;
                }
                ieDto.TemplateLog.ExecCount++;
                if (ieDto.TemplateLog.ExecCount >= 3)
                {
                    ieDto.TemplateLog.Status = 2;
                }
                #endregion

                //构造导出Sql语句
                GetExportSql(ieDto);

                //收集导出数据
                ieDto.QueryWatch.Start();
                dataTable = await GetDataBySql(ieDto, new DataTable());

                ieDto.QueryWatch.Stop();
                #region 导出excel数据
                //默认为0Magicodes.IE插件(分sheet导出:默认10000)
                if (ieDto.Template.TemplateType > 0)
                {
                    ieDto.ExportType = (ExportTypeEnum)ieDto.Template.TemplateType;
                }
                if (ieDto.ExportType == ExportTypeEnum.MagicodesCommon)
                {
                    //格式DataTable表头
                    FormatterHead(ieDto, dataTable, true);
                    //导出数据
                    ieDto.WriteWatch.Start();
                    fileInfo = await _iExcelExporter.Export(excelFilePath, dataTable, maxRowNumberOnASheet : ieDto.Template.ExecMaxCountPer);

                    ieDto.WriteWatch.Stop();
                }
                //模板导出自定义表头:支持图片
                else if (ieDto.ExportType == ExportTypeEnum.MagicodesImgByTemplate)
                {
                    //格式DataTable表头
                    FormatterHead(ieDto, dataTable);
                    var jarray = JArray.FromObject(dataTable);
                    if (!ieDto.ExportObj.ContainsKey("DataList"))
                    {
                        ieDto.ExportObj.Add(new JProperty("DataList", jarray));
                    }

                    //复制模板,解决资源共享占用问题
                    if (File.Exists(excelTemplatePath))                            //必须判断要复制的文件是否存在
                    {
                        File.Copy(excelTemplatePath, excelTemplateCopyPath, true); //三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换
                    }
                    //导出数据
                    ieDto.WriteWatch.Start();
                    fileInfo = await _iExcelExporter.ExportByTemplate <JObject>(excelFilePath, ieDto.ExportObj, excelTemplateCopyPath);

                    ieDto.WriteWatch.Stop();
                    //删除拷备模板文件
                    if (File.Exists(excelTemplateCopyPath))
                    {
                        File.Delete(excelTemplateCopyPath);
                    }
                }
                //MiniExcel导出
                else if (ieDto.ExportType == ExportTypeEnum.MiniExcelCommon)
                {
                    //格式DataTable表头
                    FormatterHead(ieDto, dataTable, true);
                    //导出数据
                    ieDto.WriteWatch.Start();
                    MiniExcel.SaveAs(excelFilePath, dataTable);
                    ieDto.WriteWatch.Stop();
                }
                //判断是本地还是远程部署
                if (sysConfig.ExcelEDownLoad.DeployType == 0)
                {
                    downLoadUrl = ieDto.LocalUrl + ExcelIEConsts.ExcelIEDownUrlSuf;
                }
                else
                {
                    string ossFilePathName = string.Format(@"{0}{1}",
                                                           string.IsNullOrEmpty(sysConfig.ExcelEDownLoad.UrlSuf) ? (ExcelIEConsts.ExcelIEDownUrlSuf + ieDto.TenantCode + "/") : sysConfig.ExcelEDownLoad.UrlSuf,
                                                           fileName);
                    downLoadUrl = string.Format(@"https://{0}.{1}/{2}",
                                                ossConfig.BucketName, ossConfig.Endpoint, string.IsNullOrEmpty(sysConfig.ExcelEDownLoad.UrlSuf) ? (ExcelIEConsts.ExcelIEDownUrlSuf + ieDto.TenantCode + "/") : sysConfig.ExcelEDownLoad.UrlSuf);
                    var ossResult = _oss.PutObject(ossConfig.BucketName, ossFilePathName, excelFilePath);
                    if (File.Exists(excelFilePath))
                    {
                        File.Delete(excelFilePath);
                    }
                }
                #endregion

                #region 导出记录数据收集保存
                ieDto.TemplateLog.FileSize    = CountSize(GetFileSize(excelFilePath));
                ieDto.TemplateLog.Status      = 1;
                ieDto.TemplateLog.FileName    = fileName;
                ieDto.TemplateLog.DownLoadUrl = downLoadUrl;
                #endregion

                _logger.LogInformation("导入成功!");
            }
            catch (Exception ex)
            {
                ieDto.TemplateLog.Status    = 2;
                ieDto.TemplateLog.ExportMsg = "导出失败:" + ex.Message + ":";
                _logger.LogError("导入失败:" + ex.Message);
                throw;
            }
            finally
            {
                ieDto.TemplateLog.ModifyTime  = DateTime.Now;
                ieDto.TemplateLog.ModifyUser  = ieDto.UserName;
                ieDto.TemplateLog.ExportCount = dataTable.Rows.Count;
                if (sysConfig.ExcelEDownLoad.DeployType != 0 && File.Exists(excelFilePath))
                {
                    File.Delete(excelFilePath);
                }
                ieDto.TaskWatch.Stop();
                ieDto.TemplateLog.ExportDurationTask  = Convert.ToDecimal(ieDto.TaskWatch.Elapsed.TotalSeconds);
                ieDto.TemplateLog.ExportDurationQuery = Convert.ToDecimal(ieDto.QueryWatch.Elapsed.TotalSeconds);
                ieDto.TemplateLog.ExportDurationWrite = Convert.ToDecimal(ieDto.WriteWatch.Elapsed.TotalSeconds);
                if (ieDto.TemplateLog.Status == 1)
                {
                    ieDto.TemplateLog.ExportMsg = "导出成功:" + ieDto.TaskWatch.Elapsed.TotalSeconds.ToString("0.00") + "秒";
                }
                if (ieDto.TemplateLog.Status == 2)
                {
                    ieDto.TemplateLog.ExportMsg += ieDto.TaskWatch.Elapsed.TotalSeconds.ToString("0.00") + "秒";
                }
                await _excelIEDomainService.EditAsyncExcelLogModel(ieDto.TemplateLog, true);
            }
            return(string.Empty);
        }