我正在尝试使用以下代码从Excel文件中读取列的值:
FileStream stream = File.Open("excelfile.xlsx", FileMode.Open, FileAccess.Read);
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
Console.WriteLine("Results: " +excelReader.ResultsCount);
while (excelReader.Read())
{
object[] values = new object[excelReader.FieldCount];
excelReader.GetValues(values);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
它如何设法例外:
“System.NotSupportedException未处理Message =”Ométodo especificadonãoésuportado。“Source =”Excel“StackTrace: 在Excel.ExcelOpenXmlReader.GetValues(Object [] values)
中
我正在使用另一种方式来读取值,但我想稍微清理一下代码...... 这是上一个阅读周期:
FileStream stream = File.Open("excelfile.xlsx", FileMode.Open, FileAccess.Read);
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
Console.WriteLine("Results: " +excelReader.ResultsCount);
while (excelReader.Read())
{
Console.WriteLine("Row:"+excelReader.GetValues())
for (int i = 0; i < excelReader.FieldCount; i++ )
{
Console.Write(excelReader.GetValue(i)+ "|");
}
Console.WriteLine("");
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
我是否在使用getValues做错了什么?我以前用它来读取SQL记录......也许这是我正在使用的库的限制。
答案 0 :(得分:4)
这确实是一个实施问题。希望有足够的人会发现这个功能对Ian在将来的版本中实现它非常有用。
目前不支持IDataReader的这种特殊方法 ExcelDataReader。我会打开这个问题,所以我们可以看看是否 其他人也想要它
- 由Excel阅读器的Ian1971开发人员
似乎存在实施问题,因此我将使用第一种方法。
答案 1 :(得分:0)
此代码工作形成了我:
请考虑:我尝试ExcelDataReader
V2.1.2.3版本,使用V3.4.0之类的更新版本。遇到.IsFirstRowAsColumnNames
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel;
using System.IO;
namespace TEST3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet result;
private void btnOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Wprkbook 97-2003|*.xls| Excel Workbook|*.xlsx", ValidateNames = true })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
IExcelDataReader reader;
if (ofd.FilterIndex == 1)
reader = ExcelReaderFactory.CreateBinaryReader(fs);
else
reader = ExcelReaderFactory.CreateOpenXmlReader(fs);
reader.IsFirstRowAsColumnNames = true;
result = reader.AsDataSet();
cboSheet.Items.Clear();
foreach (DataTable dt in result.Tables)
cboSheet.Items.Add(dt.TableName);
reader.Close();
}
}
}
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void cbosheet_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView.DataSource = result.Tables[cboSheet.SelectedIndex];
}
}
}