@@ -156,7 +156,7 @@ public MainForm()
156
156
pi . SetValue ( this . mainGridView , true , null ) ;
157
157
}
158
158
159
-
159
+
160
160
}
161
161
162
162
public MainForm ( string fileToOpenPath ) : this ( )
@@ -394,14 +394,14 @@ private void MainGridView_CellPainting(object sender, DataGridViewCellPaintingEv
394
394
Rectangle r1 = new Rectangle ( e . CellBounds . Left + e . CellBounds . Width - img . Width , 4 , img . Width , img . Height ) ;
395
395
Rectangle r2 = new Rectangle ( 0 , 0 , img . Width , img . Height ) ;
396
396
string header = ( ( DataGridView ) sender ) . Columns [ e . ColumnIndex ] . HeaderText ;
397
- e . PaintBackground ( e . CellBounds , true ) ;
397
+ e . PaintBackground ( e . CellBounds , true ) ;
398
398
e . PaintContent ( e . CellBounds ) ;
399
399
e . Graphics . DrawImage ( img , r1 , r2 , GraphicsUnit . Pixel ) ;
400
400
401
401
e . Handled = true ;
402
402
}
403
403
}
404
- else if ( e . RowIndex >= 0 && e . ColumnIndex >= 0 )
404
+ else if ( e . RowIndex >= 0 && e . ColumnIndex >= 0 )
405
405
{
406
406
if ( e . Value == null || e . Value == DBNull . Value )
407
407
{
@@ -833,17 +833,36 @@ private void WriteDataToCSVFile(string path, BackgroundWorker worker, DoWorkEven
833
833
834
834
private void WriteDataToExcelFile ( string path , BackgroundWorker worker , DoWorkEventArgs e )
835
835
{
836
+ string dateFormat = AppSettings . DateTimeDisplayFormat . GetDateFormat ( ) ;
836
837
using var fs = new FileStream ( path , FileMode . OpenOrCreate ) ;
837
838
var excelWriter = new ExcelWriter ( fs ) ;
838
839
excelWriter . BeginWrite ( ) ;
839
840
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
840
848
for ( int i = 0 ; i < this . MainDataSource . DefaultView . Count ; i ++ )
841
849
{
842
850
for ( int j = 0 ; j < this . MainDataSource . Columns . Count ; j ++ )
843
851
{
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
+ }
845
862
}
846
863
}
864
+
865
+ excelWriter . EndWrite ( ) ;
847
866
}
848
867
849
868
private void ExportFileBackgroundWorker_RunWorkerCompleted ( object sender , RunWorkerCompletedEventArgs e )
@@ -864,19 +883,21 @@ private void ExportFileBackgroundWorker_RunWorkerCompleted(object sender, RunWor
864
883
}
865
884
}
866
885
867
- private void ExportResults ( FileType fileType )
886
+ private void ExportResults ( FileType defaultFileType )
868
887
{
869
888
if ( this . mainGridView . RowCount > 0 )
870
889
{
871
890
this . exportFileDialog . Title = string . Format ( "{0} records will be exported" , this . mainGridView . RowCount ) ;
872
891
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 ;
874
893
if ( this . exportFileDialog . ShowDialog ( ) == DialogResult . OK )
875
894
{
895
+ string filePath = this . exportFileDialog . FileName ;
896
+ var selectedFileType = Path . GetExtension ( filePath ) . Equals ( FileType . XLS . GetExtension ( ) ) ? FileType . XLS : FileType . CSV ;
876
897
var args = new ExportToFileArgs ( )
877
898
{
878
- FilePath = this . exportFileDialog . FileName ,
879
- FileType = fileType
899
+ FilePath = filePath ,
900
+ FileType = selectedFileType
880
901
} ;
881
902
this . ExportFileBackgroundWorker . RunWorkerAsync ( args ) ;
882
903
this . ShowLoadingIcon ( "Exporting Data" , this . ExportFileBackgroundWorker ) ;
@@ -904,11 +925,7 @@ public ParquetReadResult(DataTable result, long totalNumberOfRecordsInFile)
904
925
}
905
926
}
906
927
907
- private enum FileType
908
- {
909
- CSV = 0 , //should match Filter Index in the exportFileDialog control's Filter property
910
- XLS = 1
911
- }
928
+
912
929
913
930
private struct ExportToFileArgs
914
931
{
@@ -960,7 +977,7 @@ private void DateFormatMenuItem_Click(object sender, EventArgs e)
960
977
AppSettings . DateTimeDisplayFormat = selectedDateFormat ;
961
978
this . RefreshDateFormatMenuItemSelection ( ) ;
962
979
this . MainDataSource = this . MainDataSource ; //Will cause a refresh of the date formats
963
- }
980
+ }
964
981
}
965
982
catch ( Exception ex )
966
983
{
@@ -1091,7 +1108,7 @@ private void mainGridView_CellMouseEnter(object sender, DataGridViewCellEventArg
1091
1108
if ( isDateTimeCell && isUserUsingDateOnlyFormat )
1092
1109
{
1093
1110
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",
1095
1112
this , relativeMousePosition , 10000 ) ;
1096
1113
}
1097
1114
}
0 commit comments