Skip to content

Commit 6235c00

Browse files
committed
fix xls export
1 parent f647878 commit 6235c00

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

src/ParquetFileViewer/Constants.cs

+6
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ public enum DateFormat
2020
ISO8601_Alt1,
2121
ISO8601_Alt2
2222
}
23+
24+
public enum FileType
25+
{
26+
CSV = 0,
27+
XLS
28+
}
2329
}

src/ParquetFileViewer/Helpers/ExtensionMethods.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Data;
34

45
namespace ParquetFileViewer.Helpers
@@ -54,5 +55,12 @@ public static IList<string> GetColumnNames(this DataTable datatable)
5455
DateFormat.Default_DateOnly => true,
5556
_ => false
5657
};
58+
59+
public static string GetExtension(this FileType fileType) => fileType switch
60+
{
61+
FileType.CSV => ".csv",
62+
FileType.XLS => ".xls",
63+
_ => throw new ArgumentOutOfRangeException(nameof(fileType))
64+
};
5765
}
5866
}

src/ParquetFileViewer/MainForm.cs

+32-15
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public MainForm()
156156
pi.SetValue(this.mainGridView, true, null);
157157
}
158158

159-
159+
160160
}
161161

162162
public MainForm(string fileToOpenPath) : this()
@@ -394,14 +394,14 @@ private void MainGridView_CellPainting(object sender, DataGridViewCellPaintingEv
394394
Rectangle r1 = new Rectangle(e.CellBounds.Left + e.CellBounds.Width - img.Width, 4, img.Width, img.Height);
395395
Rectangle r2 = new Rectangle(0, 0, img.Width, img.Height);
396396
string header = ((DataGridView)sender).Columns[e.ColumnIndex].HeaderText;
397-
e.PaintBackground(e.CellBounds, true);
397+
e.PaintBackground(e.CellBounds, true);
398398
e.PaintContent(e.CellBounds);
399399
e.Graphics.DrawImage(img, r1, r2, GraphicsUnit.Pixel);
400400

401401
e.Handled = true;
402402
}
403403
}
404-
else if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
404+
else if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
405405
{
406406
if (e.Value == null || e.Value == DBNull.Value)
407407
{
@@ -833,17 +833,36 @@ private void WriteDataToCSVFile(string path, BackgroundWorker worker, DoWorkEven
833833

834834
private void WriteDataToExcelFile(string path, BackgroundWorker worker, DoWorkEventArgs e)
835835
{
836+
string dateFormat = AppSettings.DateTimeDisplayFormat.GetDateFormat();
836837
using var fs = new FileStream(path, FileMode.OpenOrCreate);
837838
var excelWriter = new ExcelWriter(fs);
838839
excelWriter.BeginWrite();
839840

841+
//Write headers
842+
for (int i = 0; i < this.MainDataSource.Columns.Count; i++)
843+
{
844+
excelWriter.WriteCell(0, i, this.MainDataSource.Columns[i].ColumnName);
845+
}
846+
847+
//Write data
840848
for (int i = 0; i < this.MainDataSource.DefaultView.Count; i++)
841849
{
842850
for (int j = 0; j < this.MainDataSource.Columns.Count; j++)
843851
{
844-
excelWriter.WriteCell(i, j, this.mainDataSource.DefaultView[i][j]?.ToString() ?? string.Empty);
852+
var value = this.mainDataSource.DefaultView[i][j];
853+
854+
if (value is DateTime dt)
855+
{
856+
excelWriter.WriteCell(i + 1, j, dt.ToString(dateFormat));
857+
}
858+
else
859+
{
860+
excelWriter.WriteCell(i + 1, j, value?.ToString() ?? string.Empty);
861+
}
845862
}
846863
}
864+
865+
excelWriter.EndWrite();
847866
}
848867

849868
private void ExportFileBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
@@ -864,19 +883,21 @@ private void ExportFileBackgroundWorker_RunWorkerCompleted(object sender, RunWor
864883
}
865884
}
866885

867-
private void ExportResults(FileType fileType)
886+
private void ExportResults(FileType defaultFileType)
868887
{
869888
if (this.mainGridView.RowCount > 0)
870889
{
871890
this.exportFileDialog.Title = string.Format("{0} records will be exported", this.mainGridView.RowCount);
872891
this.exportFileDialog.Filter = "CSV file (*.csv)|*.csv|Excel file (*.xls)|*.xls";
873-
this.exportFileDialog.FilterIndex = (int)fileType + 1;
892+
this.exportFileDialog.FilterIndex = (int)defaultFileType + 1;
874893
if (this.exportFileDialog.ShowDialog() == DialogResult.OK)
875894
{
895+
string filePath = this.exportFileDialog.FileName;
896+
var selectedFileType = Path.GetExtension(filePath).Equals(FileType.XLS.GetExtension()) ? FileType.XLS : FileType.CSV;
876897
var args = new ExportToFileArgs()
877898
{
878-
FilePath = this.exportFileDialog.FileName,
879-
FileType = fileType
899+
FilePath = filePath,
900+
FileType = selectedFileType
880901
};
881902
this.ExportFileBackgroundWorker.RunWorkerAsync(args);
882903
this.ShowLoadingIcon("Exporting Data", this.ExportFileBackgroundWorker);
@@ -904,11 +925,7 @@ public ParquetReadResult(DataTable result, long totalNumberOfRecordsInFile)
904925
}
905926
}
906927

907-
private enum FileType
908-
{
909-
CSV = 0, //should match Filter Index in the exportFileDialog control's Filter property
910-
XLS = 1
911-
}
928+
912929

913930
private struct ExportToFileArgs
914931
{
@@ -960,7 +977,7 @@ private void DateFormatMenuItem_Click(object sender, EventArgs e)
960977
AppSettings.DateTimeDisplayFormat = selectedDateFormat;
961978
this.RefreshDateFormatMenuItemSelection();
962979
this.MainDataSource = this.MainDataSource; //Will cause a refresh of the date formats
963-
}
980+
}
964981
}
965982
catch (Exception ex)
966983
{
@@ -1091,7 +1108,7 @@ private void mainGridView_CellMouseEnter(object sender, DataGridViewCellEventArg
10911108
if (isDateTimeCell && isUserUsingDateOnlyFormat)
10921109
{
10931110
var relativeMousePosition = this.PointToClient(Cursor.Position);
1094-
this.dateOnlyFormatWarningToolTip.Show($"Date only format enabled. To see time values: Edit -> Date Format",
1111+
this.dateOnlyFormatWarningToolTip.Show($"Date only format enabled. To see time values: Edit -> Date Format",
10951112
this, relativeMousePosition, 10000);
10961113
}
10971114
}

0 commit comments

Comments
 (0)