【问题标题】:How To remove #NA from Excel Range in C#如何在 C# 中从 Excel 范围中删除#NA
【发布时间】:2019-11-02 23:32:42
【问题描述】:

我一直试图弄清楚这一点。但是像thisthis 这样的解决方案并不能解决我的问题。

我正在使用以下代码将数据从数据库添加到 Excel:-

//Adding Table values in Excel
            int sum = 0;
            object[,] arr = new object[result.Rows.Count, result.Columns.Count];
            for (int r = 0; r < result.Rows.Count; r++)
            {
                DataRow dr = result.Rows[r];
                for (int c = 0; c < result.Columns.Count; c++)
                {
                    arr[r, c] = dr[c];
                }
                    sum = sum + Convert.ToInt32(dr[5]);
            }


            Microsoft.Office.Interop.Excel.Range c3 = (Microsoft.Office.Interop.Excel.Range)ws.Cells[20, 1];
            Microsoft.Office.Interop.Excel.Range c4 = (Microsoft.Office.Interop.Excel.Range)ws.Cells[result.Rows.Count + 20, result.Columns.Count];

            Microsoft.Office.Interop.Excel.Range range1 = ws.get_Range(c3, c4);
            range1.Value = arr;

现在在range1.Value = arr; 行上,当我调试代码时,它正在添加另一行,所有列的值都为#N/A,显示的值为-2146826246。

首先,我不明白为什么要在最后添加一行它自己的? 其次,为什么这些值会以#N/A 的形式出现以及如何解决这个问题?

【问题讨论】:

    标签: c# asp.net .net excel


    【解决方案1】:

    当范围大于数组时,Excel 中的 #N/A 错误(德语中的#N/V)会出现。因此,如果myRange 是 [5,5] 而myArray 是 [4,4],则

    myRange.Value = myArray 会返回这个:

    如何避免 - 不要将小数组分配给更大的范围。或者循环遍历范围并检查 # 符号。然后用它做点什么。这是一些“玩”的代码,看看它如何将值从数组分配到范围:

    using System;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace excelTest
    {
        class Program
        {
            static void Main()
            {
                Excel.Application excel = new Excel.Application
                {
                    Visible = true,
                    DisplayAlerts = false
                };
    
                Excel.Workbook wbk = excel.Workbooks.Add(Type.Missing);
                Excel.Worksheet wks = wbk.Worksheets[1];
    
                Excel.Range cellA1 = wks.Cells[1, 1];
                Excel.Range cellE5 = wks.Cells[5, 5];
                Excel.Range myRange = wks.get_Range(cellA1, cellE5);
    
                int[,] myArray = new int[4, 4];
                for (int col = 0; col < myArray.GetLength(0); col++)
                {
                    for (int row = 0; row < myArray.GetLength(1); row++)
                    {
                        myArray[col, row] = (col + row) * 2;
                    }
                }
                myRange.Value = myArray;
            }
    

    这是Excel.Workbook Open

    public static Excel.Workbook Open(Excel.Application excelInstance,
            string fileName, bool readOnly = false, bool editable = true,
            bool updateLinks = true)
    {
        Excel.Workbook book = excelInstance.Workbooks.Open(
            fileName, updateLinks, readOnly,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);
        return book;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      相关资源
      最近更新 更多