I have enclosed 3 files,
- excel input containing the data
- excel subtotal output generated using MS Excel Subtotal function manually
- excel subtotal output generated using Aspose cells (18.5) subtotal feature programmatically
There is a discrepancy between both the outputs. Please check the outline menu on left side of the worksheet.
FYI, applied subtotal feature twice,
- Region change of column, aggregate function SUM on the aggregation column Sale Price
- Region change of column, aggregate function SUM on the aggregate column Sale Unit
And i have used subtotal(, false, false, true) method so that it will not override previous generated subtotal
I am also attaching the sample aspose code. Please check and help me in achieving the same behaviour as MS Excel
Sample Java Code
import java.io.IOException;
import java.io.InputStream;
import com.aspose.cells.BackgroundType;
import com.aspose.cells.Cell;
import com.aspose.cells.CellArea;
import com.aspose.cells.Cells;
import com.aspose.cells.ConsolidationFunction;
import com.aspose.cells.DataSorter;
import com.aspose.cells.License;
import com.aspose.cells.Row;
import com.aspose.cells.SortOrder;
import com.aspose.cells.Style;
import com.aspose.cells.StyleFlag;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
public class AsposeCreateSubtotals2 {
public static void main(String[] args) {
// Instantiate a new workbook
Workbook workbook = null;
try {
workbook = new Workbook(Thread.currentThread().getContextClassLoader().getResourceAsStream("5375.xlsx"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the Cells collection in the first worksheet
Cells cells = workbook.getWorksheets().get(0).getCells();
// Create a cellarea i.e.., B3:C19
CellArea ca = new CellArea();
ca.StartRow = 1;// don't count header
ca.StartColumn = 0;
ca.EndRow = 49037;
ca.EndColumn = 3;
// Get the workbook datasorter object.
DataSorter sorter = workbook.getDataSorter();
// Set the first order for datasorter object.
sorter.addKey(1,SortOrder.ASCENDING);
sorter.addKey(0,SortOrder.ASCENDING);
sorter.sort(workbook.getWorksheets().get(0).getCells(), ca);
// Apply subtotal, the consolidation function is Sum and it will applied
// to
// Second column (C) in the list
cells.subtotal(ca, 1, ConsolidationFunction.SUM, new int[] {3});
cells.subtotal(ca, 1, ConsolidationFunction.SUM, new int[] {2}, false, false, true);
applyStylesToSubtotalRow(workbook);
//Extend the column width a bit
cells.setColumnWidthPixel(0,172);
// Save the excel file
try {
//Auto-fit rows in the worksheet
workbook.getWorksheets().get(0).autoFitRows();
workbook.save("AsposeCreateSubtotals_out2.xlsx");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Print message
System.out.println("Process completed successfully");
}
private static void applyStylesToSubtotalRow(Workbook wb) {
Style style;
StyleFlag styleFlag;
Cells cells = wb.getWorksheets().get(0).getCells();
Cell cell = null;
do {
cell = cells.findFormulaContains("=SUBTOTAL(", cell);
if (cell != null) {
// Apply formatting to subtotal and grand total rows
style = wb.createStyle();
style.getFont().setName("Calibri");
style.getFont().setSize(14);
style.getFont().setBold(true);
style.setForegroundColor(com.aspose.cells.Color.getAliceBlue());
style.setPattern(BackgroundType.SOLID);
styleFlag = new StyleFlag();
styleFlag.setFontName(true);
styleFlag.setFontSize(true);
styleFlag.setFontBold(true);
styleFlag.setCellShading(true);
cell.setStyle(style, styleFlag);
//checkGrandTotalRow(wb.getWorksheets().get(0), cell.getRow(),cell.getColumn());
}
} while (cell != null);
}
private static void checkGrandTotalRow(Worksheet ws, int rowNum, int colNum) {
for (int i = 0; i <= colNum; i++) {
Object obj = ws.getCells().get(rowNum, i).getValue();
System.out.println("obj >>" + obj);
if (null != obj && obj.toString().toUpperCase().contains("GRAND")) {
hideGrandTotalRow(ws, rowNum);
break;
}
}
}
private static void hideGrandTotalRow(Worksheet ws, int rowNum) {
System.out.println("delete");
ws.getCells().deleteRow(rowNum);
}
}
AsposeCreateSubtotals_out2.zip (2.7 MB)