8

如果我尝试读取空的 EXCEL 单元格,则会收到 System.com_object 错误。我的代码是:

public static List<OrderPC> getFilters(string fileCheckout)
    {
        List<OrderPC> orderPCs = new List<OrderPC>();
        XLDoc sldoc = new XLDoc();

        string localPath = @"C:\Temp\PCs.xlsx";

        Microsoft.Office.Interop.Excel.Application oXL=null;
        Microsoft.Office.Interop.Excel.Workbook mWorkBook=null;
        Microsoft.Office.Interop.Excel.Worksheet mWSheet1=null;
        Microsoft.Office.Interop.Excel.Range xlRange=null;
        try
        {
            oXL = new Microsoft.Office.Interop.Excel.Application();

            mWorkBook = oXL.Workbooks.Open(localPath);

            mWSheet1 = mWorkBook.Sheets[1];

            xlRange = mWSheet1.UsedRange;

            foreach (Microsoft.Office.Interop.Excel.Hyperlink hl in xlRange.Hyperlinks)
            {

               int y = hl.Range.Column;

                int z = hl.Range.Row;

                string vFilter = mWSheet1.Cells[z, y + 1].Value2.Trim();

                if (vFilter.CompareTo("Weekly") == 0)
                {
                    String baseUri = "http://xxx.yyy.net?";
                    int followUpIndex = baseUri.Length;
                    OrderPC orderPc = new OrderPC();
                    orderPc.ProductClass = hl.TextToDisplay.Trim();
                    orderPc.HyperLink = hl.Address.Trim().Substring(followUpIndex);
                    orderPc.SpecType = mWSheet1.Cells[z, y - 1].Value2.Trim();
                     if (mWSheet1.Rows[z].Cells[y + 3] != null || mWSheet1.Rows[z].Cells[y + 3].Value2 != string.Empty)
                        {
                            orderPc.ManufactureDate = mWSheet1.Cells[z, y + 3].Value2.ToString(); //Here is the error**
                        }
                    //Console.WriteLine(orderPc.ProductClass+"----"+orderPc.HyperLink);

                    orderPCs.Add(orderPc);
                }
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Marshal.FinalReleaseComObject(xlRange);
            Marshal.FinalReleaseComObject(mWSheet1);

            mWorkBook.Close(Type.Missing, Type.Missing, Type.Missing);
            Marshal.FinalReleaseComObject(mWorkBook);

            oXL.Quit();
            Marshal.FinalReleaseComObject(oXL);
        }
        return orderPCs;
    }

这个 excel 文件有 10 列,我想我正在读取一个有效的单元格。错误是**”

{Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)

"** 自从它的 COM 以来,我没有任何线索。非常感谢您的帮助。

4

2 回答 2

15

添加另一张支票

if (mWSheet1.Cells[z, y + 3].Value2 != null)

或使用以下代码转换为字符串,因为如果 Value2 为 null 则不会失败

orderPc.ManufactureDate = Convert.ToString(mWSheet1.Cells[z, y + 3].Value2);
于 2012-11-15T15:56:13.020 回答
0

您正在检查您的单元格是否不为空,或者不是 string.empty。您的单元格很可能不是 null 或不是 string.emtpy。

您需要使用 && 而不是 ||,因为您只需要 true,如果您的单元格不是 null 并且不是 string.empty

于 2021-03-16T04:55:08.307 回答