<NME> JsonExporter.cs
<BEF> ﻿using System;
using System.IO;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace excel2json {
    /// <summary>
    /// 将DataTable对象，转换成JSON string，并保存到文件中
    /// </summary>
    class JsonExporter {
        string mContext = "";

        public string context {
            get {
                return mContext;
            }
        }

        /// <summary>
        /// 构造函数：完成内部数据创建
        /// </summary>
        /// <param name="excel">ExcelLoader Object</param>
        public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray) {

            List<DataTable> validSheets = new List<DataTable>();
            for (int i = 0; i < excel.Sheets.Count; i++) {
                DataTable sheet = excel.Sheets[i];

                if (sheet.Columns.Count > 0 && sheet.Rows.Count > 0)
                    validSheets.Add(sheet);
            }

            if (validSheets.Count == 1) {   // single sheet

                //-- convert to object
                if (excludePrefix.Length > 0 && sheetName.StartsWith(excludePrefix))
                    continue;

                if (sheet.Columns.Count > 0 && sheet.Rows.Count > 0)
                    validSheets.Add(sheet);
            else { // mutiple sheet

                Dictionary<string, object> data = new Dictionary<string, object>();
                foreach(var sheet in validSheets) {
                    object sheetValue = convertSheet(sheet, exportArray, lowcase);
                    data.Add(sheet.TableName, sheetValue);
                }

                //-- convert to json string
                mContext = JsonConvert.SerializeObject(data, Formatting.Indented);
            }
        }

        private object convertSheet(DataTable sheet, bool exportArray, bool lowcase) {
            if (exportArray) {
                return convertArray(sheet, lowcase);
            else
            else {
                return convertDict(sheet, lowcase);
            }
        }

        private object convertArray(DataTable sheet, bool lowcase) {
            List<object> values = new List<object>();

            int firstDataRow = 0;
            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
                DataRow row = sheet.Rows[i];

                values.Add(
                    convertRowData(sheet, row, lowcase, firstDataRow)
                    );
            }

            return values;
        }

        /// <summary>
        /// 以第一列为ID，转换成ID->Object的字典对象
        /// </summary>
        private object convertDict(DataTable sheet, bool lowcase) {
            Dictionary<string, object> importData =
                new Dictionary<string, object>();

            int firstDataRow = 0;
            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
                DataRow row = sheet.Rows[i];
                string ID = row[sheet.Columns[0]].ToString();
                if (ID.Length <= 0)
                    ID = string.Format("row_{0}", i);

                importData[ID] = convertRowData(sheet, row, lowcase, firstDataRow);
            }

            return importData;
        }

        /// <summary>
        /// 把一行数据转换成一个对象，每一列是一个属性
        /// </summary>
        private object convertRowData(DataTable sheet, DataRow row, bool lowcase, int firstDataRow) {
            var rowData = new Dictionary<string, object>();
            int col = 0;
            foreach (DataColumn column in sheet.Columns) {
                object value = row[column];

                if (value.GetType() == typeof(System.DBNull)) {
                    value = getColumnDefault(sheet, column, firstDataRow);
                }
                else if (value.GetType() == typeof(double)) { // 去掉数值字段的“.0”
                    double num = (double)value;
                    if ((int)num == num)
                        value = (int)num;
                }

                string fieldName = column.ToString();
                // 表头自动转换成小写
                if (lowcase)
                    fieldName = fieldName.ToLower();

                if (string.IsNullOrEmpty(fieldName))
                    fieldName = string.Format("col_{0}", col);

                rowData[fieldName] = value;
                col++;
            }

            return rowData;
        }

        /// <summary>
        /// 对于表格中的空值，找到一列中的非空值，并构造一个同类型的默认值
        /// </summary>
        private object getColumnDefault(DataTable sheet, DataColumn column, int firstDataRow) {
            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
                object value = sheet.Rows[i][column];
                Type valueType = value.GetType();
                if (valueType != typeof(System.DBNull)) {
                    if (valueType.IsValueType)
                        return Activator.CreateInstance(valueType);
                    break;
                }
            }
            return "";
        }

        /// <summary>
        /// 将内部数据转换成Json文本，并保存至文件
        /// </summary>
        /// <param name="jsonPath">输出文件路径</param>
        public void SaveToFile(string filePath, Encoding encoding) {
            //-- 保存文件
            using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write)) {
                using (TextWriter writer = new StreamWriter(file, encoding))
                    writer.Write(mContext);
            }
        }
    }
}
                    double num = (double)value;
                    if ((int)num == num)
                        value = (int)num;
                }

                //全部转换为string
                //方便LitJson.JsonMapper.ToObject<List<Dictionary<string, string>>>(textAsset.text)等使用方式 之后根据自己的需求进行解析
                if (allString && !(value is string))
                {
                    value = value.ToString();
                }

                string fieldName = column.ToString();
                // 表头自动转换成小写
                if (lowcase)
                    fieldName = fieldName.ToLower();

                if (string.IsNullOrEmpty(fieldName))
                    fieldName = string.Format("col_{0}", col);

                rowData[fieldName] = value;
                col++;
            }

            return rowData;
        }

        /// <summary>
        /// 对于表格中的空值，找到一列中的非空值，并构造一个同类型的默认值
        /// </summary>
        private object getColumnDefault(DataTable sheet, DataColumn column, int firstDataRow)
        {
            for (int i = firstDataRow; i < sheet.Rows.Count; i++)
            {
                object value = sheet.Rows[i][column];
                Type valueType = value.GetType();
                if (valueType != typeof(System.DBNull))
                {
                    if (valueType.IsValueType)
                        return Activator.CreateInstance(valueType);
                    break;
                }
            }
            return "";
        }

        /// <summary>
        /// 将内部数据转换成Json文本，并保存至文件
        /// </summary>
        /// <param name="jsonPath">输出文件路径</param>
        public void SaveToFile(string filePath, Encoding encoding)
        {
            //-- 保存文件
            using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                using (TextWriter writer = new StreamWriter(file, encoding))
                    writer.Write(mContext);
            }
        }
    }
}

