@@ -25,17 +25,17 @@ public string context {
25
25
/// 构造函数:完成内部数据创建
26
26
/// </summary>
27
27
/// <param name="excel">ExcelLoader Object</param>
28
- public JsonExporter ( ExcelLoader excel , bool lowcase , bool exportArray , string dateFormat , bool forceSheetName , int headerRows , string excludePrefix )
28
+ public JsonExporter ( ExcelLoader excel , bool lowcase , bool exportArray , string dateFormat , bool forceSheetName , int headerRows , string excludePrefix , bool cellJson )
29
29
{
30
- mHeaderRows = headerRows - 1 ;
30
+ mHeaderRows = headerRows - 1 ;
31
31
List < DataTable > validSheets = new List < DataTable > ( ) ;
32
32
for ( int i = 0 ; i < excel . Sheets . Count ; i ++ )
33
33
{
34
34
DataTable sheet = excel . Sheets [ i ] ;
35
-
35
+
36
36
// 过滤掉包含特定前缀的表单
37
37
string sheetName = sheet . TableName ;
38
- if ( excludePrefix . Length > 0 && sheetName . StartsWith ( excludePrefix ) )
38
+ if ( excludePrefix . Length > 0 && sheetName . StartsWith ( excludePrefix ) )
39
39
continue ;
40
40
41
41
if ( sheet . Columns . Count > 0 && sheet . Rows . Count > 0 )
@@ -52,7 +52,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
52
52
{ // single sheet
53
53
54
54
//-- convert to object
55
- object sheetValue = convertSheet ( validSheets [ 0 ] , exportArray , lowcase , excludePrefix ) ;
55
+ object sheetValue = convertSheet ( validSheets [ 0 ] , exportArray , lowcase , excludePrefix , cellJson ) ;
56
56
57
57
//-- convert to json string
58
58
mContext = JsonConvert . SerializeObject ( sheetValue , jsonSettings ) ;
@@ -63,7 +63,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
63
63
Dictionary < string , object > data = new Dictionary < string , object > ( ) ;
64
64
foreach ( var sheet in validSheets )
65
65
{
66
- object sheetValue = convertSheet ( sheet , exportArray , lowcase , excludePrefix ) ;
66
+ object sheetValue = convertSheet ( sheet , exportArray , lowcase , excludePrefix , cellJson ) ;
67
67
data . Add ( sheet . TableName , sheetValue ) ;
68
68
}
69
69
@@ -72,15 +72,15 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
72
72
}
73
73
}
74
74
75
- private object convertSheet ( DataTable sheet , bool exportArray , bool lowcase , string excludePrefix )
75
+ private object convertSheet ( DataTable sheet , bool exportArray , bool lowcase , string excludePrefix , bool cellJson )
76
76
{
77
77
if ( exportArray )
78
- return convertSheetToArray ( sheet , lowcase , excludePrefix ) ;
78
+ return convertSheetToArray ( sheet , lowcase , excludePrefix , cellJson ) ;
79
79
else
80
- return convertSheetToDict ( sheet , lowcase , excludePrefix ) ;
80
+ return convertSheetToDict ( sheet , lowcase , excludePrefix , cellJson ) ;
81
81
}
82
82
83
- private object convertSheetToArray ( DataTable sheet , bool lowcase , string excludePrefix )
83
+ private object convertSheetToArray ( DataTable sheet , bool lowcase , string excludePrefix , bool cellJson )
84
84
{
85
85
List < object > values = new List < object > ( ) ;
86
86
@@ -90,7 +90,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase, string exclude
90
90
DataRow row = sheet . Rows [ i ] ;
91
91
92
92
values . Add (
93
- convertRowToDict ( sheet , row , lowcase , firstDataRow , excludePrefix )
93
+ convertRowToDict ( sheet , row , lowcase , firstDataRow , excludePrefix , cellJson )
94
94
) ;
95
95
}
96
96
@@ -100,7 +100,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase, string exclude
100
100
/// <summary>
101
101
/// 以第一列为ID,转换成ID->Object的字典对象
102
102
/// </summary>
103
- private object convertSheetToDict ( DataTable sheet , bool lowcase , string excludePrefix )
103
+ private object convertSheetToDict ( DataTable sheet , bool lowcase , string excludePrefix , bool cellJson )
104
104
{
105
105
Dictionary < string , object > importData =
106
106
new Dictionary < string , object > ( ) ;
@@ -113,7 +113,7 @@ private object convertSheetToDict(DataTable sheet, bool lowcase, string excludeP
113
113
if ( ID . Length <= 0 )
114
114
ID = string . Format ( "row_{0}" , i ) ;
115
115
116
- var rowObject = convertRowToDict ( sheet , row , lowcase , firstDataRow , excludePrefix ) ;
116
+ var rowObject = convertRowToDict ( sheet , row , lowcase , firstDataRow , excludePrefix , cellJson ) ;
117
117
// 多余的字段
118
118
// rowObject[ID] = ID;
119
119
importData [ ID ] = rowObject ;
@@ -125,7 +125,7 @@ private object convertSheetToDict(DataTable sheet, bool lowcase, string excludeP
125
125
/// <summary>
126
126
/// 把一行数据转换成一个对象,每一列是一个属性
127
127
/// </summary>
128
- private Dictionary < string , object > convertRowToDict ( DataTable sheet , DataRow row , bool lowcase , int firstDataRow , string excludePrefix )
128
+ private Dictionary < string , object > convertRowToDict ( DataTable sheet , DataRow row , bool lowcase , int firstDataRow , string excludePrefix , bool cellJson )
129
129
{
130
130
var rowData = new Dictionary < string , object > ( ) ;
131
131
int col = 0 ;
@@ -138,6 +138,24 @@ private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row
138
138
139
139
object value = row [ column ] ;
140
140
141
+ // 尝试将单元格字符串转换成 Json Array 或者 Json Object
142
+ if ( cellJson )
143
+ {
144
+ string cellText = value . ToString ( ) . Trim ( ) ;
145
+ if ( cellText . StartsWith ( "[" ) || cellText . StartsWith ( "{" ) )
146
+ {
147
+ try
148
+ {
149
+ object cellJsonObj = JsonConvert . DeserializeObject ( cellText ) ;
150
+ if ( cellJsonObj != null )
151
+ value = cellJsonObj ;
152
+ }
153
+ catch ( Exception exp )
154
+ {
155
+ }
156
+ }
157
+ }
158
+
141
159
if ( value . GetType ( ) == typeof ( System . DBNull ) )
142
160
{
143
161
value = getColumnDefault ( sheet , column , firstDataRow ) ;
0 commit comments