Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8098019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:56:44+00:00 2026-06-05T21:56:44+00:00

I am trying to read an Excel sheet that has some merged cells using

  • 0

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 an 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;

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:56:46+00:00Added an answer on June 5, 2026 at 9:56 pm

    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.

    EDIT:
    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++;
    }
    

    Also I think cellValue will not ever be null, but it may be empty "" so I like to use:

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

    EDIT2:

    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);
    

    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;
    

    you get a NullReference error because the object itself cellTarget is null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read data from an Excel sheet using Office.Interoperability.Excel namespace. I'd like
I'm trying to read data from excel using POI. How can I check if
I am trying to read in an excel sheet using xlrd, but I'm having
Possible Duplicate: Reading Datetime value From Excel sheet I'm trying to read in a
I'm trying to read from excel file with oleDB provider using C#: using (var
I am trying to read data from excel files using datatable. The command select
I'm trying to read an excel file from C# using COM, and can get
I am trying to read data from an excel sheet but there is an
I am trying to read one excel sheet and then creating another excel sheet
I'm trying to read data from excel (xls) sheet. I'm running Windows 7 64-bit,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.