读取Excel工作表使用C#所在的表已合并单元格 [英] Reading Excel Sheet Using C# Where the Table has Merged Cells

查看:280
本文介绍了读取Excel工作表使用C#所在的表已合并单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个使用C#的一些合并单元格的Excel工作表。我在另一篇文章说,从合并的角度code的观点仍然存在单元读取的,但他们只是有空值。所以,我试图跳过空值,但我仍然得到靠不住的结果。这里是code我使用。它应该填充和HTML表从S preadsheet值:

I am trying to read an Excel sheet that has some merged cells using C#. I read in another post that merged cells still existed from the code's point of view but they just had null values. So I have tried skipping null values but I am still getting wonky results. Here is the code I am using. It is supposed to populate and HTML table with values from the spreadsheet:

private void Output_Excel_File()
{
    string inputFileLocation = AppDomain.CurrentDomain.BaseDirectory + "/dashboard.xls";
    DataSet ds = Get_Spreadsheet_Data(inputFileLocation, "Dashboard Statistics");
    if (ds.Tables.Count > 0)
    {
        foreach (DataTable dt in ds.Tables)
        {
            int row = 0;
            foreach (DataRow dr in dt.Rows)
            {
                int col = 0;
                foreach (DataColumn dc in dt.Columns)
                {
                    string cellValue = dr[dc].ToString();
                    cellValue = Regex.Replace(cellValue, "\n", "<br /");
                    if (cellValue == "X" || cellValue == null)
                    {
                        col++;
                        continue;
                    }
                    string index = "r" + row.ToString() + "c" + col.ToString();
                    Literal cellTarget = (Literal)form1.FindControl(index);
                    cellTarget.Text = cellValue;
                    col++;
                }
                row++;
            }
        }
    }
}

在我的特别索引是关闭的,该行:

In particular my indexing is off and the line:

cellTarget.Text = cellValue;

最终总是抛出一个空引用异常,当索引变得不匹配在HTML格式的索引。

always ends up throwing a null reference exception when the indexing becomes mismatched with the indexing in the HTML.

我GOOGLE与谷歌搜索,但我很为难。任何的建议是AP preciated。

I've Googled and Googled but I'm stumped. Any advice is appreciated.

问候。

推荐答案

从我记得,合并单元格的左上角大多数细胞将携带数据。在合并后的集团所有其他单元格引用是空的。

From what I remember, The upper left most cell of merged cells will carry the data. Every other cell reference in the merged group will be empty.

编辑:
如果你想跳过处理r0c1(因为我认为它不存在),如果它是X或Null,那么你就必须做这样的事情:

If you want to skip processing r0c1 (Because I assume it doesnt exist) if it is "X" or Null, then you would have to do something like this:

foreach (DataColumn dc in dt.Columns)
{
    string cellValue = dr[dc].ToString();
    cellValue = Regex.Replace(cellValue, "\n", "<br /"); 
    if (cellValue != "X" | cellValue != null)
    {
        string index = "r" + row.ToString() + "c" + col.ToString();
        Literal cellTarget = (Literal)form1.FindControl(index);
        cellTarget.Text = cellValue; 
    }

    col++;
}

另外,我觉得cellValue永远不会为空,但它可能是空的所以我喜欢用:

if (cellValue != "X" | cellValue.Length == 0) 

EDIT2:

您可能有点混淆您recieving的 NullRefference 例外。从我看到它不是因为 cellValue 为空,这是一个从:

You may be a bit confused with the NullRefference exception that you are recieving. From what I see it not because the cellValue is null, it's from:

(Literal)form1.FindControl(index);

试图找到r0c1我假设不存在,并返回一个空的控制(文字)。所以,当你去使用

trying to find r0c1 which I assume doesn't exist and returns a null control(Literal). So when you go to use

cellTarget.Text = cellValue;

你会得到一个NullReference错误,因为对象本身 cellTarget 为null。

这篇关于读取Excel工作表使用C#所在的表已合并单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