2017-05-03 233 views
-1

我有一個以下格式的excel文件。使用aspose單元讀取excel中單元格的所有值

col 1|col 2 | col 3 | status|some col| 
------------------------------------- 
1 | 23 | test | UDS | Test 
------------------------------------- 
12 | 2 | test2 | ADS | Test23 

我需要將狀態列中的所有值讀入List。我該怎麼做?這裏的'狀態'列總是在excel的一個固定區域。恩。第8行AA列將始終爲'狀態'

某些列標題正在重複,因此我無法將此excel讀入數據表並從數據表中讀取列值。

回答

1

使用的Aspose.Cells的API,這裏是完成任務的另一種簡單的方法:如 示例代碼:

 //Open your template file. 
     Workbook wb = new Workbook("e:\\test2\\Book1.xlsx"); 
     //Get the first worksheet. 
     Worksheet worksheet = wb.Worksheets[0]; 
     //Get the cells collection. 
     Cells cells = worksheet.Cells; 

     //Define the list. 
     List<string> myList = new List<string>(); 
     //Get the AA column index. (Since "Status" is always @ AA column. 
     int col = CellsHelper.ColumnNameToIndex("AA"); 

     //Get the last row index in AA column. 
     int last_row = worksheet.Cells.GetLastDataRow(col); 

     //Loop through the "Status" column while start collecting values from row 9 
     //to save each value to List 
     for (int i = 8; i <= last_row; i++) 
     { 
      myList.Add(cells[i, col].Value.ToString()); 
     } 
    } 

我在Aspose擔任Support developer/Evangelist的職位。

0

我一直在用excel閱讀。這是我實施的方式,可以幫助你。我已經更新了代碼以匹配被問到的問題。讓我知道,如果我能以任何方式幫助你!

public static List<string> ReadExcelDataFile(string fileFullPath) 
    { 
     Application xlApp = new Application(); 
     Workbook xlWorkBook = null; 
     Worksheet dataSheet = null; 
     Range dataRange = null; 
     List<string> data = new List<string>(); 
     object[,] valueArray; 

     try 
     { 
      // Open the excel file 
      xlWorkBook = xlApp.Workbooks.Open(fileFullPath, null, true); 

      if (xlWorkBook.Worksheets != null 
       && xlWorkBook.Worksheets.Count > 0) 
      { 
       // Get the first data sheet 
       dataSheet = xlWorkBook.Worksheets[1]; 

       // Get range of data in the worksheet 
       dataRange = dataSheet.UsedRange; 

       // Read all data from data range in the worksheet 
       valueArray = (object[,])dataRange.get_Value(XlRangeValueDataType.xlRangeValueDefault); 

       // Here if you sure of the column index then you need not loop and find the column in excel shet. Just directly index to the column and skip first loop 
       for (int colIndex = 0; colIndex < valueArray.GetLength(1); colIndex++) 
       { 
        if (valueArray[0, colIndex] != null 
         && !string.IsNullOrEmpty(valueArray[0, colIndex].ToString()) 
         && valueArray[0, colIndex].ToString().Equals("status")) 
        { 
         for (int rowIndex = 1; rowIndex < valueArray.GetLength(0); rowIndex++) 
         { 
          if (valueArray[rowIndex, colIndex] != null 
           && !string.IsNullOrEmpty(valueArray[rowIndex, colIndex].ToString())) 
          { 
           // Get data from each column which is not null and is a numeric value 
           data.Add(valueArray[rowIndex, colIndex].ToString()); 
          } 
         } 
        } 
       } 
      } 
      else 
      { 
       throw new Exception("Invalid or Empty sheet"); 
      } 
     } 
     catch (Exception generalException) 
     { 
      throw generalException; 

     } 
     finally 
     { 
      if (xlWorkBook != null) 
      { 
       // Close the workbook after job is done 
       xlWorkBook.Close(); 
       xlApp.Quit(); 
      } 
     } 

     return data; 
    } 
0

使用的Aspose.Cells我能做到這一點如下,

  Workbook workbook = new Workbook(exlFile); 
      Worksheet worksheet = workbook.Worksheets[0]; 
      int column = 3; //this is fixed 
      int row = 5;  // this is fixed 
      int rows = worksheet.Cells.MaxRow; 
      Range range = worksheet.Cells.CreateRange(row, column, rows - row + 1, 1); 
      DataTable dataTable = range.ExportDataTable(); 
相關問題