Free Support Forum -

Formatting Cell Width for Report/Print layout

I am creating a “Report” worksheet and need the “body” row cells to be a specific width based on the data in each row.

How do I format a specific series of row cells without affecting the Top Header / Bottom Footer rows?

See the “TallyReport” sheet in the attached workbook.
Notice that the “Defect” column is too narrow and the “No.” & “Length” columns are to wide.



Well, you may unprotect the worksheet (as it is protected) and then auto-fit the columns (e.g C column = Defect field) for your needs. Also, you may adjust the columns’ width accordingly if you do not want them to be auto-fitted.

See the topics for your reference:

OK, But…

Now I need to format my “Report” sheet so that when it is Printed (Rendered) via the SheetRender class that each “page” prints on a separate sheet of paper.

I need to see how to control Page Breaks for the “Report” sheet I am creating in code.
How can I do this?

See the attached “TallyReport” sheet. I tried to add blank lines between the “pages” but that’s not working and may be problematic with differing printer page margins…



Thanks for the sample file.

Well, I am afraid (as I could understand your needs) there is no concrete or better logical way to accomplish your custom needs, you have to use your own code and insert your desired line breaks at your regular specified positions with Scaling the Sheet’s Page Setup options accordingly. I have written a sample code, you may write your own way to do that. Please see the attached input and output files for your reference.

Sample code:

Workbook workbook = new Workbook(“e:\test2\input_file.xlsx”);
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
Aspose.Cells.Cell cell, prevcell = null, lastcell;
FindOptions findOptions = new FindOptions();
findOptions.CaseSensitive = true;
string stringtofind = “PIPE TALLY”;
int cnt = 0;
cell = cells.Find(stringtofind, prevcell, findOptions);
if (cell == null)
{ break; }
lastcell = cell;
if (lastcell.Row == 0)
prevcell = cell;
int row = lastcell.Row;
int col = CellsHelper.ColumnNameToIndex(“M”);
worksheet.HorizontalPageBreaks.Add(row - 1, 0, col);
prevcell = cell;
} while (cell != null);

worksheet.PageSetup.Zoom = 80;


Hope, this helps.

Thank you.

I just added the “worksheet.HorizontalPageBreaks.Add” code to my code and it works great!
One line of code fixed it!

Thanks Again, Jean.


Good to know that your issue is sorted out.

Feel free to contact us any time if you need further help or have any issue.

Thank you.