OpenFileDialog ofImport = new OpenFileDialog();
ofImport.Title = "Select file";
ofImport.InitialDirectory = @"c:\";
ofImport.FileName = txtFileName.Text;
ofImport.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
ofImport.FilterIndex = 1;
ofImport.RestoreDirectory = true;
if (ofImport.ShowDialog() == DialogResult.OK)
{
string path = System.IO.Path.GetFullPath(ofImport.FileName);
string query = "SELECT * FROM Customer.xlsx";
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ofImport.FileName+";Extended Properties=" + "\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
//DataSet dataSet = new DataSet();
adapter.Fill(dsSource);
dataGridView1.DataSource = dsSource;
}
else
{
ofImport.Dispose();
}
我想使用DataGridView
将Excel数据检索到dataset
。 dsSource
是使用的数据集。
我收到的错误在adapter.Fill(dsSource);
行
Microsoft Access数据库引擎找不到对象'xlsx'。 确保对象存在,并拼写其名称和路径 名字正确。如果'xlsx'不是本地对象,请检查您的网络 连接或联系服务器管理员。
我可以选择文件但不填写数据集。
怎么办?
答案 0 :(得分:4)
在以下行中,您要从文件中选择:
string query = "SELECT * FROM " + ofImport.FileName;
然而,您需要从表中进行选择,所以这应该是这样的:
string query = "SELECT * FROM [Sheet1$]"; // Note the '$' sign!!
您需要找出Excel文件的工作表名称,以便您可以从中进行选择(工作表名称显示在工作表的选项卡中 - 只需向其附加$
符号)。 文件名仅用于连接字符串,因此数据库引擎知道要打开哪个文件。
想想以下与.NET中通常的SQL数据库访问类比:
file name = database name
sheet name = table name
修改强>
为了使事情更清楚:在下图中,工作表名称用红色圈出。在您的代码中,在select语句中写入工作表名称,后跟美元符号。
答案 1 :(得分:3)
这是我的代码,在没有数据库配置的情况下读取excel到datagridview。
DataTable dt = new DataTable("dataTable");
DataSet dsSource = new DataSet("dataSet");
dt.Reset();
Excel.Workbook ExWorkbook;
Excel.Worksheet ExWorksheet;
Excel.Range ExRange;
Excel.Application ExObj = new Excel.Application();
openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
ExWorkbook = ExObj.Workbooks.Open(openFileDialog1.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ExWorksheet = (Excel.Worksheet)ExWorkbook.Sheets.get_Item(1);
ExRange = ExWorksheet.UsedRange;
for (int Cnum = 1; Cnum <= ExRange.Columns.Count; Cnum++)
{
dt.Columns.Add(new DataColumn((ExRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
}
dt.AcceptChanges();
string[] columnNames = new String[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
{
columnNames[0] = dt.Columns[i].ColumnName;
}
//string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();
for (int Rnum = 2; Rnum <= ExRange.Rows.Count; Rnum++)
{
DataRow dr = dt.NewRow();
for (int Cnum = 1; Cnum <= ExRange.Columns.Count; Cnum++)
{
if ((ExRange.Cells[Rnum, Cnum] as Excel.Range).Value2 != null)
{
dr[Cnum - 1] = (ExRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
}
}
dt.Rows.Add(dr);
dt.AcceptChanges();
}
ExWorkbook.Close(true, Missing.Value, Missing.Value);
ExObj.Quit();
dataGridView1.DataSource = dt;
答案 2 :(得分:1)
DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
dt = dsSource.Tables[Index];
dt.Reset();
Excel.Workbook workbook;
Excel.Worksheet NwSheet;
Excel.Range ShtRange;
Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
OpenFileDialog filedlgExcel = new OpenFileDialog();
filedlgExcel.Title = "Select file";
filedlgExcel.InitialDirectory = @"c:\";
//filedlgExcel.FileName = textBox1.Text;
filedlgExcel.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
filedlgExcel.FilterIndex = 1;
filedlgExcel.RestoreDirectory = true;
if (filedlgExcel.ShowDialog() == DialogResult.OK)
{
workbook = ExcelObj.Workbooks.Open(filedlgExcel.FileName, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
ShtRange = NwSheet.UsedRange;
for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
{
dt.Columns.Add(new DataColumn((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
}
dt.AcceptChanges();
string[] columnNames = new String[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
{
columnNames[0] = dt.Columns[i].ColumnName;
}
//string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();
for (int Rnum = 2; Rnum <= ShtRange.Rows.Count; Rnum++)
{
DataRow dr = dt.NewRow();
for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
{
if ((ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2 != null)
{
dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
}
}
dt.Rows.Add(dr);
dt.AcceptChanges();
}
workbook.Close(true, Missing.Value, Missing.Value);
ExcelObj.Quit();
dataGridView1.DataSource = dt;