.net中Json转DataTable导致精度损失

otgo 发布于 2013/08/27 17:02
阅读 1K+
收藏 0

asp.net mvc中利用NPOI生成excel,view采用extjs4.0

controler中

public string ExportDataToExcel(string store)
{
      DataTable newTable = JsonToTable(store);//JSONHelper.DeserializeObject<DataTable>(store); 
      ExportExcel.ExportEasy(newTable, "设备日推移图");
      return "设备日推移图.xls";
      //return File(ExportExcel.ExportEasy(newTable).ToArray(), "application/vnd.ms-excel", "111.xls");
}

public static DataTable JsonToTable(string json)
{
     DataTable dt = new DataTable();
     dt = (DataTable)JsonConvert.DeserializeObject<DataTable>(json);
     return dt;
}    
/// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dtSource">交叉表</param>
        /// <param name="strFileName">excel名称</param>
        /// <remarks>NPOI认为Excel的第一个单元格是:(0,0)</remarks>
        public static bool ExportEasy(DataTable dtSource, string strFileName)
        {
            //MemoryStream memoryStream = new MemoryStream();
            try
            {
                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet = workbook.CreateSheet() as HSSFSheet;

                HSSFRow dataRow = sheet.CreateRow(0) as HSSFRow;
                foreach (DataColumn column in dtSource.Columns)
                {
                    dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                }

                for (int i = 0; i < dtSource.Rows.Count; i++)
                {
                    dataRow = sheet.CreateRow(i + 1) as HSSFRow;
                    for (int j = 0; j < dtSource.Columns.Count; j++)
                    {
                        if (j > 0)
                        {
                            dataRow.CreateCell(j).SetCellValue(Convert.ToDouble(dtSource.Rows[i][j]));
                        }
                        else
                        {
                            dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
                        }
                    }
                }
                string path = System.Web.HttpContext.Current.Server.MapPath(@"~/") + strFileName + ".xls";
                using (FileStream fs = File.OpenWrite(path))
                {
                    workbook.Write(fs);
                    fs.Flush();
                }
                //workbook.Write(memoryStream);
                sheet.Dispose();
                workbook.Dispose();
                sheet.Dispose();
                workbook.Dispose();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }

extjs

xtype: 'button',
                            text: '导出数据',
                            handler: function () {
                                Ext.MessageBox.confirm('导出数据', '你想导出数据吗?', function (choice) {
                                    if (choice == 'yes') {
                                        Ext.Ajax.request({
                                            timeout: 2 * 1000 * 60,
                                            url: 'Report/ExportDataToExcel',
                                            params: { store: Ext.JSON.encode(Ext.Array.pluck(grid.getStore().data.items, "data")) },
                                            success: function (response, opts) {
                                                var u = response.responseText;
                                                window.open(u, '_blank');
                                            }
                                        });
                                    }
                                });
                            }
在Json转换为DataTable的时候导致了精度的损失,这是怎么回事呢?

加载中
0
真幻的现实
真幻的现实
看下datatable的column数据类型
返回顶部
顶部