Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove "Number Stored as Text" prompt upon Export To Excel using EPPLUS?

I have export my gridview data to excel using EPPLUS, but however for a few columns I have this green tag "Number Stored as Text" prompt after opening the excel file that was exported.

How can I code in my export to excel codes to change the datatype/convert to number for a specific column starting from the 2nd row of a specified column since the 1st row is header?

like image 913
James Boer Avatar asked Oct 28 '15 00:10

James Boer


3 Answers

I had similar problems. As Ordel Eraki said: do not stringify the value:

workSheet.Cells[1, 1].Value = 2; // Value is object

workSheet.Cells[1, 1].Value = 2.ToString()

like image 59
mihkov Avatar answered Oct 17 '22 21:10

mihkov


You should save you string as an int not a string. Whenever you want your cell to be as number, just cast your cell input as int.

That means, when populating your data, be sure to populate it as int.

like image 2
Orel Eraki Avatar answered Oct 17 '22 19:10

Orel Eraki


I have updated your code here.
Check the added code..
EDITED
I have change the variable name for your DataTable assuming the thing what you are doing.. GridView1 is a GridView UI Control and you put the DataTable Name as GridView1, the code might be confuse on what is GridView1

protected void EXPORT_BUTTON_Click(object sender, EventArgs e)
{
    // Added Code
    int parseValue;
    bool isInt;

    ExcelPackage package = new ExcelPackage();

    ExcelWorksheet Grid = package.Workbook.Worksheets.Add("ORSA ASSESSMENTS");

    DataTable dt = new DataTable();
    for (int i = 0; i < dt.Columns.Count; i++)
    {
        // Update Type
        dt.Columns.Add("column" + i.ToString(), typeof(int));
    }

    foreach (GridViewRow row in GridView1.Rows)
    {
        DataRow dr = dt.NewRow();
        for (int j = 0; j < GridView1.Columns.Count; j++)
        {
            row.Cells[j].Text = row.Cells[j].Text.Replace("&nbsp;", " ");

            // Added Code
            isInt = int.TryParse(row.Cells[j].Text.Trim(), out parseValue);

            // Added Code
            if (isInt) 
                dr["column" + j.ToString()] = parseValue;

        }

        dt.Rows.Add(dr);
    }


    Grid.Cells["A1"].LoadFromDataTable(dt, true);

    using (ExcelRange rng = Grid.Cells["A1:Z1"])
    {
        rng.Style.Font.Bold = true;
    }

    Grid.Cells[ORSA.Dimension.Address].AutoFitColumns();



    var FolderPath = ServerName + DirectoryLocation + DirectoryFolder + ExportsFolder;
    var filename = ExcelName + @"_" + ".xlsx";
    var filepath = new FileInfo(Path.Combine(FolderPath, filename));

    Response.Clear();
    package.SaveAs(filepath);
    Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";");
    Response.Charset = "";
    Response.ContentType = "application/vnd.xlsx";
    Response.TransmitFile(filepath.FullName);
    Response.End();

}


This is the sample i mocked up and is a working example

protected void Page_Load(object sender, EventArgs e)
{
    // Check
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();

        // Create Column
        for(int i = 0; i < 5; i++)
            dt.Columns.Add("column" + i, typeof(int));

        for (int i = 0; i < 10; i++)
            dt.Rows.Add(i, i+1, i+2, i+3, i+4);

        GenerateExcel(dt);
    }
}

private void GenerateExcel(DataTable dt)
{
    using (ExcelPackage pkg = new ExcelPackage())
    {
        ExcelWorksheet ws = pkg.Workbook.Worksheets.Add("Sheet1");

        ws.Cells[1, 1].LoadFromDataTable(dt, true, OfficeOpenXml.Table.TableStyles.Light18);

        pkg.SaveAs(new FileInfo("C:\\Test.xlsx"));
    }
}
like image 1
Nic Avatar answered Oct 17 '22 20:10

Nic