diff --git a/src/ParquetFileViewer/Constants.cs b/src/ParquetFileViewer/Constants.cs index ab8b866..6fa8771 100644 --- a/src/ParquetFileViewer/Constants.cs +++ b/src/ParquetFileViewer/Constants.cs @@ -20,4 +20,10 @@ public enum DateFormat ISO8601_Alt1, ISO8601_Alt2 } + + public enum FileType + { + CSV = 0, + XLS + } } diff --git a/src/ParquetFileViewer/Helpers/ExtensionMethods.cs b/src/ParquetFileViewer/Helpers/ExtensionMethods.cs index fd16adc..f60f771 100644 --- a/src/ParquetFileViewer/Helpers/ExtensionMethods.cs +++ b/src/ParquetFileViewer/Helpers/ExtensionMethods.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Data; namespace ParquetFileViewer.Helpers @@ -54,5 +55,12 @@ public static IList GetColumnNames(this DataTable datatable) DateFormat.Default_DateOnly => true, _ => false }; + + public static string GetExtension(this FileType fileType) => fileType switch + { + FileType.CSV => ".csv", + FileType.XLS => ".xls", + _ => throw new ArgumentOutOfRangeException(nameof(fileType)) + }; } } diff --git a/src/ParquetFileViewer/MainForm.cs b/src/ParquetFileViewer/MainForm.cs index e3ab678..6e20b92 100644 --- a/src/ParquetFileViewer/MainForm.cs +++ b/src/ParquetFileViewer/MainForm.cs @@ -156,7 +156,7 @@ public MainForm() pi.SetValue(this.mainGridView, true, null); } - + } public MainForm(string fileToOpenPath) : this() @@ -394,14 +394,14 @@ private void MainGridView_CellPainting(object sender, DataGridViewCellPaintingEv Rectangle r1 = new Rectangle(e.CellBounds.Left + e.CellBounds.Width - img.Width, 4, img.Width, img.Height); Rectangle r2 = new Rectangle(0, 0, img.Width, img.Height); string header = ((DataGridView)sender).Columns[e.ColumnIndex].HeaderText; - e.PaintBackground(e.CellBounds, true); + e.PaintBackground(e.CellBounds, true); e.PaintContent(e.CellBounds); e.Graphics.DrawImage(img, r1, r2, GraphicsUnit.Pixel); e.Handled = true; } } - else if (e.RowIndex >= 0 && e.ColumnIndex >= 0) + else if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { if (e.Value == null || e.Value == DBNull.Value) { @@ -833,17 +833,36 @@ private void WriteDataToCSVFile(string path, BackgroundWorker worker, DoWorkEven private void WriteDataToExcelFile(string path, BackgroundWorker worker, DoWorkEventArgs e) { + string dateFormat = AppSettings.DateTimeDisplayFormat.GetDateFormat(); using var fs = new FileStream(path, FileMode.OpenOrCreate); var excelWriter = new ExcelWriter(fs); excelWriter.BeginWrite(); + //Write headers + for (int i = 0; i < this.MainDataSource.Columns.Count; i++) + { + excelWriter.WriteCell(0, i, this.MainDataSource.Columns[i].ColumnName); + } + + //Write data for (int i = 0; i < this.MainDataSource.DefaultView.Count; i++) { for (int j = 0; j < this.MainDataSource.Columns.Count; j++) { - excelWriter.WriteCell(i, j, this.mainDataSource.DefaultView[i][j]?.ToString() ?? string.Empty); + var value = this.mainDataSource.DefaultView[i][j]; + + if (value is DateTime dt) + { + excelWriter.WriteCell(i + 1, j, dt.ToString(dateFormat)); + } + else + { + excelWriter.WriteCell(i + 1, j, value?.ToString() ?? string.Empty); + } } } + + excelWriter.EndWrite(); } private void ExportFileBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) @@ -864,19 +883,21 @@ private void ExportFileBackgroundWorker_RunWorkerCompleted(object sender, RunWor } } - private void ExportResults(FileType fileType) + private void ExportResults(FileType defaultFileType) { if (this.mainGridView.RowCount > 0) { this.exportFileDialog.Title = string.Format("{0} records will be exported", this.mainGridView.RowCount); this.exportFileDialog.Filter = "CSV file (*.csv)|*.csv|Excel file (*.xls)|*.xls"; - this.exportFileDialog.FilterIndex = (int)fileType + 1; + this.exportFileDialog.FilterIndex = (int)defaultFileType + 1; if (this.exportFileDialog.ShowDialog() == DialogResult.OK) { + string filePath = this.exportFileDialog.FileName; + var selectedFileType = Path.GetExtension(filePath).Equals(FileType.XLS.GetExtension()) ? FileType.XLS : FileType.CSV; var args = new ExportToFileArgs() { - FilePath = this.exportFileDialog.FileName, - FileType = fileType + FilePath = filePath, + FileType = selectedFileType }; this.ExportFileBackgroundWorker.RunWorkerAsync(args); this.ShowLoadingIcon("Exporting Data", this.ExportFileBackgroundWorker); @@ -904,11 +925,7 @@ public ParquetReadResult(DataTable result, long totalNumberOfRecordsInFile) } } - private enum FileType - { - CSV = 0, //should match Filter Index in the exportFileDialog control's Filter property - XLS = 1 - } + private struct ExportToFileArgs { @@ -960,7 +977,7 @@ private void DateFormatMenuItem_Click(object sender, EventArgs e) AppSettings.DateTimeDisplayFormat = selectedDateFormat; this.RefreshDateFormatMenuItemSelection(); this.MainDataSource = this.MainDataSource; //Will cause a refresh of the date formats - } + } } catch (Exception ex) { @@ -1091,7 +1108,7 @@ private void mainGridView_CellMouseEnter(object sender, DataGridViewCellEventArg if (isDateTimeCell && isUserUsingDateOnlyFormat) { var relativeMousePosition = this.PointToClient(Cursor.Position); - this.dateOnlyFormatWarningToolTip.Show($"Date only format enabled. To see time values: Edit -> Date Format", + this.dateOnlyFormatWarningToolTip.Show($"Date only format enabled. To see time values: Edit -> Date Format", this, relativeMousePosition, 10000); } }