读取excel第一个工作表名 怎么用C#读取excel第一个工作表名? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 通过OleDb连接,把excel文件作为数据源来读取(这里是fill进dataset,也可以返回OleDbDataReader来逐行读,数据较快) 获得excel对象后 sheet[0]应该是第一个表名称了 查询EXCEL:http://topic.csdn.net/u/20090424/10/4012f164-f557-4b06-83b0-16cde29d57f3.html http://www.programfan.com/club/showtxt.asp?id=138851查询工作表名~ /// <summary> /// 读取Excel中表的名称 /// </summary> /// <param name="excelFile"></param> /// <returns></returns> public String[] GetExcelSheetNames(string excelFile) { OleDbConnection objConn = null; System.Data.DataTable dt = null; try { //此连接可以操作.xls与.xlsx文件 string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + excelFile + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'"; objConn = new OleDbConnection(strConn); objConn.Open(); dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (dt == null) { return null; } String[] excelSheets = new String[dt.Rows.Count]; int i = 0; foreach (DataRow row in dt.Rows) { excelSheets[i] = row["TABLE_NAME"].ToString(); i++; } return excelSheets; } catch { return null; } finally { if (objConn != null) { objConn.Close(); objConn.Dispose(); } if (dt != null) { dt.Dispose(); } } }这个方法可以读取Excel中的工作表的名称,但得出的名称被排序了,我想要的是第一个工作表的名称。 Microsoft.Office.Interop.Excel.Application obj = default(Microsoft.Office.Interop.Excel.Application);Microsoft.Office.Interop.Excel.Workbook objWB = default(Microsoft.Office.Interop.Excel.Workbook);string FirstSheetName = null;obj = (Microsoft.Office.Interop.Excel.Application)Microsoft.VisualBasic.Interaction.CreateObject("Excel.Application", string.Empty);objWB = obj.Workbooks.Open(filepath, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing);FirstSheetName = ((Microsoft.Office.Interop.Excel.Worksheet)objWB.Worksheets[1]).Name;objWB.Close(Type.Missing, Type.Missing, Type.Missing);objWB = null;obj.Quit();obj = null; public static string GetExcelFirstTableName0(string ExcelFile) { string tableName = null; if (System.IO.File.Exists(ExcelFile)) { using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet." + "OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + ExcelFile)) { conn.Open(); DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); tableName = dt.Rows[0][2].ToString().Trim(); } } return tableName; } Excel.Application excel = new Excel.ApplicationClass(); Excel._Workbook xBk = excel.Workbooks.Add("\\路径\\EXCEL名称.xls"); Excel._Worksheet xSt = (Excel._Worksheet)xBk.Worksheets[1]; 第一个工作表名= xSt.Name +1注意这个string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\book1.xls;Extended Properties=Excel 8.0" ;不同版本的链接字符串不一样的,,, dataSource转换DataView问题 求个WinForm免费好用的生成图表的控件~! 水晶报表报表可根据纸张大小自动缩放 怎么将字符串转换成xmldatadocument的对象格式 智能设备 word文档 CheckBoxList控件中的checkbox被选中,则定义好的.js文件就不可用 高分请教!Datagrid中实现css功能,或实现这样的效果! 谁能举例说明白Match,Group,Capture的关系? 怪事 串口通讯为什么会死掉 .net2005和.new2003中listview控件效率问题 怎么用一个按钮来控制gridview
http://topic.csdn.net/u/20090424/10/4012f164-f557-4b06-83b0-16cde29d57f3.html
查询工作表名~
/// 读取Excel中表的名称
/// </summary>
/// <param name="excelFile"></param>
/// <returns></returns>
public String[] GetExcelSheetNames(string excelFile)
{ OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
//此连接可以操作.xls与.xlsx文件
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + excelFile + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
objConn = new OleDbConnection(strConn);
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheets;
}
catch
{
return null;
}
finally
{
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}这个方法可以读取Excel中的工作表的名称,但得出的名称被排序了,我想要的是第一个工作表的名称。
Microsoft.Office.Interop.Excel.Workbook objWB = default(Microsoft.Office.Interop.Excel.Workbook);
string FirstSheetName = null;
obj = (Microsoft.Office.Interop.Excel.Application)Microsoft.VisualBasic.Interaction.CreateObject("Excel.Application", string.Empty);
objWB = obj.Workbooks.Open(filepath, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing,Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,Type.Missing, Type.Missing);
FirstSheetName = ((Microsoft.Office.Interop.Excel.Worksheet)objWB.Worksheets[1]).Name;
objWB.Close(Type.Missing, Type.Missing, Type.Missing);
objWB = null;
obj.Quit();
obj = null;
{
string tableName = null;
if (System.IO.File.Exists(ExcelFile))
{
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet." +
"OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + ExcelFile))
{
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
tableName = dt.Rows[0][2].ToString().Trim();
}
}
return tableName;
}
Excel._Workbook xBk = excel.Workbooks.Add("\\路径\\EXCEL名称.xls");
Excel._Worksheet xSt = (Excel._Worksheet)xBk.Worksheets[1];
第一个工作表名= xSt.Name
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\book1.xls;Extended Properties=Excel 8.0" ;
不同版本的链接字符串不一样的,,,