如何在C#中读取excel列并设置数据表? [英] How to read excel column and set datatable in C#?
本文介绍了如何在C#中读取excel列并设置数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我选择了excel文件。但是我没有读取excel列值而且我没有设置datatable.Excel列相同的datagrid列值。
如何读取excel文件和设置数据网格。
另外我接受此错误。为什么?
//此错误是DataTable是microsoft.Office.Interop.Excel.DataTable和System.Data.DataTable之间的ambigues参考
我尝试过:
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
命名空间MyWpfProject
{
类VMInsertFile
{
public ContactBtnClickExcelCommand {
get {return contactBtnClickExcelCommand; }
set {contactBtnClickExcelCommand = value; OnPropertyChanged();
}
public InsertOpenDialog {
DataTable dt = new DataTable(); //这个错误是DataTable是microsoft.Office.Interop.Excel.DataTable和System.Data.DataTable之间的ambigues引用
string con =
@Provider = Microsoft.Jet.OLEDB.4.0; Data Source = {0}+
@扩展属性='Excel 8.0; HDR =是;';
使用(OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand(select * from [Sheet1 $],connection);
使用(OleDbDataReader dr = command.ExecuteReader())
{
while(dr.Read())
{
var row1Col0 = dr [0];
Console.WriteLine(row1Col0);
}
}
}
解决方案
,连接);
使用( OleDbDataReader dr = command.ExecuteReader())
{
while(dr.Read())
{
var row1Col0 = dr [0];
Console.WriteLine (row1Col0);
}
}
}
因为你永远不可能离开地面,所以必须有一直是问题。
System.Data.DataTable dt = new System.Data.DataTable();
I select excel file.But I dont read excel column value and I dont set datatable.Excel column same datagrid column value.
How to read excel file and set datagrid.
In addition I take this error.Why?
//this error is DataTable is an ambigues reference betweenMicrosoft.Office.Interop.Excel.DataTable and System.Data.DataTable
What I have tried:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyWpfProject { class VMInsertFile { public ContactBtnClickExcelCommand { get { return contactBtnClickExcelCommand; } set{ contactBtnClickExcelCommand= value; OnPropertyChanged(); } public InsertOpenDialog { DataTable dt=new DataTable(); //this error is DataTable is an ambigues reference betweenMicrosoft.Office.Interop.Excel.DataTable and System.Data.DataTable string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + @"Extended Properties='Excel 8.0;HDR=Yes;'"; using(OleDbConnection connection = new OleDbConnection(con)) { connection.Open(); OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection); using(OleDbDataReader dr = command.ExecuteReader()) { while(dr.Read()) { var row1Col0 = dr[0]; Console.WriteLine(row1Col0); } } }
解决方案
", connection); using(OleDbDataReader dr = command.ExecuteReader()) { while(dr.Read()) { var row1Col0 = dr[0]; Console.WriteLine(row1Col0); } } }
Since you never could have gotten off the ground, this must have been "the" question.
System.Data.DataTable dt = new System.Data.DataTable();
这篇关于如何在C#中读取excel列并设置数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文