导出网格数据到EXCEL时,以字符串形式获取数字类型字段
本文关键字:字符串 获取 数字 字段 类型 网格 数据 EXCEL | 更新日期: 2023-09-27 18:05:26
我正在导出剑道网格到EXCEL。下面是我的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从剑道网格生成Excel导出。
查看public JsonResult Export(KendoGridFilter filter, string guid)方法了解更多细节。