簡體   English   中英

將網格數據導出到EXCEL時獲取字符串中的數字類型字段

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

我正在將Kendo網格導出到EXCEL。 使用DocumentFormat.OpenXml.Below是我正在使用的ExportToExcel()操作。

導出時效果很好,但是“數字字段”在EXCEl中顯示為字符串。由於某種原因,如果網格中為字符串類型,則必須在“數字”(如果是數字字段)/字符串中具有數據值。

有什么辦法可以實現這一目標?

請幫助我。

    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);
    }

我正在使用EPPlus從Kendo網格生成Excel導出。

有關更多詳細信息,請參見此 文件中公共JsonResult Export(KendoGridFilter過濾器,字符串guid)方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM