We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Set DataType of a Cell

Hi.

Please let me know How to set DataType of a Cell and How to Set Function to Get Totals of a Cell at end of Reports.

Sunil

Hi,<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” />

Thank you for considering Aspose.

Yes, you can use Cells[index].Style.Number property to set different Cell data formats. Please see the sample code for setting the cell style.

Sample Code:

Workbook wb = new Workbook();

wb.Open("C:\\output1.xls");

Worksheet sheet = wb.Worksheets[0];

sheet.Cells[7, 1].Style.Number = 15;

sheet.Cells[7, 1].PutValue("10/12/2008");

wb.Save("c:\\style_column.xlsx",FileFormatType.Excel2007Xlsx);

Please see the following link for supported format styles by Aspose.Cells,

https://docs.aspose.com/cells/net/data-formatting/#setting-display-formats-of-numbers-and-dates

Also you can use Cells.Formula property to set the formula to get the totals.

worksheet.Cells[51].Formula = "=SUM(A1:A50)";

Thank you & Best Regards,

Thanks nausherwan.

Aspose.Cells will automatic adjust to Right and Also if Want to Fix .00 say Rs. 12456.26 then format setting upto 2 decimal.

Sunil

Hi,

Well, you can Set Cells.Style.Number = 2 to get your desired result.

Thank you & Best Regards,

Hi Sunil,

Since you are using Aspose.Cells for Java I think, So, refer to the following sample Java code for your need.

Sample code:

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().addSheet();
Cells cells = worksheet.getCells();
Cell cell = cells.getCell("A1");
cell.setValue(Calendar.getInstance());
//Setting the display format with decimal points for the number
Style style = cell.getStyle();
style.setNumber(2);
cell.setStyle(style);
workbook.save("C:\\output.xls",FileFormatType.DEFAULT);

Thank you.