【发布时间】:2013-04-29 15:09:45
【问题描述】:
当工作表不存在时,我正在尝试使用此代码处理异常:
int k = blYear.SelectedIndex;
ExcelWorksheet currentWorkSheet;
try
{
currentWorkSheet = workbook.Worksheets[k + 1];
}
catch (Exception)
{
MessageBox.Show("La hoja de trabajo no existe en el archivo de Excel", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
btnCargarExcel.Enabled = true;
blYear.Enabled = true;
filePath.Text = "";
}
但是我收到了这个错误:
-
如果我尝试编译出现这个错误
错误 1 使用未分配的局部变量 'currentWorkSheet' d:\Work\ClanMovil\CMApp\WindowsFormsApplication1\WindowsFormsApplication1\ExcelDBUserControl.cs 71 46 CMApp
如果我忽略错误并继续构建,那么我会得到另一个
--------------- Microsoft Visual Studio --------------------------- 源文件:D:\Work\ClanMovil\CMApp\WindowsFormsApplication1\WindowsFormsApplication1\ExcelDBUserControl.cs
模块: d:\Work\ClanMovil\CMApp\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\CMApp.exe
进程:[4944] CMApp.vshost.exe
源文件与构建模块时不同。你会 像调试器一样使用它吗? 是的 没有
System.Collections.Generic.KeyNotFoundException 未处理
HResult=-2146232969 消息=给定的密钥不存在于 字典。来源=mscorlib 堆栈跟踪: 在 System.Collections.Generic.Dictionary`2.get_Item(TKey 键) 在 OfficeOpenXml.ExcelWorksheets.get_Item(Int32 PositionID) 在 WindowsFormsApplication1.ExcelDBUserControl.btnCargarExcel_Click(对象 发件人,EventArgs e) 在 d:\Work\ClanMovil\CMApp\WindowsFormsApplication1\WindowsFormsApplication1\ExcelDBUserControl.cs:line 66 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 原因, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 d:\Work\ClanMovil\CMApp\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 中的 WindowsFormsApplication1.Program.Main() 20 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 保留SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 回调, 对象状态, Boolean 保留SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:
处理这个问题的正确方法是什么?
编辑第一个错误 好的,看到有关未声明的 var 的错误,我对代码进行了一些修改,现在是这样的:
using (var package = new ExcelPackage(existingFile))
{
ExcelWorkbook workbook = package.Workbook;
ExcelWorksheet currentWorkSheet = workbook.Worksheets.First();
if (workbook != null)
{
if (workbook.Worksheets.Count > 0)
{
int k = blYear.SelectedIndex;
try
{
currentWorkSheet = workbook.Worksheets[k + 1];
}
catch (Exception)
{
MessageBox.Show("La hoja de trabajo no existe en el archivo de Excel", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
btnCargarExcel.Enabled = true;
blYear.Enabled = true;
filePath.Text = "";
}
}
}
}
但这有一个问题,即使 Exception 被启动,因为这个ExcelWorksheet currentWorkSheet = workbook.Worksheets.First(); 而加载了第一个工作表,这不是我想要实现的。如果我将 var 定义为 ExcelWorksheet currentWorkSheet; 并将值设置在第一个条件内,则会出现相同的错误:
错误 1 使用未分配的局部变量 'currentWorkSheet' d:\Work\ClanMovil\CMApp\WindowsFormsApplication1\WindowsFormsApplication1\ExcelDBUserControl.cs 71 46 CMApp
出现所以我不知道如何处理,我被困在这里
【问题讨论】:
-
WorkBook 或 Excel 应用程序的实例在哪里?尝试查看此链接以获得简单的修复。 stackoverflow.com/questions/1111935/c-sharp-and-excel-interop
-
@DJKRAZE 我对我的帖子进行了一些编辑,不,你留给我的链接没有帮助
-
ExcelWorksheet currentWorksheet = null工作吗? -
@HjalmarZ 是的,它完成了这项工作
标签: c# .net exception exception-handling