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.

Wed May 29, 2024 4:12 pm

Spire XLS version 13.11.4

I am using Spire XLS in my ASP.NET Core application (.NET 7.0) to create a UI that allows a user to download an excel workbook. I have an excel template filed stored in my root folder that the code accesses to generate the workbook from. The excel template has two sheets, with the first containing four textboxes. My goal is to select the textbox and add text to it using the HtmlString method. However, through debugging, I found that Spire does not detect the already existing textboxes because I noticed the count of textboxes in the template was zero.

Here is the code I am using to access the textbox:

XlsTextBoxShape shape = sheet1.TextBoxes[0] as XlsTextBoxShape
shape.HtmlString = "this is a test string";

The reason I want to select the textbox that exists within the template is because I need a specific font and font size preemptively set before I add formatting through the HtmlString method.

jacktorrealba
 
Posts: 6
Joined: Mon Sep 18, 2023 8:25 pm

Thu May 30, 2024 3:39 am

Hi,

Thanks for your inquiry.
I have tested with Spire.XLS 14.5.3 and found that I can successfully detect the Textbox and set the HtmlString. Please try again with the latest version 14.5.3 as it includes more fixes. If you still encounter any issues, please provide us with your template test file for further investigation.
You can attach it here or send it via email to 'support@e-iceblue.com'.
Thank you for your assistance.

Sincerely,
Doris
E-iceblue support team
Attachments
screenshot.png
screenshot.png (58.09 KiB) Viewed 2007 times
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Thu May 30, 2024 2:21 pm

Thanks for the quick response. I tested with the newer version and works. However, I have a follow up question. I want to loop through my dataset and create a new sheet for each record where each sheet is just a copy of that first sheet (essentially the first sheet with the textboxes serves as a template sheet that I want to clone). I noticed that when I perform the same function you did for the newly added sheet, the textbox count comes to 0. Here is my code:


Workbook workbook = new();
workbook.LoadFromFile(filePath);
Worksheet templateSheet = workbook.Worksheets[0];
Debug.WriteLine("Textbox count: " + templateSheet.TextBoxes.Count); // this outputs the correct count of textboxes (4)

for (int i = 0; i < data.Count; i++)
{
Worksheet blank1 = workbook.Worksheets.Add(data[i].ID.ToString());
blank1.CopyFrom(templateSheet);
Debug.WriteLine("Textbox count: " + blank1.TextBoxes.Count); // this outputs a count of 0 textboxes
}

jacktorrealba
 
Posts: 6
Joined: Mon Sep 18, 2023 8:25 pm

Fri May 31, 2024 11:23 am

Hello.

Thank you for your feedback.
I found that saving and reloading after copying the sheet solves the problem, you can test it by referring to the following code:

Code: Select all
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("testTextbox_output.xlsx");
            Worksheet templateSheet = workbook.Worksheets[0];
            Debug.WriteLine("Textbox count: " + templateSheet.TextBoxes.Count);
            Worksheet blank1 = workbook.Worksheets.Add("new");
            blank1.CopyFrom(templateSheet);

            MemoryStream ms = new MemoryStream();
            workbook.SaveToStream(ms);
            workbook.LoadFromStream(ms);

            Debug.WriteLine("Textbox count: " + workbook.Worksheets["new"].TextBoxes.Count);
            workbook.SaveToFile("newTextbox.xlsx");


If there's still any issue, welcome to contact us again.

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Fri May 31, 2024 5:10 pm

That did the trick! I guess I have to get the sheet by using "workbook.Worksheets..." instead of just assigned that to a variable and using the variable name to access its methods.

jacktorrealba
 
Posts: 6
Joined: Mon Sep 18, 2023 8:25 pm

Mon Jun 03, 2024 6:59 am

Hi,

Thank you for your feedback.
I'm glad that this solved your issue. There should be no problem accessing the worksheet through a variable name, but it might be necessary to save the workbook before the worksheet becomes effective. If you have any other questions, please feel free to ask.

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 334
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.XLS