我想知道如何从excel文件中获取列名并将名称写在组合框中。
This is the excel print and the names I want are in red
这是我用来打开excel文件的代码:
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xls", ValidateNames = true })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
reader.IsFirstRowAsColumnNames = true;
result = reader.AsDataSet();
comboBox1.Items.Clear();
foreach (DataTable dt in result.Tables) comboBox1.Items.Add(dt.TableName);
reader.Close();
string ConecçãoDB = ConfigurationManager.ConnectionStrings["ConecçaoDB"].ConnectionString;
string Table = ConfigurationManager.AppSettings["table"];
string ssqltable = Table;
string ssqlconnectionstring = ConecçãoDB;
filename = ofd.FileName;
MessageBox.Show(Convert.ToString(filename));
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";
var conexao = new System.Data.OleDb.OleDbConnection(connectionString);
var sql = "SELECT * FROM ["+ comboBox1.SelectedText+ "$]";
string sclearsql = "delete from " + ssqltable;
}
}
答案 0 :(得分:0)
1)包括
HDR=YES
在您的查询字符串中
如果1)不起作用 然后尝试以下方法 2)包括范围
var sql = "SELECT * FROM [" + SHEET + "$" + Range + "];
其中您的目标范围的范围是格式X1:X2等