I am generating an excel from data brought from a stored procedure in sql server.
I bring the stored data in data table format and I generate the excel with this class:
My problem is that one of the fields is decimal and when I open the excel it marks me that it is damaged but I still open it and it brings all the data but in the field in decimal if the value has decimals it marks it as string and that's why he returns me that is damaged.
I read that I can format it this way:
var nformat4Decimal = new NumberingFormat
{
NumberFormatId = UInt32Value.FromUInt32(iExcelIndex++),
FormatCode = StringValue.FromString("#,##0.0000")
};
But I do not know in which part of the class to use this. My idea is to generate a boray abrray as it is used in the class and to save those that are decimal type in true
int numberOfColumns = dt.Columns.Count;
bool[] IsNumericColumn = new bool[numberOfColumns];
IsNumericColumn[colInx] = (col.DataType.FullName == "System.Decimal")
and when you do the foreach of the rows if you are in the index of that column convert it
for (int colInx = 0; colInx < numberOfColumns; colInx++)
{
DataColumn col = dt.Columns[colInx];
AppendTextCell(excelColumnNames[colInx] + "1", col.ColumnName, headerRow);
IsNumericColumn[colInx] = (col.DataType.FullName == "System.Decimal") || (col.DataType.FullName == "System.Int32");
}
//
// Now, step through each row of data in our DataTable...
//
double cellNumericValue = 0;
foreach (DataRow dr in dt.Rows)
{
// ...create a new row, and append a set of this row's data to it.
++rowIndex;
var newExcelRow = new Row { RowIndex = rowIndex }; // add a row at the top of spreadsheet
sheetData.Append(newExcelRow);
for (int colInx = 0; colInx < numberOfColumns; colInx++)
{
cellValue = dr.ItemArray[colInx].ToString();
// Create cell with data
if (IsNumericColumn[colInx])
{
//Formatear el campo
}
else
{
// For text cells, just write the input data straight out to the Excel file.
AppendTextCell(excelColumnNames[colInx] + rowIndex.ToString(), cellValue, newExcelRow);
}
}
}
But I do not know how you would format it.
I hope you understand.
Greetings.