【发布时间】:2015-07-19 14:25:22
【问题描述】:
我使用 openfiledialog 并对其进行过滤以获取: .xls 和 .xlsx 和 .xlsm 文件。 但我不知道接下来要做什么,我用 firstName 和 lastName 构建了一个工人类,我想从 excel 文件中获取数据并将其放入变量中。
这是我的 openfiledialog 代码:
private void ExcelLoad_Click(object sender, EventArgs e)
{
int size = -1;
openFileDialog1.Title = "Browse Excel file";
openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
}
Console.WriteLine(size); // <-- Shows file size in debugging mode.
Console.WriteLine(result); // <-- For debugging use.
Stream excelOpenFile= openFileDialog1.OpenFile();
}
那么我怎样才能从这种文件中读取数据。(成功打开它但我不知道如何使用该文件并从中获取数据)。
【问题讨论】:
-
在这里提问前需要先做研究。谷歌“在 C# 中打开 excel 文件”
-
Reading Excel files from C# 的可能重复项