Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Mar 01, 2018 1:58 pm

So i am trying to insert data into a cell.
I have different sheets having different types of data that need to be inserted.

I'm having difficulties in verifying a specific name exists on a specific sheet.

EX:

I have 2 sheets MTR and UPS

They both have name, rating etc named as MTR_NAME, MTR_RATING etc and UPS_NAME, UPS_RATING

But the MTR one has volts, namedas MTR_VOLTS which doesn't have a value in the UPS sheet, so UPS_VOLTS does not exist

Is there an easy way to verify that a certain name exists on a certain sheet, as the names are generated dynamically by the sheet name.

Basically i got a function that inserts data if the name exists but if the name doesn't exist i have to throw the error out, i want a cleaner way to do this, without relying on a catch clause to do the work.

Thank you

Conisor
 
Posts: 5
Joined: Fri Sep 01, 2017 7:51 am

Fri Mar 02, 2018 8:29 am

Hello,

Thanks for your inquiry.
Below is sample code for your kind reference.
Code: Select all
Workbook wb = new Workbook();
//Get the named range "MTR_VOLTS" in "UPS" sheet
INamedRange nameRange = wb.Worksheets["UPS"].Names.GetByName("MTR_VOLTS");
//If nameRange is null, it indicates the "MTR_VOLTS" doesn't exist in "UPS".
if (nameRange!= null)
{
      //insert data
      //...
}
else
{
       Console.WriteLine("The name doesn't exist in UPS!");
}

If there is any question, welcome to get it back to us.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Wed Mar 07, 2018 9:24 am

Hello,

Greetings from E-iceblue.
Did we resolve your issue?
Thanks in advance for your valuable feedback and time.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Thu Mar 08, 2018 3:40 pm

Hello Nina,

sorry but i can't seem to find a way to insert data into a INamedRange type of object. If i set the value in the nameRange (from your example) the code just throws an error. And doesn't do anything>

Can you give me an example of how you set the value for an INamedRange type of object?

Conisor
 
Posts: 5
Joined: Fri Sep 01, 2017 7:51 am

Fri Mar 09, 2018 6:08 am

Hello,

Thanks for your feedback.
Below is sample code for your kind reference. If this is not you want, please provide your input and expect documents to help us have a better investigation on your requirement. In addition, I use the latest Spire.XLS Pack(Hotfix) Version:8.3.0 to test.
Code: Select all
Workbook wb = new Workbook();
wb.LoadFromFile("Input.xlsx");
//Get the named range "MTR_NAME" in "MTR" sheet
INamedRange nameRange = wb.Worksheets["MTR"].Names.GetByName("MTR_NAME");
if (nameRange!=null)
{
       //Get range
       CellRange[] ranges = nameRange.RefersToRange.Cells;
       for (int i = 0; i < ranges.Length; i++)
       {
            //Insert data
            ranges[i].Value = "Val" + i;
       }
}
else
{
        Console.WriteLine("The name doesn't exist in MTR!");
}
wb.SaveToFile("Result.xlsx", ExcelVersion.Version2013);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Fri Mar 16, 2018 10:22 am

Hello,

Greetings from E-iceblue.
How is your issue now?
Your feedback will be greatly appreciated.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Tue Mar 20, 2018 10:03 am

Hello Nina,

Thank you for your time.

I was unable to fix this.

It works ok like i said in my original post but i didn't want to rely on catch statements to make it work.

The problem is that i'm either using namerange or a cell name,

So example either using MTR_VOLTS or A1. That's what's passed into the method.

So the examples you gave me works for one case and not another. Either that or i'm not understand this properly.

Thank you.

Conisor
 
Posts: 5
Joined: Fri Sep 01, 2017 7:51 am

Tue Mar 20, 2018 10:18 am

Hello,

Thanks for your feedback.
Could you please provide your input and expected Excel files? We will look into them and guide you accordingly.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.XLS