Skip to content

Commit 190b3cd

Browse files
committed
Added Ability to choose tabs in Excel files
1 parent de26c50 commit 190b3cd

File tree

14 files changed

+189
-34
lines changed

14 files changed

+189
-34
lines changed

src/UniqueFileRecordsComparer/UniqueFileRecordsComparer.App/ComparerArguments.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ public class ComparerArguments
44
{
55
public string SourceFilePath { get; set; }
66
public string TargetFilePath { get; set; }
7+
8+
public int SourceFileTabIndex { get; set; }
9+
public int TargetFileTabIndex { get; set; }
710
}
811
}

src/UniqueFileRecordsComparer/UniqueFileRecordsComparer.App/SelectColumnsForm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public SelectColumnsForm(ComparerArguments comparerArguments)
2222
var targetReader =
2323
FileReaderFactory.CreateFromFileName(new FileInfoWrapper(new FileInfo(comparerArguments.TargetFilePath)));
2424

25-
_sourceRowCollection = sourceReader.Read();
26-
_targetRowCollection = targetReader.Read();
25+
_sourceRowCollection = sourceReader.Read(comparerArguments.SourceFileTabIndex);
26+
_targetRowCollection = targetReader.Read(comparerArguments.TargetFileTabIndex);
2727

2828
AddColumnsToCheckList(SourceColumnsCheckList, _sourceRowCollection);
2929
AddColumnsToCheckList(TargetColumnsCheckList, _targetRowCollection);

src/UniqueFileRecordsComparer/UniqueFileRecordsComparer.App/SelectFilesForm.Designer.cs

Lines changed: 76 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/UniqueFileRecordsComparer/UniqueFileRecordsComparer.App/SelectFilesForm.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System;
2+
using System.IO;
3+
using System.IO.Abstractions;
4+
using System.Linq;
25
using System.Windows.Forms;
6+
using UniqueFileRecordsComparer.Core.Readers;
37

48
namespace UniqueFileRecordsComparer.App
59
{
@@ -17,12 +21,33 @@ private void ChooseSourceFileButton_Click(object sender, EventArgs e)
1721
{
1822
_comparerArguments.SourceFilePath = GetFilePathFromDialog();
1923
SourceFilePathLabel.Text = _comparerArguments.SourceFilePath;
24+
25+
FillTabs(SourceFileTabsDropDown, _comparerArguments.SourceFilePath);
2026
}
2127

2228
private void ChooseTargetFileButton_Click(object sender, EventArgs e)
2329
{
2430
_comparerArguments.TargetFilePath = GetFilePathFromDialog();
2531
TargetFilePathLabel.Text = _comparerArguments.TargetFilePath;
32+
33+
FillTabs(TargetFileTabsDropDown, _comparerArguments.TargetFilePath);
34+
}
35+
36+
private void FillTabs(ComboBox fileTabsDropDown, string path)
37+
{
38+
fileTabsDropDown.Items.Clear();
39+
fileTabsDropDown.Enabled = false;
40+
41+
var fileReader = FileReaderFactory.CreateFromFileName(new FileInfoWrapper(new FileInfo(path)));
42+
if (fileReader.GetTabNamesByIndex().Keys.Count > 1)
43+
{
44+
foreach (var tab in fileReader.GetTabNamesByIndex())
45+
{
46+
fileTabsDropDown.Items.Insert(tab.Key, tab.Value);
47+
}
48+
fileTabsDropDown.Enabled = true;
49+
fileTabsDropDown.SelectedIndex = 0;
50+
}
2651
}
2752

2853
private static string GetFilePathFromDialog()
@@ -34,7 +59,7 @@ private static string GetFilePathFromDialog()
3459
dialog.Filter = @"Data file |*.csv;*.xlsx";
3560

3661
var result = dialog.ShowDialog();
37-
62+
3863
if(result == DialogResult.OK)
3964
return dialog.FileName;
4065
}
@@ -45,6 +70,9 @@ private void NextButton_Click(object sender, EventArgs e)
4570
{
4671
if (IsFormValid())
4772
{
73+
_comparerArguments.SourceFileTabIndex = SourceFileTabsDropDown.SelectedIndex;
74+
_comparerArguments.TargetFileTabIndex = TargetFileTabsDropDown.SelectedIndex;
75+
4876
var selectColumnsForm = new SelectColumnsForm(_comparerArguments);
4977
selectColumnsForm.Show();
5078
Hide();
@@ -57,7 +85,7 @@ private void NextButton_Click(object sender, EventArgs e)
5785

5886
private bool IsFormValid()
5987
{
60-
return !string.IsNullOrEmpty(_comparerArguments.SourceFilePath) &&
88+
return !string.IsNullOrEmpty(_comparerArguments.SourceFilePath) &&
6189
!string.IsNullOrEmpty(_comparerArguments.TargetFilePath);
6290
}
6391
}

src/UniqueFileRecordsComparer/UniqueFileRecordsComparer.Core/Readers/CsvReader.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.IO.Abstractions;
@@ -17,12 +18,17 @@ public CsvReader(FileInfoBase fileInfo)
1718
_fileInfo = fileInfo;
1819
}
1920

