void InitializeOledbConnection(string filename, string extrn) { connString = ""; if (extrn == ".xlsx") connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + glb + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'"; //for above excel 2007 else connString = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + glb + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"; //for below excel 2007 Oledb = new OleDbConnection(connString); } private DataTable ReadFile() { schemaTable = new DataTable(); OleDbCommand Oledbcmd = new OleDbCommand(); Oledbcmd.Connection = Oledb; Oledb.Open(); Oledbcmd.CommandText = "Select SIGNAL,ROUTE_BUTTON from [CONTROL_TABLE$]"; OleDbDataAdapter da = new OleDbDataAdapter(Oledbcmd); ContentTable = new DataTable(); da.Fill(ContentTable); OleDbDataReader dr = Oledbcmd.ExecuteReader(); ContentTable = null; dr.Close(); Oledb.Close(); Oledb.Dispose(); return ContentTable; } private void t1NextTabRedirectButton_Click(object sender, EventArgs e) { string filePath = string.Empty; string fileExt = string.Empty; InitializeOledbConnection("C:\\gui_Files\\ICT_LAYOUT_AJJ.xlsx", ".xlsx"); DataTable schemaTable = ReadFile(); routeNamesListView.Columns.Add("SIGNAL"); routeNamesListView.Columns.Add("ROUTE_BUTTON"); routeNamesListView.View = View.List; routeNamesListView.CheckBoxes = true; }
ContentTable = null;
string GetConnString(string path) { string connString = ""; string extrn = Path.GetExtension(path); if (extrn == ".xlsx") connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'"; //for above excel 2007 else connString = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"; //for below excel 2007 return (connString); } private DataTable ReadFile(string path) { DataTable dt = new DataTable(); string conString = GetConnString(path); using (OleDbConnection con = new OleDbConnection(conString)) { string query = "Select SIGNAL,ROUTE_BUTTON from [CONTROL_TABLE$]"; OleDbCommand Oledbcmd = new OleDbCommand(query, con); OleDbDataAdapter da = new OleDbDataAdapter(Oledbcmd); da.Fill(dt); } return dt; } private void t1NextTabRedirectButton_Click(object sender, EventArgs e) { DataTable schemaTable = ReadFile("C:\\gui_Files\\ICT_LAYOUT_AJJ.xlsx"); }
Oledbcmd.CommandText = "Select SIGNAL,ROUTE_BUTTON from [CONTROL_TABLE$]"; Oledbcmd.CommandType = CommandType.Text;
public string connString; DataTable schemaTable; DataTable ContentTable; private void button1_Click(object sender, EventArgs e) { string filePath = string.Empty; string fileExt = string.Empty; string fileName = "D:\\00Latitude\\00InfyTFS\\Standard Interface\\ExcelRepository\\GPB 14-4-17.xls"; connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; DataTable schemaTable = ReadFile(); routeNamesListView.Columns.Add("IPCNODEID"); routeNamesListView.Columns.Add("IPC"); for (int i = 0; i < schemaTable.Rows.Count; i++) { DataRow drow = schemaTable.Rows[i]; // Only row that have not been deleted if (drow.RowState != DataRowState.Deleted) { // Define the list items ListViewItem lvi = new ListViewItem(drow[0].ToString()); for (int j = 1; j < schemaTable.Columns.Count; j++) { lvi.SubItems.Add(drow[j].ToString()); } routeNamesListView.Items.Add(lvi); } } routeNamesListView.View = View.Details; routeNamesListView.CheckBoxes = true; } private DataTable ReadFile() { OleDbConnection conn = new OleDbConnection(connString); schemaTable = new DataTable(); OleDbCommand Oledbcmd = new OleDbCommand(); Oledbcmd.Connection = conn; conn.Open(); Oledbcmd.CommandText = "Select IPCNODEID,IPC from [daily data$]"; Oledbcmd.CommandType = CommandType.Text; //-------------- OleDbDataAdapter da = new OleDbDataAdapter(Oledbcmd); ContentTable = new DataTable(); da.Fill(ContentTable); OleDbDataReader dr = Oledbcmd.ExecuteReader(); dr.Close(); conn.Close(); conn.Dispose(); return ContentTable; }
routeNamesListView.View = View.Details;
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)