[英]How to find the name of the columns in an excel file
I was wondering how can I get the column names from an excel file and write the names inside a combobox. 我想知道如何从excel文件中获取列名并将其写在组合框中。
This is the excel print and the names I want are in red 这是excel打印,我想要的名称为红色
This is the code I am using to open the excel file: 这是我用来打开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;
}
}
1) include 1)包括
HDR=YES
in your query string 在您的查询字符串中
if 1) won't work then try the following 2) include the range 如果1)无效,请尝试以下2)包含范围
var sql = "SELECT * FROM [" + SHEET + "$" + Range + "];
where range your target range in format X1:X2 etc 您的目标范围的范围,格式为X1:X2等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.