2013-04-26 12 views
7

pracuję z EPPlus Biblioteki i napisał ten kod (tylko dla testu)wartość odczytaną z pliku programu Excel przy użyciu EPPus przyczynę „Wiersz poza zasięgiem” błędu

private void btnCargarExcel_Click(object sender, EventArgs e) 
    { 
     if (this.openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      if (System.IO.File.Exists(openFileDialog1.FileName)) 
      { 
       Stopwatch stopWatch = new Stopwatch(); 
       stopWatch.Start(); 
       Thread.Sleep(10000); 

       filePath.Text = openFileDialog1.FileName.ToString(); 

       var existingFile = new FileInfo(openFileDialog1.FileName.ToString()); 

       using (var package = new ExcelPackage(existingFile)) 
       { 
        ExcelWorkbook workbook = package.Workbook; 
        if (workbook != null) 
        { 
         if (workbook.Worksheets.Count > 0) 
         { 
          ExcelWorksheet currentWorkSheet = workbook.Worksheets.First(); 
          textBox3.Text = currentWorkSheet.Cells[0, 1].Value.ToString(); 
         }        
        } 
       } 

       stopWatch.Stop(); 

       TimeSpan ts = stopWatch.Elapsed; 
       executiontime.Text = 
        String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, 
            ts.Milliseconds/10).ToString(); 
      } 
      else 
      { 
       MessageBox.Show("No se pudo abrir el fichero!"); 
       System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel); 
       appExcel = null; 
       System.Windows.Forms.Application.Exit(); 
      } 
     } 
    } 

aby sprawdzić, czy mój kod działa porządku dodam tę linię textBox3.Text = currentWorkSheet.Cells[0, 1].Value.ToString(); (pojawia się pomysł z here), ale kiedy uruchomić aplikację otrzymuję ten błąd:

Row out of range

dlaczego tak jest? Czego mi brakuje lub robię źle? To StackTrace do błędu:

System.ArgumentException was unhandled HResult=-2147024809 Message=Row out of range Source=EPPlus StackTrace: 
     at OfficeOpenXml.ExcelRange.ValidateRowCol(Int32 Row, Int32 Col) 
     at OfficeOpenXml.ExcelRange.get_Item(Int32 Row, Int32 Col) 
     at WindowsFormsApplication1.ExcelDBUserControl.btnCargarExcel_Click(Object sender, EventArgs e) in d:\Private\Dropbox\Work\ClanMovil21Comunicaciones\Apps\WindowsFormsApplication1\WindowsFormsApplication1\ExcelDBUserControl.cs:line 74 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at WindowsFormsApplication1.Program.Main() in d:\Private\Dropbox\Work\ClanMovil21Comunicaciones\Apps\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 20 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() InnerException: 

Również i aditionally (nie wiem, czy to lepiej, aby otworzyć nowe pytanie tylko do tego) i związane z tym samym tematem, w jaki sposób można uzyskać nazwę (tekst) każdego arkusza roboczego, a następnie przycisku opcji kompilacji, ponieważ wiele skoroszytów znajduje się w moim skoroszycie?

+3

1-indeks bazowy, może – mcalex

+0

@mcalex Nie jestem ekspertem C#, więc nie podążać za tobą, czy mógłbyś wyjaśnić mi lepiej? – Reynier

Odpowiedz

20

Pierwsza komórka w arkuszu programu Excel to [1,1]. currentWorkSheet.Cells [0, 1] może być przyczyną tego wyjątku, komórka [0, 1] jest poza zakresem

+2

Dziękuję bardzo .. Szukałem ostatnich 3 godzin .. – Sayka

Powiązane problemy