20-
public RowCollection Read()
21+
public RowCollection Read(int? tabIndex)
2122
{
2223
IList<Row> rows = ReadCsv().ToList();
2324
return new RowCollection(rows);
2425
}
2526

27+
public IDictionary<int, string> GetTabNamesByIndex()
28+
{
29+
return new Dictionary<int, string>();
30+
}
31+
2632
private IEnumerable<Row> ReadCsv()
2733
{
2834
using (var reader = CreateCsvReader())

src/UniqueFileRecordsComparer/UniqueFileRecordsComparer.Core/Readers/ExcelReader.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,41 @@ public ExcelReader(FileInfoBase fileInfo)
1616
_fileInfo = fileInfo;
1717
}
1818

19-
public RowCollection Read()
19+
public RowCollection Read(int? tabIndex)
2020
{
21-
return new RowCollection(ExtractDataSet().ToList());
21+
return new RowCollection(ExtractDataSet(tabIndex ?? 0).ToList());
2222
}
2323

24-
private IEnumerable<Row> ExtractDataSet()
24+
public IDictionary<int, string> GetTabNamesByIndex()
25+
{
26+
var dictionary = new Dictionary<int, string>();
27+
int index = 0;
28+
using (var excelReader = ExcelReaderFactory.CreateOpenXmlReader(_fileInfo.OpenRead()))
29+
{
30+
foreach (DataTable table in excelReader.AsDataSet().Tables)
31+
{
32+
dictionary.Add(index++, table.TableName);
33+
}
34+
}
35+
36+
return dictionary;
37+
}
38+
39+
private IEnumerable<Row> ExtractDataSet(int tabIndex)
2540
{
2641
using (var excelReader = ExcelReaderFactory.CreateOpenXmlReader(_fileInfo.Open(FileMode.Open, FileAccess.Read)))
2742
{
2843
excelReader.IsFirstRowAsColumnNames = true;
29-
using (var result = excelReader.AsDataSet())
44+
using (var table = excelReader.AsDataSet().Tables[tabIndex])
3045
{
31-
return GetRows(result);
46+
return GetRows(table);
3247
}
3348
}
3449
}
3550

36-
private static IEnumerable<Row> GetRows(DataSet result)
51+
private IEnumerable<Row> GetRows(DataTable table)
3752
{
38-
var dataTable = result.Tables[0];
39-
40-
return dataTable
53+
return table
4154
.Rows
4255
.Cast<DataRow>()
4356
.Select(GetRow);
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
using System.Collections.Generic;
2+
13
namespace UniqueFileRecordsComparer.Core.Readers
24
{
35
public interface IFileReader
46
{
5-
RowCollection Read();
7+
RowCollection Read(int? tabIndex);
8+
IDictionary<int, string> GetTabNamesByIndex();
69
}
710
}
37.4 KB
Binary file not shown.

src/UniqueFileRecordsComparer/tests/UniqueFileRecordsComparer.Core.IntegrationTests/CompareTwoFieldWithOneFieldTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static RowCollection ReadRowCollection(string path)
6565
{
6666
var reader = FileReaderFactory.CreateFromFileName(new FileInfoWrapper(new FileInfo(path)));
6767

68-
return reader.Read();
68+
return reader.Read(0);
6969
}
7070

7171
private static void CheckFilesExist()

src/UniqueFileRecordsComparer/tests/UniqueFileRecordsComparer.Core.IntegrationTests/ReaderTests/When_reading_csv_file.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void Then_returns_the_expected_result()
6767
}
6868
};
6969

70-
var result = FileReaderFactory.CreateFromFileName(new FileInfoWrapper(new FileInfo(CsvFilePath))).Read();
70+
var result = FileReaderFactory.CreateFromFileName(new FileInfoWrapper(new FileInfo(CsvFilePath))).Read(0);
7171

7272
result.ShouldAllBeEquivalentTo(expectedResult);
7373
}

0 commit comments

Comments
 (0)