2012-03-11 156 views
0

從c#中的exel讀取時遇到一些麻煩。 我有這樣的代碼,我讀出每一個細胞,從A到X.從Excel中讀取單元格

  int i = 1; 
      int number; 
      System.Array myvalues; string[] strArray; 
      Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "X" + i.ToString()); 
      while(range.Count!=0) 
      { 
        i++; 
        range = worksheet.get_Range("A" + i.ToString(), "X" + i.ToString()); 
        myvalues = (System.Array)range.Cells.Value; 
        strArray = ConvertToStringArray(myvalues); 
        number = Convert.ToInt32(strArray[0]); 
      } 

我的問題是:我怎麼能以「數量」值,下一個讀取Excel中的4 *「號」行?

例如:

  A B C D E F G H I J 
     a a a a a 1 a a a a 

F公司細胞值是1,所以我想讀(GHIJ) 若F的單元格的值是2的我想讀取(GHIJKLMN)

  A B C D E F G H I J K L M N 
     a a a a a 2 a a a a a a a a 

F公司單元格值3:

 A B C D E F G H I J K L M N O P Q R 
     a a a a a 3 a a a a a a a a a a a a 

回答

0

代碼:

int i = 1; 
do 
{ 

    i++; 
    var range = worksheet.get_Range("A" + i.ToString(), "X" + i.ToString()); 
    if (range.Count != 0) 
    { 
     var myvalues = (System.Array)range.Cells.Value; 
     var strArray = ConvertToStringArray(myvalues); 
     var number = Convert.ToInt32(strArray[0]); 
     MyMethodReadRage(worksheet, number); 
    } 

} while(range.Count!=0); 

爲讀取範圍的方法:

void MyMethodReadRage(Microsoft.Office.Interop.Excel.Worksheet worksheet, int numberRows) 
{ 
    var range = worksheet.get_Range("A" + numberRows, "X" + (numberRows + 4)); 
    /*code*/ 
} 

EDIT

讀取列的值「F」,乘以值除以4並添加先前列的數量(最多「F」)。然後你使用這個(這個未經證實的)函數來得到這封信。

/// <summary> 
    /// http://www.freevbcode.com/ShowCode.asp?ID=4303 
    /// </summary> 
    private string ColumnLetter(int ColumnNumber) 
    { 
     if (ColumnNumber > 26) 
     { 
      return string.Format("{0}{1}", (char)(Convert.ToInt32((ColumnNumber - 1)/26) + 64), (char)(((ColumnNumber - 1) % 26) + 65)); 
     } 
     else 
     { 
      return string.Format("{0}", (char)(ColumnNumber + 64)); 
     } 
    } 

編輯II

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplicationExcelCeldas 
{ 
    class Program 
    { 
     private const int NumberPositionColumnF = 6; 

     static void Main(string[] args) 
     { 
      Microsoft.Office.Interop.Excel.Worksheet worksheet = new Microsoft.Office.Interop.Excel.Worksheet(); 

      var i = 0; 
      var test = new TestExcel(); 
      do 
      { 
       i++; 
       var rangeF = worksheet.get_Range(string.Format("F{0}" , i)); 
       if (rangeF.Count == 0) 
       { 
        break; 
       } 

       var values = test.GetCellsStringArray(rangeF); 
       if (values.Count() > 0) 
       { 
        int number = 0; 
        if (int.TryParse(values[0], out number)) 
        { 
         var rangeAll = worksheet.get_Range(
          string.Format("A{0}" , i), 
          string.Format("{0}{1}", test.ColumnLetter(Program.NumberPositionColumnF + (number * 4)), i)); 


         /* 
         your code for work with rangeAll 
         */ 
        } 
       } 

      } while (true); 
     } 
    } 

    class TestExcel 
    { 
     internal string[] GetCellsStringArray(Microsoft.Office.Interop.Excel.Range range) 
     { 
      var myvalues = (System.Array)range.Cells.Value; 
      return this.ConvertToStringArray(myvalues); 
     } 

     internal string[] ConvertToStringArray(System.Array values) 
     { 
      string[] theArray = new string[values.Length]; 
      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; 
     } 

     internal string ColumnLetter(int columnNumber) 
     { 
      if (columnNumber > 26) 
      { 
       return string.Format("{0}{1}", (char)(Convert.ToInt32((columnNumber - 1)/26) + 64), (char)(((columnNumber - 1) % 26) + 65)); 
      } 
      else 
      { 
       return string.Format("{0}", (char)(columnNumber + 64)); 
      } 
     } 
    } 
} 
+0

我不知道如果單元格是「X」。我只知道「號碼」保存在「W」單元中。我可能有4個塊,我的意思是(4 * 4 * 4) – Vlasin 2012-03-11 17:03:50

+0

你可以把一個excel的例子,這將是所需的結果? – 2012-03-12 01:52:39

+0

是的!我修改了我的問題... – Vlasin 2012-03-12 07:36:49

0

嗨,我剛剛創建的代碼,它會突破從第一列中的所有行,只要你想你可以改變它(微軟最新的庫互操作DLL .NET原生lib)它的工作很好,希望它可以幫助你

 int sheetIndex = 1; //PLEASE NOTE THIS LIB WORKS WITH NON-ZERO BASED INDEX 
     string excelFilePath = "your_path/your_excel_file.xls"; 
     List<string> yourList = new List<string>(); 
     Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); 
     Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Open(excelFilePath);    
     Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[sheetIndex]; 
     Microsoft.Office.Interop.Excel.Range rangeSelection = worksheet.Columns[1]; 

     foreach (Microsoft.Office.Interop.Excel.Range row in rangeSelection.Rows) 
     { 
      Microsoft.Office.Interop.Excel.Range cell = (Microsoft.Office.Interop.Excel.Range)row.Cells[1, 1]; 
      if (cell.Value2 != null) 
       list.Add(cell.Value2.ToString()); 
     }