'Spire Xls set cell range hexadesimal format
I am using Spire Xls with c#, to create an Excel document. The problem is with one of my columns, that is an hexadesimal string. In most cases the cells of the columns are ok, but, when the hexadesimal string contains only numbers (and the 'E' character) then, those cells appear in number format
**Example:
** 92995E85 (original string) -> 9.2995E+89 (cell result)I want to keep the original string in the cell.
This is my code:
Workbook workbook = new Workbook();
workbook.Version = ExcelVersion.Version2013;
var ws = workbook.ActiveSheet;
ws.InsertDataTable(report, true, 1, 1);
for (int i = 1; i <= 9; i++)
ws.AutoFitColumn(i);
using(var fs = new FileStream(options.ReportPath.Trim(), FileMode.Create)) {
//cerrar bien el filestream para q escriba a disco
workbook.SaveToStream(fs);
workbook.Dispose();
}
The
reportvariable is aDataTablewith the problematic column as string.
Solution 1:[1]
I tested with Spire.XLS but couldn't reproduce the issue. I'd advise you to set the number format to text and try again. Here is my code:
{
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
DataTable dt = GetData();
//Set the number format of the first column as text
//sheet.Columns[0].NumberFormat = "@";
sheet.InsertDataTable(dt, false, 1, 1, false);
workbook.SaveToFile("InsertData.xlsx", ExcelVersion.Version2010);
}
private static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("column1", typeof(string));
dt.Columns.Add("column2", typeof(DateTime));
DataRow row = dt.NewRow();
row[0] = "92995E85";
row[1] = "00:00";
dt.Rows.Add(row);
row = dt.NewRow();
row[0] = "333";
//row[1] = "2";
dt.Rows.Add(row);
return dt;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dheeraj Malik |

