加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#-4.0 – 如何使用C#.net中的EXCEL interop api读取空单元格值

发布时间:2020-12-15 07:58:21 所属栏目:百科 来源:网络整理
导读:如果我尝试读取空的EXCEL单元格,则会收到System.com_object错误.我的代码是: public static ListOrderPC getFilters(string fileCheckout) { ListOrderPC orderPCs = new ListOrderPC(); XLDoc sldoc = new XLDoc(); string localPath = @"C:TempPCs.xlsx
如果我尝试读取空的EXCEL单元格,则会收到System.com_object错误.我的代码是:
public static List<OrderPC> getFilters(string fileCheckout)
    {
        List<OrderPC> orderPCs = new List<OrderPC>();
        XLDoc sldoc = new XLDoc();

        string localPath = @"C:TempPCs.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:无法对空引用执行运行时绑定
在CallSite.Target(Closure,CallSite,Object)
在System.Dynamic.UpdateDelegates.UpdateAndExecute1 [T0,TRet](CallSite站点,T0 arg0)

“**我的COM没有任何线索.非常感谢帮助.

解决方法

添加另一张支票
if (mWSheet1.Cells[z,y + 3].Value2 != null)

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

orderPc.ManufactureDate = Convert.ToString(mWSheet1.Cells[z,y + 3].Value2);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读