你如何让Excel工作簿的第一页的名字吗? [英] How do you get the name of the first page of an excel workbook?

查看:175
本文介绍了你如何让Excel工作簿的第一页的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你不知道在Excel工作簿的第一个工作表的名称。而你要找到一种方法,从的第一个的页面阅读。这个片断有时工作,但并非总是如此。难道仅仅是我吗?或者是有没有道理的方式做到这一点?

Suppose you don't know the name of the first worksheet in an excel workbook. And you want to find a way to read from the first page. This snippet sometimes works, but not always. Is it just me? Or is there a no brainer way to do this?

            MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + inputFile + "';Extended Properties=Excel 8.0;");

            String[] excelSheets = new String[tbl.Rows.Count]; 
            int i = 0;
            foreach (DataRow row in tbl.Rows)
            {
                excelSheets[i] = row["TABLE_NAME"].ToString();
                i++;
            }
            string pageName = excelSheets[0]; 

            OleDbDataAdapter myAdapter = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" + pageName + "]", MyConnection);

请注意:我正在寻找在名称第一个工作表中。

Note: I am looking for the name of the first worksheet.

推荐答案

如果您有Office的机器,为什么不使用Visual Studio工具办公室(安装VSTO)。这是本质上的代码来获取工作表:

If you have Office installed on the machine, why not just use Visual Studio Tools for Office (VSTO). Here is essentially the code to get the worksheet:

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook =  app.Workbooks.Open(fileName,otherarguments);
Microsoft.Office.Interop.Excel.Worksheet worksheet =  workbook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

这篇关于你如何让Excel工作簿的第一页的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