简体   繁体   English

.NET C#-导入Excel文件-工作表名称!

[英].NET C# - Importing Excel File - Sheet Name!

I'm using the NPOI Library to import some data from an excel sheet to my database. 我正在使用NPOI库将一些数据从Excel工作表导入到数据库中。

One early issue I'm curious about, is typically, to begin, you would have something like this: 我想知道的一个早期问题通常是,开始时,您会遇到以下问题:

snippet 片段

 using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs);
                    HSSFSheet sheet = templateWorkbook.GetSheet("Sales");

My query is, what happens when the name of the worksheet is different each time, eg I plan to import sales data, and the sheets within the workbooks I receive from suppliers are titled by date. 我的查询是,每次工作表的名称不同时会发生什么情况,例如,我计划导入销售数据,并且从供应商处收到的工作簿中的工作表均按日期进行了标题。

Is there any way that you can specify GetSheet(); 有什么方法可以指定GetSheet(); to just get the first sheet in the workbook? 只是获得工作簿中的第一张纸?

Any pointers would be much appreciated, 任何指针将不胜感激,

thanks guys :) 多谢你们 :)

Without trying it out I'd assume that you're looking for getSheetAt . 如果不尝试,我会假设您正在寻找getSheetAt

So it would be: 因此它将是:

HSSFSheet sheet = templateWorkbook.GetSheetAt(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM