I`m using closedXML to generate a simple template with 3 columns.
To create this template i`m using this code:
protected void btnTemplate_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
var tipo = "TARGET";
if (ddlTipo.SelectedValue == "RESULTADO")
{
tipo = "RESULTADO";
}
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("BU", typeof(string)),
new DataColumn("MÉTRICA", typeof(string)),
new DataColumn(tipo,typeof(string)) });
//Exporting to Excel
//Codes for the Closed XML
using (XLWorkbook wb = new XLWorkbook())
{
var worksheet = wb.Worksheets.Add(dt, "BASE");
worksheet.Cell("C1").DataType = XLDataType.Text;
//wb.SaveAs(folderPath + "DataGridViewExport.xlsx");
string myName = ("Template.xlsx");
MemoryStream stream = GetStream(wb);// The method is defined below
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment; filename=" + myName);
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
Its working fine, the problem is in the C column, it can receive percentage, and it changed the type in the excel.
Is there a way to force in closedXML that the column C is always treated as TEXT? To not convert the numbers. i tried using worksheet.Cell("C1").DataType = XLDataType.Text; to force as text,but it doesnt`t work.