-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathForm1.cs
More file actions
103 lines (86 loc) · 3.02 KB
/
Copy pathForm1.cs
File metadata and controls
103 lines (86 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// <auto-generated/>
using System.Data;
using System.Diagnostics;
namespace ExcelDataReader.Sample;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/*
public static void GetValues(DataSet dataset, string sheetName)
{
foreach (DataRow row in dataset.Tables[sheetName].Rows)
{
foreach (var value in row.ItemArray)
{
Console.WriteLine("{0}, {1}", value, value.GetType());
}
}
}
*/
private static IList<string> GetTablenames(DataTableCollection tables)
{
var tableList = new List<string>(tables.Count);
foreach (var table in tables)
{
tableList.Add(table.ToString());
}
return tableList;
}
private void Button1Click(object sender, EventArgs e)
{
var result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
private void Button2Click(object sender, EventArgs e)
{
try
{
using var stream = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var sw = new Stopwatch();
sw.Start();
using IExcelDataReader reader = string.Equals(Path.GetExtension(textBox1.Text), ".csv", StringComparison.OrdinalIgnoreCase) ?
ExcelReaderFactory.CreateCsvReader(stream) : ExcelReaderFactory.CreateReader(stream, new()
{
SinglePassMode = singlePassModeCheckBox.Checked,
});
var openTiming = sw.ElapsedMilliseconds;
// reader.IsFirstRowAsColumnNames = firstRowNamesCheckBox.Checked;
ds = reader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = false,
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{
UseHeaderRow = firstRowNamesCheckBox.Checked,
FillMergedCellsValue = fillMergeCellCheckBox.Checked
}
});
toolStripStatusLabel1.Text = "Elapsed: " + sw.ElapsedMilliseconds.ToString() + " ms (" + openTiming.ToString() + " ms to open)";
var tablenames = GetTablenames(ds.Tables);
sheetCombo.DataSource = tablenames;
if (tablenames.Count > 0)
sheetCombo.SelectedIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SelectTable()
{
var tablename = sheetCombo.SelectedItem.ToString();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds; // dataset
dataGridView1.DataMember = tablename;
// GetValues(ds, tablename);
}
private void SheetComboSelectedIndexChanged(object sender, EventArgs e)
{
SelectTable();
}
}