空的Excel文档C#

嗨,我不断从下面的代码中得到这个错误,想知道是否有人可以帮助。

error processing excel file: cannot perform runtime binding on a null reference 

码:

  private void Import_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Show open file dialog box Nullable<bool> result = dlg.ShowDialog(); // Process open file dialog box results if (result == true) { // Open document string filename = dlg.FileName; Microsoft.Office.Interop.Excel.Application vExcelObj = new Microsoft.Office.Interop.Excel.Application(); try { Microsoft.Office.Interop.Excel.Workbook theWorkbook = vExcelObj.Workbooks.Open(filename, Type.Missing, true); Microsoft.Office.Interop.Excel.Worksheet sheet = theWorkbook.Worksheets[1]; string vFirstName = "temp"; string vLastName = "temp"; int vIndex = 1; while (vFirstName != "") { // Change the letters of the appropriate columns here! // In my example, 'A' is first name, 'B' last name vFirstName = sheet.get_Range("A" + vIndex.ToString()).Value.ToString(); // if i take out the exception handling the error is on this line vLastName = sheet.get_Range("B" + vIndex.ToString()).Value.ToString(); this.SaveNewCustomer(vFirstName, vLastName); vIndex++; } } catch (Exception ex) { MessageBox.Show("Error processing excel file : " + ex.Message); } finally { vExcelObj.Quit(); } } } private void SaveNewCustomer(string firstName, string lastName) { string uri = "http://localhost:8002/Service/Customer"; StringBuilder sb = new StringBuilder(); sb.Append("<Customers>"); sb.AppendLine("<FirstName>" + firstName + "</FirstName>"); sb.AppendLine("<LastName>" + lastName + "</LastName>"); sb.AppendLine("</Customers>"); string NewStudent = sb.ToString(); byte[] arr = Encoding.UTF8.GetBytes(NewStudent); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); req.Method = "POST"; req.ContentType = "application/xml"; req.ContentLength = arr.Length; Stream reqStrm = req.GetRequestStream(); reqStrm.Write(arr, 0, arr.Length); reqStrm.Close(); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); reqStrm.Close(); resp.Close(); } } 

代码只需要一个Excel文档,并尝试将数据发送到我的Web服务。

所以我尝试使用下面的方法,但它冻结了应用程序:S没有错误只是挂起。 编辑尝试:

  while (vFirstName != "") { var columnACell = sheet.get_Range("A" + vIndex.ToString()); var columnBCell = sheet.get_Range("B" + vIndex.ToString()); var columnACellValue = columnACell.Value; var columnBCellValue = columnBCell.Value; if (columnACellValue != null && columnBCellValue != null) { vFirstName = columnACellValue.ToString(); vLastName = columnBCellValue.ToString(); this.SaveNewStaff(vFirstName, vLastName); //, vPassword vIndex++; } } } 

在这里输入图像说明

编辑2

只是拿了代码,并通过它。 发现问题。 我想我误解了原来发生的事情。

发生了什么事情是, while (vFirstName != "")将继续前进,直到vFirstName是一个空string。 但是这绝不会发生! 原因如下:

  • 只要列A和列B将有价值,一切都会好起来的。 该代码将按预期行事。
  • 当代码到达一个没有值的Excel行时,它会打到一个空白的单元格,这个单元格的值将被设置为null 。 这导致exception。

所以真正的解决scheme是让循环继续下去,直到它击中一个null值的单元格,然后退出。 有点像这样:

 while (true) { // Split the satements var columnACell = sheet.get_Range("A" + vIndex.ToString()); var columnBCell = sheet.get_Range("B" + vIndex.ToString()); var columnACellValue = columnACell.Value; var columnBCellValue = columnBCell.Value; if (columnACellValue != null && columnBCellValue != null) { vFirstName = columnACellValue.ToString(); vLastName = columnBCellValue.ToString(); } else { break; } this.SaveNewCustomer(vFirstName, vLastName); vIndex++; }; 

刚刚在我的最后testing了这个,它似乎工作。

另外,请确保您完全退出Excel,因为调用Excel.Quit()通常是不够的。 打开任务pipe理器,并检查是否有额外的EXCEL.exe实例浮动。 为了防止那些我通常在完成它之后杀死Excel(比正确释放Excel的COM对象更容易),如本文所述。


原来的post

这听起来像这里有几个选项:

  • 单元格是空的,这意味着.Value将是null
  • sheetnull
  • get_Range()返回null – 这听起来不太可能。

将这一行拆分成单独的语句,并查看哪一个引发错误。 这将告诉你在哪里看得更远。

从你正在做的事情来看 – search列,直到find名字 – 这听起来像是在单元格的Value内部遇到空Value 。 为了解决这个问题,我通常添加一个快速的if语句来testingnull

编辑

下面是一个例子(可能不会编译),希望能够修复单元格内的null值,并帮助查明其他与null相关的问题。 用这样的东西replace违规行:

 var columnACell = sheet.get_Range("A" + vIndex.ToString()); var columnBCell = sheet.get_Range("B" + vIndex.ToString()) var columnACellValue = columnACell.Value; var columnBCellValue = columnBCell.Value; if (columnACellValue != null && columnBCellValue != null) { vFirstName = columnACellValue.ToString(); vLastName = columnBCellValue.ToString(); } 

请注意,我假定您的C#编译器支持通过var隐式静态types。

工作表是空的。 也许只有1个工作表? 你正在使用工作表[1],当然返回第二个.. 🙂

尝试拆分读取值的代码,并testing它是否为空。

 object oName = sheet.get_Range("A" + vIndex.ToString()).Value; vFirstName = (oName == null ? string.Empty : oName.ToString(); object oLast = sheet.get_Range("B" + vIndex.ToString()).Value; vLastName = (oLast == null ? string.Empty : oLast.ToString()); if(vFirstName.Length > 0 && vLastName.Length > 0) this.SaveNewCustomer(vFirstName, vLastName); 

并注意到,在您的SaveNewStaff /客户(….)您closuresRequestStream两次。 也许第二次closures冻结你的代码。

  reqStrm.Close(); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); reqStrm.Close(); // <= Already closed before?