2015-09-01 61 views
1

我試圖從Excelhelp中使用Filehelpers ExcelNPOIStorage提取數據。因此,我創建了一個類:如何使用C#/ FileHelpers從Excel文件中提取數據ExcelNPOIStorage

public static class UalExcelReader 
    { 
     public static UalShipmentRecord[] ReadInput(String pathToFile) 
     { 
      var provider = new ExcelNPOIStorage(typeof (UalShipmentRecord)) 
      { 
       StartRow = 2, 
       StartColumn = 1, 
       FileName = pathToFile 
      }; 
      var res = (UalShipmentRecord[]) provider.ExtractRecords(); 
      return res; 
     } 
    } 

,當然還有模型類:

[DelimitedRecord("|")] 
public class UalShipmentRecord 
{ 
    public string contentofcol1; 
    public string contentofcol2; 
    ... 

} 

但我得到()的IndexOutOfRangeException調用ExtractRecords:

System.IndexOutOfRangeException was unhandled 
    HResult=-2146233080 
    Message=Index was outside the bounds of the array. 
    Source=FileHelpers 
    StackTrace: 
     at FileHelpers.RecordOperations.ValuesToRecord(Object[] values) 
     at FileHelpers.DataLink.DataStorage.ValuesToRecord(Object[] values) 
     at FileHelpers.ExcelNPOIStorage.ExcelNPOIStorage.ExtractRecords() 
     at Test.Controller.UalExcelReader.ReadInput(String pathToFile) in c:\TEMP\test\Test\Test\Test\Controller\UalExcelReader.cs:line 17 
     at Test.App.OnStartup(StartupEventArgs eventArgs) in c:\TEMP\test\Test\Test\Test\App.xaml.cs:line 23 
     at System.Windows.Application.<.ctor>b__1(Object unused) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
     at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(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.Windows.Threading.DispatcherOperation.Invoke() 
     at System.Windows.Threading.Dispatcher.ProcessQueue() 
     at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
     at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
     at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
     at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
     at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
     at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
     at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
     at System.Windows.Threading.Dispatcher.Run() 
     at System.Windows.Application.RunDispatcher(Object ignore) 
     at System.Windows.Application.RunInternal(Window window) 
     at System.Windows.Application.Run(Window window) 
     at System.Windows.Application.Run() 
     at Test.App.Main() in c:\TEMP\test\Test\Test\Test\obj\Debug\App.g.cs:line 0 
     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: 

我是否正確地使用它?有一個我可以看的例子嗎?

+0

請發佈完整的例外,包括其呼叫幀。你可以簡單地通過調用Exception.ToString()來獲得它。 –

+0

我加了異常。 – hot33331

+0

你能提供一個測試項目和示例文件來演示這個嗎? – netniV

回答

1

1)當您的Excel表格中有空白單元格時,可能會發生此錯誤。這似乎是ExcelNPOIStorage檢索給定行的值的一個潛在問題。

NPOI正在使用NPOI.CellWalk來遍歷每行中的單元格,這看似跳過空白單元格。結果Values數組比預期的空白單元的數量短。

它看起來像一個不同的方法是必需的,這裏提到:http://poi.apache.org/spreadsheet/quick-guide.html#Iterator

2)的東西時,沒有這樣的由具有不正確StartRowStartColumn值可引起空白單元格。

相反的StartRowStartColumn智能感知意見,爲ExcelNPOIStorage他們以0(而在ExcelStorage他們是基於1)

來源:https://github.com/MarcosMeli/FileHelpers/blob/master/FileHelpers.ExcelNPOIStorage/Test/Program.cs

鑑於我所遇到的問題,我將第二個評論上面,並使用更可靠的舊ExcelStorage類,其缺點是它依賴於Excel Interop。

相關問題