Skip to content

Commit

Permalink
Merge pull request #26 from dantezhu/master
Browse files Browse the repository at this point in the history
Merge PR #26
  • Loading branch information
neil3d authored Mar 31, 2020
2 parents b5aaf0c + 8d64cba commit c80cbdb
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 141 deletions.
2 changes: 1 addition & 1 deletion GUI/DataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void loadExcel(Program.Options options) {
ExcelLoader excel = new ExcelLoader(excelPath, header);

//-- 导出JSON
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat);
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName);
}
}
}
318 changes: 181 additions & 137 deletions GUI/MainForm.Designer.cs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public MainForm() {
this.comboBoxLowcase.SelectedIndex = 1;
this.comboBoxHeader.SelectedIndex = 1;
this.comboBoxDateFormat.SelectedIndex = 0;
this.comboBoxSheetName.SelectedIndex = 1;

this.comboBoxEncoding.Items.Clear();
this.comboBoxEncoding.Items.Add("utf8-nobom");
Expand Down Expand Up @@ -126,6 +127,7 @@ private void loadExcelAsync(string path) {
options.Lowcase = this.comboBoxLowcase.SelectedIndex == 0;
options.HeaderRows = int.Parse(this.comboBoxHeader.Text);
options.DateFormat = this.comboBoxDateFormat.Text;
options.ForceSheetName = this.comboBoxSheetName.SelectedIndex == 0;

//-- start import
this.backgroundWorker.RunWorkerAsync(options);
Expand Down
3 changes: 3 additions & 0 deletions GUI/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
<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="label6.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
4 changes: 2 additions & 2 deletions JsonExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public string context {
/// 构造函数:完成内部数据创建
/// </summary>
/// <param name="excel">ExcelLoader Object</param>
public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string dateFormat) {
public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string dateFormat, bool forceSheetName) {

List<DataTable> validSheets = new List<DataTable>();
for (int i = 0; i < excel.Sheets.Count; i++) {
Expand All @@ -37,7 +37,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
Formatting = Formatting.Indented
};

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

//-- convert to object
object sheetValue = convertSheet(validSheets[0], exportArray, lowcase);
Expand Down
8 changes: 8 additions & 0 deletions Program.Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public Options() {
this.Encoding = "utf8-nobom";
this.Lowcase = false;
this.ExportArray = false;
this.ForceSheetName = false;
}

[Option('e', "excel", Required = true, HelpText = "input excel file path.")]
Expand Down Expand Up @@ -56,6 +57,13 @@ public string DateFormat {
get;
set;
}

[Option('s', "sheet", Required = false, DefaultValue = false, HelpText = "export with sheet name, even there's only one sheet.")]
public bool ForceSheetName
{
get;
set;
}
}
}
}
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static void Run(Options options) {
ExcelLoader excel = new ExcelLoader(excelPath, header);

//-- export
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat);
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName);
exporter.SaveToFile(exportPath, cd);
}
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* -l, –lowcase (Default: false) 自动把字段名称转换成小写格式.
* -a 序列化成数组
* -d, --date:指定日期格式化字符串,例如:dd / MM / yyy hh: mm:ss
* -s 序列化时强制带上sheet name,即使只有一个sheet

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

0 comments on commit c80cbdb

Please sign in to comment.