2012-01-27 22 views
0

Я написал этот код, чтобы подсчитать количество строк, заполненных на листе Excel. Он работает до тех пор, пока не достигнет определенного количества строк (а не всего). Затем приходит с сообщением об ошибке «Исключение из HRESULT: 0x800A01A8» Любая помощь очень ценится«Исключение из HRESULT: 0x800A01A8»

namespace ConsoleApplication1 
{ 
    class ExcelClass 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main(string[] args) 
     { 
      Excel.Application excelApp = new Excel.Application(); // Creates a new Excel Application 
      excelApp.Visible = true; // Makes Excel visible to the user. 


      // The following code opens an existing workbook 
      string workbookPath = "D:\\RSG_D.xls"; // Add your own path here 
      Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, 
       false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, 
       false, 0, true, false, false); 

      // The following gets the Worksheets collection 
      Excel.Sheets excelSheets = excelWorkbook.Worksheets; 

      // The following gets Sheet1 for editing 
      string currentSheet = "Sheet1"; 
      Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet); 

      //declare a variable to hold the CurrentCulture 
      System.Globalization.CultureInfo oldCI; 
      //get the old CurrenCulture and set the new, en-US 
      //void SetNewCurrentCulture() 
      //{ 
      oldCI = System.Threading.Thread.CurrentThread.CurrentCulture; 
      System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); 
      //} 

      int rowCounter = 1; 

      while (rowCounter != null) 
      { 
       Excel.Range countRows = (Excel.Range)excelWorksheet.Cells[rowCounter, 1] as Excel.Range; 
       object CountRows = countRows.Value; 
       rowCounter++; 
       Console.WriteLine(CountRows); 
      } 
      excelWorkbook.Close(0); 
      excelApp.Quit(); 


      //reset Current Culture back to the originale 
      System.Threading.Thread.CurrentThread.CurrentCulture = oldCI; 


     } 
    } 
} 
+0

Возможный дубликат http://stackoverflow.com/questions/1563406/what-is-error-code-0x800a01a8-coming-out-of- excel-activex-call – AlG

+1

что бесконечный цикл я шпион? – msmucker0527

ответ

1

У меня была точно такая же проблема сегодня в: Вот код, который я использовал, и это правильно работает для меня:

 Excel.Application oExcel = new Excel.Application(); 
     //oExcel.Visible = true; (this caused me huge problems 
     Excel.Workbook oBook = oExcel.Workbooks.Open(@"C:\Yoink\Birr Castle Demesne  Interactive Map\Birr Castle Demesne Interactive Map\bin\Debug\Red Tree Trail.xlsx"); 
     Excel.Worksheet oSheet1 = oBook.Worksheets["Red Tree Trail"] as Excel.Worksheet; (use your own worksheet title there) 
     Excel.Range rng = oSheet1.get_Range("A1", "AJ51"); (use your own range there 
     int rowCount = rng.Rows.Count; 
     int colCount = rng.Columns.Count; 

     string[,] tsReqs = new string[rowCount, colCount]; 

     for (int i = 1; i <= rowCount; i++) 
     { 
      for (int j = 1; j <= colCount; j++) 
      { 
       string str = rng.Cells[i, j].Text; 
       tsReqs[i - 1, j - 1] = str; 
      } 
     } 

Я думаю, ваша проблема в этой строке:

Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, 
      false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, 
      false, 0, true, false, false); 
Смежные вопросы