如何在C#中的Excel文件中识别工作表 [英] How to identify a worksheet in an excel file in C#

查看:83
本文介绍了如何在C#中的Excel文件中识别工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用C#读取Excel文件.这是我正在使用的代码:


 公共  void  method()
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.范围
             int  rCnt =  0 ;
             int  cCnt =  0 ;

            xlApp =  Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open( @"  0  true  5 " " " true ,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows," 错误错误 true  0 );
            xlWorkSheet =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item( 4 );
...
..
.
} 



现在我想根据该工作表的名称阅读特定的工作表.Rite现在这是我用来标识工作表的代码:

 xlWorkSheet =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item( 4 );


如何修改此名称,以便可以根据其名称来标识工作表?

在此先感谢!!!

解决方案

与其使用Excel.Workbook.Sheets集合,不如使用Excel.Workbook.Worksheets集合,它更容易访问您可以利用早期绑定的方法.

这是按名称获取工作表的示例代码

 Excel.Application excelApp =  Excel.Application();
excelApp.Visible =  true ;

Excel.Workbook workbook = excelApp.Workbooks.Open(" ,
    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);

// 关键所在:
Excel.Worksheet工作表=(Excel.Worksheet)workbook.Worksheets [" ];  


Hi all,

I''m trying to read an excel file in C#.This is code i''m using :


public void method()
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;
            int rCnt = 0;
            int cCnt = 0;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open(@"path", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4);
...
..
.
}



Now i want to read a particular WorkSheet depending upon the name of that sheet.Rite now this is the code i''m using to identify a worksheet:

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4);


How can i modify this so that the worksheet can be identified depending upon its name??

Thanks in advance!!!

解决方案

nstead of the using Excel.Workbook.Sheets collection, it''s easier to access the Excel.Workbook.Worksheets collection, that way you can utilize early binding.

Here is the sample code to fetch the worksheet by name

Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;

Excel.Workbook workbook = excelApp.Workbooks.Open("C:\MyWorkbook.xls",
    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);

// The key line:
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets["MyWorksheet"];


这篇关于如何在C#中的Excel文件中识别工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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