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); }
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; } }
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!
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.
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