如何做到使用C#

问题描述:

我想做一个excel文件的映射,但在一定的方式列Excel文件的映射。 如实施例我有一个EXEL文件,其中(Excel文件的)第2列必须被引用的有SQL数据库的第1列。如何做到使用C#

这是我使用的Formenter image description here

我已经有了SQL搜索做我只是想知道我可以在组合框中把名字列在Excel文件。

这是我使用搜索Excel文件中的代码:

 using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xls", ValidateNames = true }) 
     { 
      DataSet result; 
      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

具体什么你有问题 - 实际数据绑定到组合框本身?你到底采取了哪些步骤来尝试这些,或者你卡住了? – LightCC

+0

@LightCC我卡上添加数据,组合框 –

更新是根据您的最新评论,因为我误解你需要什么我的答案。

您需要访问Excel对象模型之后,你将拥有的Excel数据全面且易于控制。

对于固体和简单的答案是指:

Microsoft Interop: Excel Column Names

当使用建议的代码确保您以下引用添加到您的代码:

using Microsoft.Office.Interop.Excel; 

记住所提供的答案有将列名放入一个通用的字符串列表中。

之后,你必须将其绑定到组合框:

BindingSource bs = new BindingSource(); 
bs.DataSource=columnNames ; 
comboBox1.DataSource = bs; 
+0

你的意思是什么“在此处添加代码来填充从数据库数据集DS” –

+0

所以我们说你的Excel文件有3列A,B,C,在COMBOX你想看 A,B,C(C#的网络表单)?请确认。 –

+0

是的,这就是我需要 –