2010-04-13 3 views

Répondre

3

Vous pouvez essayer quelque chose comme ceci. il devrait fonctionner

U peut spécifier votre plage comme vous le souhaitez à l'intérieur.

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; 
} 
+0

mais si j'ai une plage fixe déjà je passe de la cellule et deux cellules à une fonction dire fromcell = A3 et Tocell = A8 et je dois avoir les valeurs de A3 à A8 – Sathish

+0

alors u besoin d'enlever la boucle for et donne la plage directement dans le code comme suit: Excel.Range range = worksheet.get_Range ("E2", "E16"); –

+2

quel est l'espace de noms que je devrais utiliser pour obtenir ConvertToStringArray – Sathish

2

Une autre approche alternative. Positng comme une réponse spearate parce qu'il donnera moins d'espace pour la confusion.

Excel.Application app = new Excel.Application(); 

Excel.Workbook wbook = null; 

Excel.Worksheet wsheet = null; 

Excel.Range range = null; 

app.Visible = false; 

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 

string filepath = inputFile1.Value.ToString(); 

if (filepath != "") 

{ 

wbook = app.Workbooks.Open(filepath, Missing.Value, Missing.Value, Missing.Value, 

Missing.Value, Missing.Value, Missing.Value, 

Missing.Value, Missing.Value, Missing.Value, 

Missing.Value, Missing.Value, Missing.Value, 

Missing.Value, Missing.Value); 



string currentSheet = "Sheet1"; 

wsheet = (Excel.Worksheet)wbook.Worksheets.get_Item(currentSheet); 

range = wsheet.get_Range("B6", "H20"); 

System.Array myvalues = (System.Array)range.Cells.Value2; 

valueArray = ConvertToStringArray(myvalues); 



if (app != null) 

{ 

app.Workbooks.Close(); 

app.Quit(); 

} 

app = null; 

wsheet = null; 

range = null; 

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; 
} 
Questions connexes