Read all the cell values from an Excel range in C#

后端 未结 2 1770
误落风尘
误落风尘 2020-12-11 05:09

Suppose I have range from E2 to E16. How do I read values from cells E2 to E16?

2条回答
  •  臣服心动
    2020-12-11 06:04

    You can try somethinfg like this. it should work

    U can specify ur range as u wish inside.

    this.openFileDialog1.FileName = "*.xls";
      if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
       {
          Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
             openFileDialog1.FileName, 0, true, 5,
              "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
              0, true); 
         Excel.Sheets sheets = theWorkbook.Worksheets;
         Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
         for (int i = 1; i <= 10; i++)
         {
         Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" + i.ToString());
         System.Array myvalues = (System.Array)range.Cells.Value;
         string[] strArray = ConvertToStringArray(myvalues);
         }
    }
    
    string[] ConvertToStringArray(System.Array values)
    { 
    
    // create a new string array
    string[] theArray = new string[values.Length];
    
    // loop through the 2-D System.Array and populate the 1-D String Array
     for (int i = 1; i <= values.Length; i++)
      {
       if (values.GetValue(1, i) == null)
        theArray[i-1] = "";
      else
       theArray[i-1] = (string)values.GetValue(1, i).ToString();
      }
    
      return theArray;
    }
    

提交回复
热议问题