我正在尝试使用 Filehelpers ExcelNPOIStorage 从 Excel 工作表中提取数据。因此我创建了一个类:
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;
...
}
但我在调用 ExtractRecords() 时遇到 IndexOutOfRangeException:
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:
我是否正确使用它?有没有我可以看的例子?
最佳答案
1) 当您的 Excel 工作表中有空白单元格时,可能会发生此错误。这似乎是 ExcelNPOIStorage
检索给定行的值的方式中的一个潜在问题。
NPOI 使用 NPOI.CellWalk
遍历每行中的单元格,这似乎会跳过空白单元格。结果,Values
数组比预期的空白单元格数短。
看起来需要一种不同的方法,如此处所述:http://poi.apache.org/spreadsheet/quick-guide.html#Iterator
2) StartRow
和 StartColumn
值不正确会导致空白单元格不存在。
与 StartRow
和 StartColumn
的智能注释相反,对于 ExcelNPOIStorage
它们是从 0 开始的(而在 ExcelStorage
它们是基于 1 的)
来源:https://github.com/MarcosMeli/FileHelpers/blob/master/FileHelpers.ExcelNPOIStorage/Test/Program.cs
考虑到我遇到的问题,我赞成上面的评论并使用更可靠的旧 ExcelStorage
类,缺点是它依赖于 Excel Interop。
关于c# - 如何使用 C#/FileHelpers ExcelNPOIStorage 从 Excel 文件中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32331926/