Skip to content

Commit

Permalink
convert json in cell
Browse files Browse the repository at this point in the history
  • Loading branch information
neil3d committed Jul 25, 2020
1 parent f1f808b commit 94075bd
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 62 deletions.
Binary file modified Docs/ExampleData.xlsx
Binary file not shown.
Binary file added Docs/excel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Docs/gui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion GUI/DataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void loadExcel(Program.Options options)
mCSharp = new CSDefineGenerator(excelPath, excel, options.ExcludePrefix);

//-- 导出JSON
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName, header, options.ExcludePrefix);
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson);
}
}
}
95 changes: 53 additions & 42 deletions GUI/MainForm.Designer.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private void loadExcelAsync(string path)
options.DateFormat = this.comboBoxDateFormat.Text;
options.ForceSheetName = this.comboBoxSheetName.SelectedIndex == 0;
options.ExcludePrefix = this.textBoxExculdePrefix.Text;
options.CellJson = this.checkBoxCellJson.Checked;

//-- start import
this.backgroundWorker.RunWorkerAsync(options);
Expand Down
6 changes: 3 additions & 3 deletions GUI/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@
<metadata name="label6.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
<metadata name="label7.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
<metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>127, 17</value>
</metadata>
<metadata name="toolStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="label7.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
<metadata name="backgroundWorker.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>241, 17</value>
</metadata>
Expand Down
46 changes: 32 additions & 14 deletions JsonExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public string context {
/// 构造函数:完成内部数据创建
/// </summary>
/// <param name="excel">ExcelLoader Object</param>
public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string dateFormat, bool forceSheetName, int headerRows, string excludePrefix)
public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string dateFormat, bool forceSheetName, int headerRows, string excludePrefix, bool cellJson)
{
mHeaderRows = headerRows-1;
mHeaderRows = headerRows - 1;
List<DataTable> validSheets = new List<DataTable>();
for (int i = 0; i < excel.Sheets.Count; i++)
{
DataTable sheet = excel.Sheets[i];

// 过滤掉包含特定前缀的表单
string sheetName = sheet.TableName;
if (excludePrefix.Length>0 && sheetName.StartsWith(excludePrefix))
if (excludePrefix.Length > 0 && sheetName.StartsWith(excludePrefix))
continue;

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

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

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

Expand All @@ -72,15 +72,15 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
}
}

private object convertSheet(DataTable sheet, bool exportArray, bool lowcase, string excludePrefix)
private object convertSheet(DataTable sheet, bool exportArray, bool lowcase, string excludePrefix, bool cellJson)
{
if (exportArray)
return convertSheetToArray(sheet, lowcase, excludePrefix);
return convertSheetToArray(sheet, lowcase, excludePrefix, cellJson);
else
return convertSheetToDict(sheet, lowcase, excludePrefix);
return convertSheetToDict(sheet, lowcase, excludePrefix, cellJson);
}

private object convertSheetToArray(DataTable sheet, bool lowcase, string excludePrefix)
private object convertSheetToArray(DataTable sheet, bool lowcase, string excludePrefix, bool cellJson)
{
List<object> values = new List<object>();

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

values.Add(
convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix)
convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix, cellJson)
);
}

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

var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix);
var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix, cellJson);
// 多余的字段
// rowObject[ID] = ID;
importData[ID] = rowObject;
Expand All @@ -125,7 +125,7 @@ private object convertSheetToDict(DataTable sheet, bool lowcase, string excludeP
/// <summary>
/// 把一行数据转换成一个对象,每一列是一个属性
/// </summary>
private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow, string excludePrefix)
private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow, string excludePrefix, bool cellJson)
{
var rowData = new Dictionary<string, object>();
int col = 0;
Expand All @@ -138,6 +138,24 @@ private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row

object value = row[column];

// 尝试将单元格字符串转换成 Json Array 或者 Json Object
if (cellJson)
{
string cellText = value.ToString().Trim();
if (cellText.StartsWith("[") || cellText.StartsWith("{"))
{
try
{
object cellJsonObj = JsonConvert.DeserializeObject(cellText);
if (cellJsonObj != null)
value = cellJsonObj;
}
catch (Exception exp)
{
}
}
}

if (value.GetType() == typeof(System.DBNull))
{
value = getColumnDefault(sheet, column, firstDataRow);
Expand Down
6 changes: 6 additions & 0 deletions Program.Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public string ExcludePrefix {
get;
set;
}

[Option('l', "cell_json", Required = false, DefaultValue = false, HelpText = "convert json string in cell")]
public bool CellJson {
get;
set;
}
}
}
}
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static void Run(Options options)
ExcelLoader excel = new ExcelLoader(excelPath, header);

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

//-- 生成C#定义文件
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
* -a 序列化成数组
* -d, --date:指定日期格式化字符串,例如:dd / MM / yyy hh: mm:ss
* -s 序列化时强制带上sheet name,即使只有一个sheet
* -exclude_prefix: 导出时,排除掉包含指定前缀的表单和列,例如:-exclude_prefix #
* -cell_json:自动识别单元格中的Json对象和Json数组,Default:false


![Excel](./Docs/excel.png)
![GUI](./Docs/gui.png)
![CMd](./Docs/cmd.png)

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

0 comments on commit 94075bd

Please sign in to comment.