Hi Team,
I have a request in my project that I need to apply custom formatting for the columns.
But I would come to know the formatString only after looping through all the cells. That is only after completing the entire flow in startCell.
So is there a way I can apply the column formatting after looping through all the cells ?
Please advise.
PFB the demo program in which I am trying to apply the column formatting.
1. Demo.java
package com.test.aspose.table;
import com.aspose.cells.Cells;
import com.aspose.cells.OoxmlSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
public class Demo {
public static void main(final String[] args) throws Exception {
// Instantiate a new Workbook
final Workbook wb = new Workbook();
// set the sheet count
final int sheetCount = 1;
// set the number of rows for the big matrix
final int rowCount = 5;
// specify the worksheet
for (int k = 0; k < sheetCount; k++) {
Worksheet sheet = null;
if (k == 0) {
sheet = wb.getWorksheets().get(k);
sheet.setName("test");
} else {
final int sheetIndex = wb.getWorksheets().add();
sheet = wb.getWorksheets().get(sheetIndex);
sheet.setName("test" + sheetIndex);
}
final Cells cells = sheet.getCells();
// set the columns width
for (int j = 0; j < 15; j++) {
cells.setColumnWidth(j, 15);
}
}
// Create an object with respect to LightCells data provider
final LightCellsDataProviderDemo dataProvider =
new LightCellsDataProviderDemo(wb, 1, rowCount, 3);
// Specify the XLSX file's Save options
final OoxmlSaveOptions opt = new OoxmlSaveOptions();
// Set the data provider for the file
opt.setLightCellsDataProvider(dataProvider);
// Save the big file
wb.save("DemoTest.xlsx", opt);
}
}
2. LightCellsDataProviderDemo.java
package com.test.aspose.table;
import com.aspose.cells.Cell;
import com.aspose.cells.Cells;
import com.aspose.cells.LightCellsDataProvider;
import com.aspose.cells.Row;
import com.aspose.cells.Style;
import com.aspose.cells.StyleFlag;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
public class LightCellsDataProviderDemo implements LightCellsDataProvider {
private final int sheetCount;
private final int maxRowIndex;
private final int maxColIndex;
private int rowIndex;
private int colIndex;
private final Worksheet worksheet;
private final Style style2;
@SuppressWarnings("deprecation")
public LightCellsDataProviderDemo(final Workbook wb, final int sheetCount, final int rowCount,
final int colCount) {
// set the variables/objects
this.sheetCount = sheetCount;
this.maxRowIndex = rowCount - 1;
this.maxColIndex = colCount - 1;
this.worksheet = wb.getWorksheets().get(0);
this.style2 = wb.createStyle();
}
@Override
public boolean isGatherString() {
return false;
}
@Override
public int nextCell() {
if (this.colIndex < this.maxColIndex) {
this.colIndex++;
return this.colIndex;
}
return -1;
}
@Override
public int nextRow() {
if (this.rowIndex < this.maxRowIndex) {
this.rowIndex++;
this.colIndex = -1; // reset column index
if (this.rowIndex % 1000 == 0) {
System.out.println("Row " + this.rowIndex);
}
return this.rowIndex;
}
// Trying to apply the column formatting after traversing through all the rows, but not working!
final StyleFlag flag2 = new StyleFlag();
flag2.setFontBold(false);
flag2.setNumberFormat(true);
this.style2.setCustom("0.00");
this.style2.getFont().setBold(true);
final Cells cells = this.worksheet.getCells();
cells.applyColumnStyle(1, this.style2, flag2);
return -1;
}
@Override
public void startCell(final Cell cell) {
cell.putValue(12.3456);
// Trying to apply the column formatting after traversing through all the cells, but not
// working!
if (this.rowIndex == this.maxRowIndex && this.colIndex == this.maxColIndex) {
final StyleFlag flag2 = new StyleFlag();
flag2.setFontBold(true);
flag2.setNumberFormat(true);
this.style2.setCustom("0.00");
this.style2.getFont().setBold(true);
final Cells cells = this.worksheet.getCells();
cells.applyColumnStyle(1, this.style2, flag2);
}
}
@Override
public void startRow(final Row row) {
row.setHeight(25);
}
@Override
public boolean startSheet(final int sheetIndex) {
if (sheetIndex < this.sheetCount) {
// reset row/column index
this.rowIndex = -1;
this.colIndex = -1;
return true;
}
return false;
}
}