将WinForms图表绑定到Excel列

时间:2013-12-06 16:00:26

标签: c# winforms excel datagridview charts

我需要创建一个带图表控件的应用。我需要将该图表控件绑定到Excel电子表格的特定列。到目前为止,我已将datagridview绑定到列。但该列包含整数,空值和字符串。如何从该列中仅提取整数? 这是代码:

DataTable dt = getDataFromXLS(@"C:\myFile.xlsx");
            if (dt != null)
                dataGridView1.DataSource = dt;
                //chart1.DataSource = dt;

private DataTable getDataFromXLS(string fileName)
        {
            try
            {
                string strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                                            "Data Source=" + fileName + "; Extended Properties=Excel 12.0;";
                OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
                cnCSV.Open();
                OleDbCommand cmdSelect = new OleDbCommand(@"Select F9 From [Sheet1$]", cnCSV);
                DataTable dtCSV = new DataTable(); 
                OleDbDataAdapter daCSV = new OleDbDataAdapter();
                daCSV.SelectCommand = cmdSelect;
                daCSV.Fill(dtCSV);
                cnCSV.Close();
                daCSV = null;

                return dtCSV;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }
            finally { }
        }

1 个答案:

答案 0 :(得分:0)

没关系。将图表配置为忽略文本和空值。