Page Break Doesn't work correctly

Hi,

I am trying to use your code to insert page breaks and ensure it fits correct page width (1 page width). Somehow your code with 50 rows for a page break work, but if you try to use different page size (change 50 to 25) then it doesn't work as expected. Please advise what is the correct way to achive this?

What I am trying to do is write a set of rows (say 45) into a sheet and then insert a horizontal page break. This is not working as expected.


Dim rowCtr, column As Integer

            column = 10


            For i As Integer = 0 To 499

                rowCtr += 1

                worksheet(rowCtr, 1).Text = "Page Break applied after every 50 Lines. " + i.ToString()


                If rowCtr Mod 25 = 0 Then

                    worksheet.HPageBreaks.Add(worksheet(rowCtr, column))

                End If

            Next

            worksheet.PageSetup.FitToPagesTall = 5

           worksheet.PageSetup.FitToPagesWide = 1


Thanks!

Sunil


6 Replies

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team January 17, 2022 12:45 PM UTC

Hi Sunil, 

Greetings from Syncfusion. 

We are checking the reported query at our end and will share you the details tomorrow (January 18th,2022). Meanwhile, it would be helpful for us if you could share the Syncfusion XlsIO version you are using at your end. 

Regards, 
Keerthi. 



SU Sunil January 17, 2022 04:07 PM UTC

Hi Keerthi,

I am using 19.4.0.41



SU Sunil January 18, 2022 05:41 AM UTC

Hi,


I have two more questions.

1) I am importing datatable into Spreadsheet. While doing that, i have some numeric values that I need to insert as is. For example 01234, while importing this trailing zero is gone as excel treating as number. How can i treat as text?

2) Writing data to Excel cell. Need to format the string in different ways. Example below

This is a sentence I need, but few words should be in bold in a cell.

Is there a way I can write a sentence to a single cell?


Thanks!

Sunil



KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team January 18, 2022 10:18 AM UTC

Hi Sunil, 

We appreciate your patience. 

Thanks for sharing the version details as requested. Please find the response for your queries below. 

Query 
Response 
I am trying to use your code to insert page breaks and ensure it fits correct page width (1 page width). Somehow your code with 50 rows for a page break work, but if you try to use different page size (change 50 to 25) then it doesn't work as expected. Please advise what is the correct way to achive this? 

What I am trying to do is write a set of rows (say 45) into a sheet and then insert a horizontal page break. This is not working as expected. 

Dim rowCtr, column As Integer 
            column = 10 

            For i As Integer = 0 To 499 
                rowCtr += 1 
                worksheet(rowCtr, 1).Text = "Page Break applied after every 50 Lines. " + i.ToString() 

                If rowCtr Mod 25 = 0 Then 
                    worksheet.HPageBreaks.Add(worksheet(rowCtr, column)) 
                End If 
            Next 
            worksheet.PageSetup.FitToPagesTall = 5 
           worksheet.PageSetup.FitToPagesWide = 1 

FitToPagesTall and FitToPagesWide splits the content in the worksheet to fit in that provided count. 

If your main requirement is to set page break at specified row, we suggest you to use the below modified code snippet. 

Code Snippet: 

int rowCtr = 0; 
int column = 1; 
for (int i = 1; (i <= 500); i++) 
{ 
    rowCtr++; 
    worksheet[rowCtr, 1].Text = "Line - " + i.ToString(); 
    if ((rowCtr % 40) == 0) 
    { 
        worksheet.HPageBreaks.Add(worksheet[rowCtr+1, column]); 
        } 
    } 


I am importing datatable into Spreadsheet. While doing that, i have some numeric values that I need to insert as is. For example 01234, while importing this trailing zero is gone as excel treating as number. How can i treat as text? 
The leading zeros can be preserved by assigning text number format to the cell. For this first you need to set the number format and then import data into the cell

Please look into the following link for more information. 

Writing data to Excel cell. Need to format the string in different ways. Example below 
This is a sentence I need, but few words should be in bold in a cell. 
Is there a way I can write a sentence to a single cell? 

Your requirement can be achieved through Rich-Text concept in Syncfusion XlsIO. Please look into the following link for more information. 


Kindly try the suggestions and let us know if these helps. 

Regards, 
Keerthi. 



SU Sunil January 19, 2022 08:25 PM UTC

Hi Keerthi,


All solutions suggested by you works well! Thanks a lot for your help! For pagebreak, at one place of code I was setting the FitToPagesTall to 999, which was overriding the page breaks. Removing that solved the issue. I can still keep below attributes, if that helps someone.


worksheet.PageSetup.FitToPagesTall = 0

worksheet.PageSetup.FitToPagesWide = 1


Thnk




KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team January 20, 2022 04:02 PM UTC

Hi Sunil, 
  
We are glad that the provided suggestion helped you and thank you for pointing out the properties that resolved the issue. Kindly let us know if you need any further assistance. 
  
Regards, 
Keerthi. 


Loader.
Up arrow icon