2012-07-27 91 views
14

有人能指出我如何閱讀Excel spreasheet,通過所有行和列循環使用EPPlus和MVC檢索值的正確方向?所以我看到創建spreasheet的例子,但沒有找到任何打開excel文件並從中讀取值。任何幫助,將不勝感激。使用EPPlus讀取Excel spreasheet

TIA 蘇..

回答

18

簡單的例子

// Get the file we are going to process 
var existingFile = new FileInfo(filePath); 
// Open and read the XlSX file. 
using (var package = new ExcelPackage(existingFile)) 
{ 
    // Get the work book in the file 
    var workBook = package.Workbook; 
    if (workBook != null) 
    { 
     if (workBook.Worksheets.Count > 0) 
     { 
      // Get the first worksheet 
      var currentWorksheet = workBook.Worksheets.First(); 

      // read some data 
      object col1Header = currentWorksheet.Cells[1, 1].Value; 
+0

awesome..thanks..i可以把它帶到我的要求,從這裏開始。 – sansid 2012-07-30 08:55:35

+8

請注意,行和列的索引從1開始,因此「Cells [0,1]」應該爲「Cells [1,1]」 – amcdrmtt 2012-11-22 10:06:15

+1

對於剛接觸此主題的開發人員,您需要爲系統使用「using」語句; System.IO;和OfficeOpenXml;運行這個示例。 – David 2016-07-25 17:02:47