Setting Page Breaks on Empty Rows

Dear Support,

I have an Excel Report that contains blocks of data separated by an empty row. I wrote logic to traverse through every row and check the first cell to see if it’s empty. The logic works fine, however I’m not able to create a PAGE BREAK on the empty cell. What am I doing wrong?

The following is the code segment:

for(int i = 0; i <= designer.Excel.Worksheets[0].Cells.MaxRow; i++) {
if (designer.Excel.Worksheets[0].Cells[i,0].Value.ToString() == “”)
designer.Excel.Worksheets[0].HPageBreaks.Add(i,0);
}


Thanks,
Tom Raic

I addition… I’m getting a Null Object Reference Exception when I call the Page Break function.

Actually the error is… “Object reference not set to an instance of an object.”

Which version are you using? That's a bug in earlier version. Please download and try v3.3.3 .

I am running version 3.3.0. Are you sure I’m not missing anything from my code? It does not recognize the HPageBreaks collection. Why does it work for others?

for(int i = 0; i <= designer.Excel.Worksheets[0].Cells.MaxRow; i++) {
try {
if (designer.Excel.Worksheets[0].Cells[i,0].Value.ToString() == “”)
{
designer.Excel.Worksheets[0].HPageBreaks.Add(i,0);
}
}
catch (Exception ex) {
throw ex;
}
}

You code is fine. I have tested it and don’t find any problem. Please try v3.3.3.

Can you send me the Excel file that you tested my code with. I have 3.3.0 already installed. You’re not giving me productive assistance. If you can’t fix this today, I’ll be forced to refund the product, and probably use another product, like SyncFusion. You told me to download the same installer twice that I have on my box already. THAT"S NOT HELPFUL AT ALL!

Angry Customer,
Garvan Interactive, LLC.

Can you send me the exact source code and Excel file that you used to test this? If it really works, send me your project file!

Sorry for the frustrations… I’m just trying to make one of my clients happy.

I FIXED IT MYSELF. The code segment i gave you DOES NOT WORK! So if you claimed you tested it, you didn’t. The Value of a Cell needs to test against a NULL value. If you see my code, you will get an error when you use the ToString function. I know you guy’s are busy over there, but if you’re goign to service a customer, do it right. You shouldn’t tell your customers that their code is right, and then lie to them about testing it yourself.

Not cool!

Sorry for any inconvience.

I did test your code. The following is my whole program and the attached file is my template file.

ExcelDesigner designer = new ExcelDesigner();

designer.Open("d:\\book1.xls");

for(int i = 0; i <= designer.Excel.Worksheets[0].Cells.MaxRow; i++)
{
try
{
if (designer.Excel.Worksheets[0].Cells[i,0].Value.ToString() == "")
{
designer.Excel.Worksheets[0].HPageBreaks.Add(i,0);
}
}
catch (Exception ex)
{
throw ex;
}
}


designer.Save("d:\\result.xls");


The above code works fine and I did find a similar problem in earlier version. So I had thought it's fixed in the new version and didn't make further investigation.

Anyway, I apologize for my carelessness. We will shape our testing and support process to serve our customers better.

@traic1,
Aspose.Excel is discontinued and no more under active development now. It is replaced by Aspose.Cells that is much advanced and contains all the latest features available in different versions of MS Excel along with the support to the features of its predecessor product. You can set line breaks and text wrapping easily as demonstrated in the following sample code:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET

// Create Workbook Object
Workbook wb = new Workbook();

// Open first Worksheet in the workbook
Worksheet ws = wb.Worksheets[0];

// Get Worksheet Cells Collection
Aspose.Cells.Cells cell = ws.Cells;

// Increase the width of First Column Width
cell.SetColumnWidth(0, 35);

// Increase the height of first row
cell.SetRowHeight(0, 36);

// Add Text to the Firts Cell
cell[0, 0].PutValue("I am using the latest version of Aspose.Cells to test this functionality");

// Make Cell's Text wrap
Style style = cell[0, 0].GetStyle();
style.IsTextWrapped = true;
cell[0, 0].SetStyle(style);

// Save Excel File
wb.Save(dataDir+ "WrappingText.out.xlsx");

You may refer to the following article to get more details about setting line breaks and text wrapping.
Line Breaks and Text Wrapping

Download the latest free trial version here to test the product features:
Aspose.Cells for .NET (Latest Version)

You may get a runnable solution here that can be used to test the new product features without any coding.