2015-07-13 96 views
0

使用EPPlus和Linq To Excel有沒有人知道如何獲取所有行的語法?EPPlus和Linq To Excel查詢所有行

IQueryable<Row> excelSheetValues = from workingSheet in excelFile.Worksheet(sheetName) select workingSheet; 

這不是檢索螞蟻數據。如何使用Linq-to-Excel獲取EPPlus的所有行

回答

0

此GitHub sample可能對您有所幫助。

Excel中包提供了大量的輔助方法。從鏈接

using (ExcelPackage package = new ExcelPackage(existingFile)) 

你可以通過工作簿和查詢一樣迭代,

(from cell in sheet.Cells[sheet.Dimension.Address] where cell.Style.Font.Bold select cell); 

示例代碼。

public static void RunLinqSample(DirectoryInfo outputDir) 
    { 
     Console.WriteLine("Now open sample 7 again and perform some Linq queries..."); 
     Console.WriteLine(); 

     FileInfo existingFile = new FileInfo(outputDir.FullName + @"\sample7.xlsx"); 
     using (ExcelPackage package = new ExcelPackage(existingFile)) 
     { 
      ExcelWorksheet sheet = package.Workbook.Worksheets[1]; 

      //Select all cells in column d between 9990 and 10000 
      var query1= (from cell in sheet.Cells["d:d"] where cell.Value is double && (double)cell.Value >= 9990 && (double)cell.Value <= 10000 select cell); 

      Console.WriteLine("Print all cells with value between 9990 and 10000 in column D ..."); 
      Console.WriteLine(); 

      int count = 0; 
      foreach (var cell in query1) 
      { 
       Console.WriteLine("Cell {0} has value {1:N0}", cell.Address, cell.Value); 
       count++; 
      } 

      Console.WriteLine("{0} cells found ...",count); 
      Console.WriteLine(); 

      //Select all bold cells 
      Console.WriteLine("Now get all bold cells from the entire sheet..."); 
      var query2 = (from cell in sheet.Cells[sheet.Dimension.Address] where cell.Style.Font.Bold select cell); 
      //If you have a clue where the data is, specify a smaller range in the cells indexer to get better performance (for example "1:1,65536:65536" here) 
      count = 0; 
      foreach (var cell in query2) 
      { 
       if (!string.IsNullOrEmpty(cell.Formula)) 
       { 
        Console.WriteLine("Cell {0} is bold and has a formula of {1:N0}", cell.Address, cell.Formula); 
       } 
       else 
       { 
        Console.WriteLine("Cell {0} is bold and has a value of {1:N0}", cell.Address, cell.Value); 
       } 
       count++; 
      } 

      //Here we use more than one column in the where clause. We start by searching column D, then use the Offset method to check the value of column C. 
      var query3 = (from cell in sheet.Cells["d:d"] 
          where cell.Value is double && 
           (double)cell.Value >= 9500 && (double)cell.Value <= 10000 && 
           cell.Offset(0, -1).GetValue<DateTime>().Year == DateTime.Today.Year+1 
          select cell); 

      Console.WriteLine(); 
      Console.WriteLine("Print all cells with a value between 9500 and 10000 in column D and the year of Column C is {0} ...", DateTime.Today.Year + 1); 
      Console.WriteLine();  

      count = 0; 
      foreach (var cell in query3) //The cells returned here will all be in column D, since that is the address in the indexer. Use the Offset method to print any other cells from the same row. 
      { 
       Console.WriteLine("Cell {0} has value {1:N0} Date is {2:d}", cell.Address, cell.Value, cell.Offset(0, -1).GetValue<DateTime>()); 
       count++; 
      } 
     } 
    } 
+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –