public FileInfo WriteHtmlToTempFolder(IProgress <string> progress)
        {
            var possibleFileName = FolderFileUtility.TryMakeFilenameValid(ContentTitle);

            var possibleFile = new FileInfo(Path.Combine(UserSettingsUtilities.TempStorageDirectory().FullName,
                                                         $"HistoricEntries-{possibleFileName}-{DateTime.Now:yyyy-MM-dd---HH-mm-ss}.htm"));

            progress?.Report($"Writing File - {possibleFile.FullName}");

            File.WriteAllText(possibleFile.FullName, GenerateHtml(progress));

            possibleFile.Refresh();

            return(possibleFile);
        }
        public static void ContentToExcelFileAsTable(List <object> toDisplay, string fileName)
        {
            var file = new FileInfo(Path.Combine(UserSettingsUtilities.TempStorageDirectory().FullName,
                                                 $"PhotoMetadata-{FolderFileUtility.TryMakeFilenameValid(fileName)}-{DateTime.Now:yyyy-MM-dd---HH-mm-ss}.xlsx"));

            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("PW Data");

            ws.Cell(1, 1).InsertTable(toDisplay);
            wb.SaveAs(file.FullName);

            var ps = new ProcessStartInfo(file.FullName)
            {
                UseShellExecute = true, Verb = "open"
            };

            Process.Start(ps);
        }
Ejemplo n.º 3
0
        public static FileInfo ContentToExcelFileAsTable(List <object> toDisplay, string fileName,
                                                         bool openAfterSaving = true, bool limitRowHeight = true, IProgress <string> progress = null)
        {
            progress?.Report($"Starting transfer of {toDisplay.Count} to Excel");

            var file = new FileInfo(Path.Combine(UserSettingsUtilities.TempStorageDirectory().FullName,
                                                 $"{DateTime.Now:yyyy-MM-dd--HH-mm-ss}---{FolderFileUtility.TryMakeFilenameValid(fileName)}.xlsx"));

            progress?.Report($"File Name: {file.FullName}");

            progress?.Report("Creating Workbook");

            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("Exported Data");

            progress?.Report("Inserting Data");

            ws.Cell(1, 1).InsertTable(toDisplay);

            progress?.Report("Applying Formatting");

            ws.Columns().AdjustToContents();

            foreach (var loopColumn in ws.ColumnsUsed().Where(x => x.Width > 70))
            {
                loopColumn.Width = 70;
                loopColumn.Style.Alignment.WrapText = true;
            }

            ws.Rows().AdjustToContents();

            if (limitRowHeight)
            {
                foreach (var loopRow in ws.RowsUsed().Where(x => x.Height > 70))
                {
                    loopRow.Height = 70;
                }
            }

            progress?.Report($"Saving Excel File {file.FullName}");

            wb.SaveAs(file.FullName);

            if (openAfterSaving)
            {
                progress?.Report($"Opening Excel File {file.FullName}");

                var ps = new ProcessStartInfo(file.FullName)
                {
                    UseShellExecute = true, Verb = "open"
                };
                Process.Start(ps);
            }

            return(file);
        }
Ejemplo n.º 4
0
        public static FileInfo PointContentToExcel(List <PointContentDto> toDisplay, string fileName,
                                                   bool openAfterSaving = true, IProgress <string> progress = null)
        {
            if (toDisplay == null || !toDisplay.Any())
            {
                return(null);
            }

            progress?.Report("Setting up list to transfer to Excel");

            var transformedList = toDisplay.Select(x => new PointContent().InjectFrom(x)).Cast <PointContent>().ToList();

            var detailList = new List <(Guid, string)>();

            foreach (var loopContent in toDisplay)
            {
                progress?.Report($"Processing {loopContent.Title} with {loopContent.PointDetails.Count} details");
                // ! This content format is used by ExcelContentImports !
                // Push the content into a compromise format that is ok for human generation (the target here is not creating 'by
                //  hand in Excel' rather taking something like GNIS data and concatenating/text manipulating the data into
                //  shape) and still ok for parsing in code
                foreach (var loopDetail in loopContent.PointDetails)
                {
                    detailList.Add((loopContent.ContentId,
                                    $"ContentId:{loopDetail.ContentId}||{Environment.NewLine}Type:{loopDetail.DataType}||{Environment.NewLine}Data:{loopDetail.StructuredDataAsJson}"));
                }
            }

            var file = new FileInfo(Path.Combine(UserSettingsUtilities.TempStorageDirectory().FullName,
                                                 $"{DateTime.Now:yyyy-MM-dd--HH-mm-ss}---{FolderFileUtility.TryMakeFilenameValid(fileName)}.xlsx"));

            progress?.Report($"File Name {file.FullName} - creating Excel Workbook");

            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("Exported Data");

            progress?.Report("Inserting Content Data");

            var insertedTable = ws.Cell(1, 1).InsertTable(transformedList);

            progress?.Report("Adding Detail Columns...");

            var contentIdColumn = insertedTable.Row(1).Cells().Single(x => x.GetString() == "ContentId")
                                  .WorksheetColumn().ColumnNumber();

            //Create columns to the right of the existing table to hold the Point Details and expand the table
            var neededDetailColumns = detailList.GroupBy(x => x.Item1).Max(x => x.Count());

            var firstDetailColumn = insertedTable.Columns().Last().WorksheetColumn().ColumnNumber() + 1;

            for (var i = firstDetailColumn; i < firstDetailColumn + neededDetailColumns; i++)
            {
                ws.Cell(1, i).Value = $"PointDetail {i - firstDetailColumn + 1}";
            }

            if (neededDetailColumns > 0)
            {
                insertedTable.Resize(ws.RangeUsed());
            }

            //Match in the point details (match rather than assume list/excel ordering)
            foreach (var loopRow in insertedTable.Rows().Skip(1))
            {
                var rowContentId = Guid.Parse(loopRow.Cell(contentIdColumn).GetString());
                var matchedData  = detailList.Where(x => x.Item1 == rowContentId);

                var currentColumn = firstDetailColumn;

                foreach (var loopDetail in matchedData)
                {
                    loopRow.Cell(currentColumn).Value = loopDetail.Item2;
                    currentColumn++;
                }
            }

            progress?.Report("Applying Formatting");

            //Format
            ws.Columns().AdjustToContents();

            foreach (var loopColumn in ws.ColumnsUsed().Where(x => x.Width > 70))
            {
                loopColumn.Width = 70;
                loopColumn.Style.Alignment.WrapText = true;
            }

            ws.Rows().AdjustToContents();

            foreach (var loopRow in ws.RowsUsed().Where(x => x.Height > 100))
            {
                loopRow.Height = 100;
            }

            progress?.Report($"Saving Excel File {file.FullName}");

            wb.SaveAs(file.FullName);

            if (openAfterSaving)
            {
                progress?.Report($"Opening Excel File {file.FullName}");

                var ps = new ProcessStartInfo(file.FullName)
                {
                    UseShellExecute = true, Verb = "open"
                };
                Process.Start(ps);
            }

            return(file);
        }