<MSG> add id to row object

<DFF> @@ -1,37 +1,37 @@
-﻿using System;
-using System.IO;
-using System.Data;
-using System.Text;
-using System.Collections.Generic;
-using Newtonsoft.Json;
-
-namespace excel2json {
-    /// <summary>
-    /// 将DataTable对象，转换成JSON string，并保存到文件中
-    /// </summary>
-    class JsonExporter {
-        string mContext = "";
-
-        public string context {
-            get {
-                return mContext;
-            }
-        }
-
-        /// <summary>
-        /// 构造函数：完成内部数据创建
-        /// </summary>
-        /// <param name="excel">ExcelLoader Object</param>
-        public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray) {
-
-            List<DataTable> validSheets = new List<DataTable>();
+﻿using System;
+using System.IO;
+using System.Data;
+using System.Text;
+using System.Collections.Generic;
+using Newtonsoft.Json;
+
+namespace excel2json {
+    /// <summary>
+    /// 将DataTable对象，转换成JSON string，并保存到文件中
+    /// </summary>
+    class JsonExporter {
+        string mContext = "";
+
+        public string context {
+            get {
+                return mContext;
+            }
+        }
+
+        /// <summary>
+        /// 构造函数：完成内部数据创建
+        /// </summary>
+        /// <param name="excel">ExcelLoader Object</param>
+        public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray) {
+
+            List<DataTable> validSheets = new List<DataTable>();
             for (int i = 0; i < excel.Sheets.Count; i++) {
                 DataTable sheet = excel.Sheets[i];
 
                 if (sheet.Columns.Count > 0 && sheet.Rows.Count > 0)
-                    validSheets.Add(sheet);
-            }
-
+                    validSheets.Add(sheet);
+            }
+
             if (validSheets.Count == 1) {   // single sheet
 
                 //-- convert to object
@@ -43,7 +43,7 @@ namespace excel2json {
             else { // mutiple sheet
 
                 Dictionary<string, object> data = new Dictionary<string, object>();
-                foreach(var sheet in validSheets) {
+                foreach (var sheet in validSheets) {
                     object sheetValue = convertSheet(sheet, exportArray, lowcase);
                     data.Add(sheet.TableName, sheetValue);
                 }
@@ -51,8 +51,8 @@ namespace excel2json {
                 //-- convert to json string
                 mContext = JsonConvert.SerializeObject(data, Formatting.Indented);
             }
-        }
-
+        }
+
         private object convertSheet(DataTable sheet, bool exportArray, bool lowcase) {
             if (exportArray) {
                 return convertArray(sheet, lowcase);
@@ -60,102 +60,104 @@ namespace excel2json {
             else {
                 return convertDict(sheet, lowcase);
             }
-        }
-
-        private object convertArray(DataTable sheet, bool lowcase) {
-            List<object> values = new List<object>();
-
-            int firstDataRow = 0;
-            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
-                DataRow row = sheet.Rows[i];
-
-                values.Add(
-                    convertRowData(sheet, row, lowcase, firstDataRow)
-                    );
-            }
-
-            return values;
-        }
-
-        /// <summary>
-        /// 以第一列为ID，转换成ID->Object的字典对象
-        /// </summary>
-        private object convertDict(DataTable sheet, bool lowcase) {
-            Dictionary<string, object> importData =
-                new Dictionary<string, object>();
-
-            int firstDataRow = 0;
-            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
-                DataRow row = sheet.Rows[i];
-                string ID = row[sheet.Columns[0]].ToString();
-                if (ID.Length <= 0)
-                    ID = string.Format("row_{0}", i);
-
-                importData[ID] = convertRowData(sheet, row, lowcase, firstDataRow);
-            }
-
-            return importData;
-        }
-
-        /// <summary>
-        /// 把一行数据转换成一个对象，每一列是一个属性
-        /// </summary>
-        private object convertRowData(DataTable sheet, DataRow row, bool lowcase, int firstDataRow) {
-            var rowData = new Dictionary<string, object>();
-            int col = 0;
-            foreach (DataColumn column in sheet.Columns) {
-                object value = row[column];
-
-                if (value.GetType() == typeof(System.DBNull)) {
-                    value = getColumnDefault(sheet, column, firstDataRow);
-                }
-                else if (value.GetType() == typeof(double)) { // 去掉数值字段的“.0”
-                    double num = (double)value;
-                    if ((int)num == num)
-                        value = (int)num;
-                }
-
-                string fieldName = column.ToString();
-                // 表头自动转换成小写
-                if (lowcase)
-                    fieldName = fieldName.ToLower();
-
-                if (string.IsNullOrEmpty(fieldName))
-                    fieldName = string.Format("col_{0}", col);
-
-                rowData[fieldName] = value;
-                col++;
-            }
-
-            return rowData;
-        }
-
-        /// <summary>
-        /// 对于表格中的空值，找到一列中的非空值，并构造一个同类型的默认值
-        /// </summary>
-        private object getColumnDefault(DataTable sheet, DataColumn column, int firstDataRow) {
-            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
-                object value = sheet.Rows[i][column];
-                Type valueType = value.GetType();
-                if (valueType != typeof(System.DBNull)) {
-                    if (valueType.IsValueType)
-                        return Activator.CreateInstance(valueType);
-                    break;
-                }
-            }
-            return "";
-        }
-
-        /// <summary>
-        /// 将内部数据转换成Json文本，并保存至文件
-        /// </summary>
-        /// <param name="jsonPath">输出文件路径</param>
-        public void SaveToFile(string filePath, Encoding encoding) {
-            //-- 保存文件
-            using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write)) {
-                using (TextWriter writer = new StreamWriter(file, encoding))
-                    writer.Write(mContext);
-            }
-        }
-    }
-}
+        }
+
+        private object convertArray(DataTable sheet, bool lowcase) {
+            List<object> values = new List<object>();
+
+            int firstDataRow = 0;
+            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
+                DataRow row = sheet.Rows[i];
+
+                values.Add(
+                    convertRowToDict(sheet, row, lowcase, firstDataRow)
+                    );
+            }
+
+            return values;
+        }
+
+        /// <summary>
+        /// 以第一列为ID，转换成ID->Object的字典对象
+        /// </summary>
+        private object convertDict(DataTable sheet, bool lowcase) {
+            Dictionary<string, object> importData =
+                new Dictionary<string, object>();
+
+            int firstDataRow = 0;
+            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
+                DataRow row = sheet.Rows[i];
+                string ID = row[sheet.Columns[0]].ToString();
+                if (ID.Length <= 0)
+                    ID = string.Format("row_{0}", i);
+
+                var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow);
+                rowObject[ID] = ID;
+                importData[ID] = rowObject;
+            }
+
+            return importData;
+        }
+
+        /// <summary>
+        /// 把一行数据转换成一个对象，每一列是一个属性
+        /// </summary>
+        private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow) {
+            var rowData = new Dictionary<string, object>();
+            int col = 0;
+            foreach (DataColumn column in sheet.Columns) {
+                object value = row[column];
+
+                if (value.GetType() == typeof(System.DBNull)) {
+                    value = getColumnDefault(sheet, column, firstDataRow);
+                }
+                else if (value.GetType() == typeof(double)) { // 去掉数值字段的“.0”
+                    double num = (double)value;
+                    if ((int)num == num)
+                        value = (int)num;
+                }
+
+                string fieldName = column.ToString();
+                // 表头自动转换成小写
+                if (lowcase)
+                    fieldName = fieldName.ToLower();
+
+                if (string.IsNullOrEmpty(fieldName))
+                    fieldName = string.Format("col_{0}", col);
+
+                rowData[fieldName] = value;
+                col++;
+            }
+
+            return rowData;
+        }
+
+        /// <summary>
+        /// 对于表格中的空值，找到一列中的非空值，并构造一个同类型的默认值
+        /// </summary>
+        private object getColumnDefault(DataTable sheet, DataColumn column, int firstDataRow) {
+            for (int i = firstDataRow; i < sheet.Rows.Count; i++) {
+                object value = sheet.Rows[i][column];
+                Type valueType = value.GetType();
+                if (valueType != typeof(System.DBNull)) {
+                    if (valueType.IsValueType)
+                        return Activator.CreateInstance(valueType);
+                    break;
+                }
+            }
+            return "";
+        }
+
+        /// <summary>
+        /// 将内部数据转换成Json文本，并保存至文件
+        /// </summary>
+        /// <param name="jsonPath">输出文件路径</param>
+        public void SaveToFile(string filePath, Encoding encoding) {
+            //-- 保存文件
+            using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write)) {
+                using (TextWriter writer = new StreamWriter(file, encoding))
+                    writer.Write(mContext);
+            }
+        }
+    }
+}

{'addition_count': 134, 'commit_subject': 'add id to row object', 'deletion_count': 132, 'file_extension': '.cs', 'lang': 'cs', 'license': 'mit', 'repo_name': 'neil3d/excel2json'}