public async Task CsvHelperAsync()
        {
            TextWriter tw    = TextWriter.Null;
            var        items = TestData.GetTestObjects();
            var        csv   = new CsvHelper.CsvWriter(tw, new CsvConfiguration(CultureInfo.InvariantCulture));

            csv.WriteField("Id");
            csv.WriteField("Name");
            csv.WriteField("Date");
            csv.WriteField("IsActive");
            for (int i = 0; i < ValueCount; i++)
            {
                csv.WriteField("Value" + i);
            }
            await csv.NextRecordAsync();

            foreach (var item in items)
            {
                csv.WriteField(item.Id);
                csv.WriteField(item.Name);
                csv.WriteField(item.Date);
                csv.WriteField(item.IsActive);
                for (int i = 0; i < ValueCount; i++)
                {
                    csv.WriteField(item.DataSet[i]);
                }
                await csv.NextRecordAsync();
            }
            await csv.FlushAsync();
        }
Example #2
0
        public async Task <byte[]> ToCsvAsync()
        {
            using var memoryStream = new System.IO.MemoryStream();
            using var writer       = new System.IO.StreamWriter(memoryStream);
            using var csv          = new CsvHelper.CsvWriter(writer,
                                                             new CsvConfiguration(CultureInfo.InvariantCulture));

            await csv
            .WriteRecordsAsync(await _schoolService.GetForExportAsync());

            await csv.FlushAsync();

            await writer.FlushAsync();

            await memoryStream.FlushAsync();

            return(memoryStream.ToArray());
        }
        public async Task <byte[]> ToCsvAsync()
        {
            var branches = await _siteService.GetAllBranches();

            using var memoryStream = new System.IO.MemoryStream();
            using var writer       = new System.IO.StreamWriter(memoryStream);
            using var csv          = new CsvHelper.CsvWriter(writer,
                                                             new CsvConfiguration(CultureInfo.InvariantCulture));
            csv.Context.RegisterClassMap <Maps.BranchMap>();

            await csv.WriteRecordsAsync(branches.OrderBy(_ => _.SystemName).ThenBy(_ => _.Name));

            await csv.FlushAsync();

            await writer.FlushAsync();

            await memoryStream.FlushAsync();

            return(memoryStream.ToArray());
        }
Example #4
0
        /// <summary>
        /// Uploads book cover images to ipfs
        /// Writes a mapping csv file to relate ean, image size, ipfs hash etc
        /// Assumes the book cover images are held locally so....
        /// Ensure the cover images have been downloaded first! (DownloadCoversAsync)
        /// </summary>
        /// <param name="books"></param>
        /// <returns></returns>
        public static async Task UploadCoversToIpfsAsync(
            IEnumerable <Book> books, string coverFolder, string indexOutputFilePath, string ipfsUrl)
        {
            ipfsFilesLoaded = 0;
            //indexOutputFilePath

            try
            {
                // we'll parallel load images - but don't want to write to the csv in parallel
                var semaphore = new SemaphoreSlim(1);

                using (var textWriter = File.CreateText(indexOutputFilePath))
                {
                    var csvWriter = new CsvHelper.CsvWriter(textWriter, CultureInfo.InvariantCulture);
                    csvWriter.WriteHeader <IpfsBookCoverMapping>();
                    csvWriter.NextRecord();

                    try
                    {
                        var ipfsService = new IpfsService(ipfsUrl);
                        foreach (var book in books.AsParallel())
                        {
                            if (!string.IsNullOrWhiteSpace(book.COVER_SMALL))
                            {
                                var smallCover = await UploadCoverToIpfs(coverFolder, book.EAN, "s", book.COVER_SMALL, ipfsService);
                                await Save(smallCover);
                            }
                            if (!string.IsNullOrWhiteSpace(book.COVER_MED))
                            {
                                var mediumCover = await UploadCoverToIpfs(coverFolder, book.EAN, "m", book.COVER_MED, ipfsService);
                                await Save(mediumCover);
                            }
                            if (!string.IsNullOrWhiteSpace(book.COVER_LARGE))
                            {
                                var largeCover = await UploadCoverToIpfs(coverFolder, book.EAN, "l", book.COVER_LARGE, ipfsService);
                                await Save(largeCover);
                            }

                            async Task Save(IpfsBookCoverMapping mapping)
                            {
                                await semaphore.WaitAsync();

                                try
                                {
                                    csvWriter.WriteRecord(mapping);
                                    csvWriter.NextRecord();
                                }
                                finally
                                {
                                    semaphore.Release();
                                }
                            }
                        }
                    }
                    finally
                    {
                        await csvWriter.FlushAsync();

                        await textWriter.FlushAsync();
                    }
                }
            }
            finally
            {
                // consider changing file name on completion to avoid it being overwritten accidentally
            }
        }