Fra*_*dle 9 c# excel excel-interop
我正在使用VS2010 + Office Interop 2007尝试从具有5-6页的Excel电子表格中获取一些特定的电子表格名称.我正在做的就是在制表符分隔的文本文件中保存我需要的那些电子表格以便进一步处理.因此,对于我得到的三个电子表格名称,每个名称都有自己的制表符分隔文本文件.
我可以通过Interop保存文件作为制表符分隔,但这假设我知道给定的页面名称是什么.我被告知每个页面名称都不遵循严格的命名约定,但在查找所需名称时,我可以考虑多个名称,如"RCP","rcp","收件人"等.
我的问题是,我可以在某种索引中获取所有电子表格页面名称,以便我可以遍历它们并尝试找到我需要的三个名称吗?这比尝试通过bajillion尝试/捕获来获取"RCP","rcp","收件人"页面要好得多.
我很接近,因为我可以通过以下方式在Excel电子表格中获取COUNT个页面:
Excel.Application excelApp = new Excel.Application(); // Creates a new Excel Application
excelApp.Visible = true; // Makes Excel visible to the user.
// The following code opens an existing workbook
string workbookPath = path;
Excel.Workbook excelWorkbook = null;
try
{
excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
}
catch
{
//Create a new workbook if the existing workbook failed to open.
excelWorkbook = excelApp.Workbooks.Add();
}
// The following gets the Worksheets collection
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
Console.WriteLine(excelSheets.Count.ToString()); //dat count
Run Code Online (Sandbox Code Playgroud)
感谢您的时间.
Der*_*rek 14
foreach ( Worksheet worksheet in excelWorkbook.Worksheets )
{
MessageBox.Show( worksheet.Name );
}
Run Code Online (Sandbox Code Playgroud)
你可以使用字典:
Dictionary<string, Worksheet> dict = new Dictionary<string, Worksheet>();
foreach ( Worksheet worksheet in excelWorkbook.Worksheets )
{
dict.Add( worksheet.Name, worksheet );
}
// accessing the desired worksheet in the dictionary
MessageBox.Show( dict[ "Sheet1" ].Name );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34510 次 |
| 最近记录: |