简体   繁体   中英

Getting Number type Fields in string while Exporting a grid data To EXCEL

I am Exporting a Kendo grid to EXCEL. Using DocumentFormat.OpenXml.Below is my ExportToExcel() Action I am Using.

When I am Exporting It works well But the " Number fields" are Showing as string in EXCEl.For some reason I have to have Data value in Number (If it's a number field)/string if its a string type in grid.

Is there any way to Achieve this ??

Please Help me out anybody.

    public JsonResult ExportToExcel(string model, string data, string title)
    {
        using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
        {
            /* Create the worksheet. */

            SpreadsheetDocument spreadsheet = Excel.CreateWorkbook(stream);
            Excel.AddBasicStyles(spreadsheet);
            Excel.AddAdditionalStyles(spreadsheet);
            Excel.AddWorksheet(spreadsheet, title);
            Worksheet worksheet = spreadsheet.WorkbookPart.WorksheetParts.First().Worksheet;


            /* Get the information needed for the worksheet */

            var modelObject = JsonConvert.DeserializeObject<dynamic>(model);
            var dataObject = JsonConvert.DeserializeObject<dynamic>(data);


            /* Add the column titles to the worksheet. */

            // For each column...
            for (int mdx = 0; mdx < modelObject.Count; mdx++)
            {
                // If the column has a title, use it.  Otherwise, use the field name.
                Excel.SetColumnHeadingValue(spreadsheet, worksheet, Convert.ToUInt32(mdx + 1),
                    modelObject[mdx].title == null ? modelObject[mdx].field.ToString() : modelObject[mdx].title.ToString(),
                    false, false);

                // Is there are column width defined?
                Excel.SetColumnWidth(worksheet, mdx + 1, modelObject[mdx].width != null
                    ? Convert.ToInt32(modelObject[mdx].width.ToString()) / 4
                    : 25);
            }


            /* Add the data to the worksheet. */

            // For each row of data...
            for (int idx = 0; idx < dataObject.Count; idx++)
            {
                // For each column...
                for (int mdx = 0; mdx < modelObject.Count; mdx++)
                {
                    // Set the field value in the spreadsheet for the current row and column.
                    Excel.SetCellValue(spreadsheet, worksheet, Convert.ToUInt32(mdx + 1), Convert.ToUInt32(idx + 2),
                        dataObject[idx][modelObject[mdx].field.ToString()].ToString(),
                        false, false);
                }
            }


            /* Save the worksheet and store it in Session using the spreadsheet title. */

            worksheet.Save();
            spreadsheet.Close();
            byte[] file = stream.ToArray();
            Session[title] = file;
        }

        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }

I'm using EPPlus to generate an Excel export from the Kendo grid.

See the public JsonResult Export(KendoGridFilter filter, string guid) method in this file for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM