Skip to content

Commit 94075bd

Browse files
committed
convert json in cell
1 parent f1f808b commit 94075bd

11 files changed

+104
-62
lines changed

Docs/ExampleData.xlsx

179 Bytes
Binary file not shown.

Docs/excel.png

64.8 KB
Loading

Docs/gui.png

288 Bytes
Loading

GUI/DataManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void loadExcel(Program.Options options)
9696
mCSharp = new CSDefineGenerator(excelPath, excel, options.ExcludePrefix);
9797

9898
//-- 导出JSON
99-
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName, header, options.ExcludePrefix);
99+
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson);
100100
}
101101
}
102102
}

GUI/MainForm.Designer.cs

+53-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GUI/MainForm.cs

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ private void loadExcelAsync(string path)
144144
options.DateFormat = this.comboBoxDateFormat.Text;
145145
options.ForceSheetName = this.comboBoxSheetName.SelectedIndex == 0;
146146
options.ExcludePrefix = this.textBoxExculdePrefix.Text;
147+
options.CellJson = this.checkBoxCellJson.Checked;
147148

148149
//-- start import
149150
this.backgroundWorker.RunWorkerAsync(options);

GUI/MainForm.resx

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@
135135
<metadata name="label6.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
136136
<value>False</value>
137137
</metadata>
138+
<metadata name="label7.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
139+
<value>False</value>
140+
</metadata>
138141
<metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
139142
<value>127, 17</value>
140143
</metadata>
141144
<metadata name="toolStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
142145
<value>17, 17</value>
143146
</metadata>
144-
<metadata name="label7.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
145-
<value>False</value>
146-
</metadata>
147147
<metadata name="backgroundWorker.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
148148
<value>241, 17</value>
149149
</metadata>

JsonExporter.cs

+32-14
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public string context {
2525
/// 构造函数:完成内部数据创建
2626
/// </summary>
2727
/// <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)
2929
{
30-
mHeaderRows = headerRows-1;
30+
mHeaderRows = headerRows - 1;
3131
List<DataTable> validSheets = new List<DataTable>();
3232
for (int i = 0; i < excel.Sheets.Count; i++)
3333
{
3434
DataTable sheet = excel.Sheets[i];
35-
35+
3636
// 过滤掉包含特定前缀的表单
3737
string sheetName = sheet.TableName;
38-
if (excludePrefix.Length>0 && sheetName.StartsWith(excludePrefix))
38+
if (excludePrefix.Length > 0 && sheetName.StartsWith(excludePrefix))
3939
continue;
4040

4141
if (sheet.Columns.Count > 0 && sheet.Rows.Count > 0)
@@ -52,7 +52,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
5252
{ // single sheet
5353

5454
//-- convert to object
55-
object sheetValue = convertSheet(validSheets[0], exportArray, lowcase, excludePrefix);
55+
object sheetValue = convertSheet(validSheets[0], exportArray, lowcase, excludePrefix, cellJson);
5656

5757
//-- convert to json string
5858
mContext = JsonConvert.SerializeObject(sheetValue, jsonSettings);
@@ -63,7 +63,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
6363
Dictionary<string, object> data = new Dictionary<string, object>();
6464
foreach (var sheet in validSheets)
6565
{
66-
object sheetValue = convertSheet(sheet, exportArray, lowcase, excludePrefix);
66+
object sheetValue = convertSheet(sheet, exportArray, lowcase, excludePrefix, cellJson);
6767
data.Add(sheet.TableName, sheetValue);
6868
}
6969

@@ -72,15 +72,15 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
7272
}
7373
}
7474

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)
7676
{
7777
if (exportArray)
78-
return convertSheetToArray(sheet, lowcase, excludePrefix);
78+
return convertSheetToArray(sheet, lowcase, excludePrefix, cellJson);
7979
else
80-
return convertSheetToDict(sheet, lowcase, excludePrefix);
80+
return convertSheetToDict(sheet, lowcase, excludePrefix, cellJson);
8181
}
8282

83-
private object convertSheetToArray(DataTable sheet, bool lowcase, string excludePrefix)
83+
private object convertSheetToArray(DataTable sheet, bool lowcase, string excludePrefix, bool cellJson)
8484
{
8585
List<object> values = new List<object>();
8686

@@ -90,7 +90,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase, string exclude
9090
DataRow row = sheet.Rows[i];
9191

9292
values.Add(
93-
convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix)
93+
convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix, cellJson)
9494
);
9595
}
9696

@@ -100,7 +100,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase, string exclude
100100
/// <summary>
101101
/// 以第一列为ID,转换成ID->Object的字典对象
102102
/// </summary>
103-
private object convertSheetToDict(DataTable sheet, bool lowcase, string excludePrefix)
103+
private object convertSheetToDict(DataTable sheet, bool lowcase, string excludePrefix, bool cellJson)
104104
{
105105
Dictionary<string, object> importData =
106106
new Dictionary<string, object>();
@@ -113,7 +113,7 @@ private object convertSheetToDict(DataTable sheet, bool lowcase, string excludeP
113113
if (ID.Length <= 0)
114114
ID = string.Format("row_{0}", i);
115115

116-
var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix);
116+
var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix, cellJson);
117117
// 多余的字段
118118
// rowObject[ID] = ID;
119119
importData[ID] = rowObject;
@@ -125,7 +125,7 @@ private object convertSheetToDict(DataTable sheet, bool lowcase, string excludeP
125125
/// <summary>
126126
/// 把一行数据转换成一个对象,每一列是一个属性
127127
/// </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)
129129
{
130130
var rowData = new Dictionary<string, object>();
131131
int col = 0;
@@ -138,6 +138,24 @@ private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row
138138

139139
object value = row[column];
140140

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+
141159
if (value.GetType() == typeof(System.DBNull))
142160
{
143161
value = getColumnDefault(sheet, column, firstDataRow);

Program.Options.cs

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public string ExcludePrefix {
7979
get;
8080
set;
8181
}
82+
83+
[Option('l', "cell_json", Required = false, DefaultValue = false, HelpText = "convert json string in cell")]
84+
public bool CellJson {
85+
get;
86+
set;
87+
}
8288
}
8389
}
8490
}

Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private static void Run(Options options)
106106
ExcelLoader excel = new ExcelLoader(excelPath, header);
107107

108108
//-- export
109-
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName, header, options.ExcludePrefix);
109+
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson);
110110
exporter.SaveToFile(exportPath, cd);
111111

112112
//-- 生成C#定义文件

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
* -a 序列化成数组
1313
* -d, --date:指定日期格式化字符串,例如:dd / MM / yyy hh: mm:ss
1414
* -s 序列化时强制带上sheet name,即使只有一个sheet
15+
* -exclude_prefix: 导出时,排除掉包含指定前缀的表单和列,例如:-exclude_prefix #
16+
* -cell_json:自动识别单元格中的Json对象和Json数组,Default:false
17+
18+
19+
![Excel](./Docs/excel.png)
20+
![GUI](./Docs/gui.png)
21+
![CMd](./Docs/cmd.png)
1522

16-
![COVER](https://neil3d.github.io/assets/img/excel2json/cover.jpg)
1723

0 commit comments

Comments
 (0)