Skip to content

Commit

Permalink
fix xls export
Browse files Browse the repository at this point in the history
  • Loading branch information
mukunku committed Aug 13, 2022
1 parent f647878 commit 6235c00
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/ParquetFileViewer/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public enum DateFormat
ISO8601_Alt1,
ISO8601_Alt2
}

public enum FileType
{
CSV = 0,
XLS
}
}
10 changes: 9 additions & 1 deletion src/ParquetFileViewer/Helpers/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Data;

namespace ParquetFileViewer.Helpers
Expand Down Expand Up @@ -54,5 +55,12 @@ public static IList<string> 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))
};
}
}
47 changes: 32 additions & 15 deletions src/ParquetFileViewer/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public MainForm()
pi.SetValue(this.mainGridView, true, null);
}


}

public MainForm(string fileToOpenPath) : this()
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 6235c00

Please sign in to comment.