读取 EXCEL的工作表名,读取选中后工作表的列名
2011-09-02 14:04
316 查看
本文地址:/article/5753154.html
读取Excel中所有工作表名
读取工作表中的字段列名
参考网页:/article/5200096.html
/article/5789281.html
http://hi.baidu.com/sunsung111/blog/item/685c5ae606831525b838206a.html
读取Excel中所有工作表名
//是否已有字段头 string strHead = "YES"; if(chkHead.Checked==false) { strHead = "NO"; } string excelFilepath = openFileExcel.FileName; string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilepath + ";Extended Properties=\"Excel 12.0;HDR=" + strHead + ";\""; if ((System.IO.Path.GetExtension(excelFilepath)).ToLower() == ".xls") { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilepath + ";Extended Properties=\"Excel 8.0;HDR=" + strHead + ";IMEX=1\""; } using (OleDbConnection conn = new OleDbConnection(strCon)) { conn.Open(); //返回在目录中定义的表 System.Data.DataTable schemeTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); for (int i = 0; i < schemeTable.Rows.Count; i++) { //工作表名 schemeTable.Rows[i][2].ToString().Trim() cmbSheets.Items.Add(schemeTable.Rows[i][2].ToString().Trim()); } if (cmbSheets.Items.Count > 0) { cmbSheets.Enabled = true; //cmbSheets.SelectedIndex = 0; } else { cmbSheets.Enabled = false; } }
读取工作表中的字段列名
//是否已有字段头 string strHead = "YES"; if (!bHead) { strHead = "NO"; } string strSheetName = ""; if(comboBoxSheets.SelectedIndex!=-1) { strSheetName = comboBoxSheets.SelectedItem.ToString(); } else { MessageBox.Show("选择工作表"); return; } string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelFilepath + ";Extended Properties=\"Excel 12.0;HDR=" + strHead + ";\""; if ((System.IO.Path.GetExtension(strExcelFilepath)).ToLower() == ".xls") { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strExcelFilepath + ";Extended Properties=\"Excel 8.0;HDR=" + strHead + ";IMEX=1\""; } using (OleDbConnection conn = new OleDbConnection(strCon)) { //读取选择的工作表 OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter("Select * from [" + strSheetName + "]", conn); DataSet dataSet = new DataSet(); oleDbDataAdapter.Fill(dataSet, "mytb"); dataGridView.DataSource = dataSet.Tables["mytb"]; //工作表内容的列名,注意Excel连接串中的HDR属性 for (int i = 0; i < dataSet.Tables["mytb"].Columns.Count; i++) { comboBoxColumns.Items.Add(dataSet.Tables["mytb"].Columns[i].ColumnName); } comboBoxColumns.Enabled = true; }
参考网页:/article/5200096.html
/article/5789281.html
http://hi.baidu.com/sunsung111/blog/item/685c5ae606831525b838206a.html
相关文章推荐
- 读取Excel默认工作表导出XML
- ASP.NET如何读取Excel文件的多张工作表
- C#读取EXCEL中工作表不是Sheet1等默认名称
- Python 导入文件夹中所有excel并按列名读取插入数据库
- c# 导入excel 读取excel数据 多个工作表时 解决科学计数法显示的问题
- c#读取Excel的列名问题
- python 处理Excel 常见问题-读取Excel中时间
- [导入]VB中操作Excel的一般做法(读取)
- POI操作Excel,读取,写入
- ADO.NET 读取EXCEL的实现代码((c#))
- python 读取excel文件生成sql文件实例详解
- -------------------------数字转换成EXCEL列名---------------------------------
- poi写入读取excel 2007版xlsx 97版xls
- 通过oledb驱动读取excel、csv数据丢失解决方案
- 读取Excel文件中的数据,并显示在MSFlexGrid表格中
- C#读取Excel数据收藏
- POI读取EXCEL文件的错误(invalid header signature )处理
- python读取excel一例-------从工资表逐行提取信息
- Excel 读取
- JAVA POI 读取2013 EXCEL时 XSSFWorkbook(fis);出错