I have a Workbook containing double values, and I want to keep the precision of those values when saving. I notice that when I save the sheet to csv format (haven’t tried other formats), Aspose Cells uses the StringValue of that cell when saving. This strips some decimals from the output, and I don’t want that.
Hi,
Hi Ronald,
Worksheet sheet = book.Worksheets[0];
DataTable table = sheet.Cells.ExportDataTable(0, 0, sheet.Cells.MaxDataRow + 1, sheet.Cells.MaxDataColumn + 1, new ExportTableOptions());
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = table.Columns.Cast().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in table.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(“C:/temp/test.csv”, sb.ToString());
Thanks Babar and Amjad for the swift responses.
Hi,
Thanks for using Aspose.Cells.
Please check if the following workaround suits your needs. It converts the value of cell A1 to string and then saves to csv and the output csv does not lose precision.
I have attached the source excel file used in this code and output csv file for your reference.
C#
string filePath = @“F:\Shak-Data-RW\Downloads\source.xlsx”;
Workbook workbook = new Workbook(filePath);
Worksheet worksheet = workbook.Worksheets[0];
Cell cell = worksheet.Cells[“A1”];
cell.PutValue(cell.StringValueWithoutFormat);
workbook.Save(“output.csv”);
Thanks Shakeel for the sample code, but then I would have to store every double value as a string in my workbook. I prefer to keep them as numeric values.
Hi,
Thanks for your feedback and using Aspose.Cells.
Newer excel formats like xls and xlsx is definitely a good choice than older format like csv.
However, when you load your workbook, it does not change your existing workbook on the disk. All changes are done inside the memory and then you save it in csv format. So nothing happens to a workbook present on hard drive. It means, you will never have to store your double value as a string inside your workbook on hard drive. All conversion of values into string will happen on runtime. Thanks for your kind comment and appreciation. Best wishes.