先上工具地址
github
别的不说了,就说批处理的写法,注意,有的指令是要 “–”为不是“-”
-e, –excel Required. 输入的Excel文件路径.
-j, –json 指定输出的json文件路径.
-p, –csharp 指定输出的C#文件路径.
-h, –header (Default: 3)表格中有几行是表头.
-c, –encoding (Default: utf8-nobom) 指定编码的名称.
-l, –lowcase (Default: false) 自动把字段名称转换成小写格式.
-a 序列化成数组
-d, –date:指定日期格式化字符串,例如:dd / MM / yyy hh: mm:ss
-s 序列化时强制带上sheet name,即使只有一个sheet
-exclude_prefix: 导出时,排除掉包含指定前缀的表单和列,例如:-exclude_prefix #
-cell_json:自动识别单元格中的Json对象和Json数组,Default:false
@SET EXCEL_FOLDER=.\Excel
@SET JSON_FOLDER=.\Json
@SET EXE=.\excel2json\excel2json.exe
@ECHO Converting excel files in folder %EXCEL_FOLDER% ...
for /f "delims=" %%i in ('dir /b /a-d /s %EXCEL_FOLDER%\*.xlsx') do (
@echo processing %%~nxi
@CALL %EXE% --excel %EXCEL_FOLDER%\%%~nxi --json %JSON_FOLDER%\%%~ni.json --header 2 -a --cell_json
)
前一个指令-a和后一个指令 --cell_json是有空格的,没有空格是无法执行的
作者在2021年更新了一个Allstring功能,但批处理有问题,推荐用1.4.2版本的
namespace excel2json
{
partial class Program
{
/// <summary>
/// 命令行参数定义
/// </summary>
internal sealed class Options
{
public Options()
{
this.HeaderRows = 3;
this.Encoding = "utf8-nobom";
this.Lowcase = false;
this.ExportArray = false;
this.ForceSheetName = false;
}
[Option('e', "excel", Required = true, HelpText = "input excel file path.")]
public string ExcelPath {
get;
set;
}
[Option('j', "json", Required = false, HelpText = "export json file path.")]
public string JsonPath {
get;
set;
}
[Option('p', "csharp", Required = false, HelpText = "export C# data struct code file path.")]
public string CSharpPath {
get;
set;
}
[Option('h', "header", Required = false, DefaultValue = 1, HelpText = "number lines in sheet as header.")]
public int HeaderRows {
get;
set;
}
[Option('c', "encoding", Required = false, DefaultValue = "utf8-nobom", HelpText = "export file encoding.")]
public string Encoding {
get;
set;
}
[Option('l', "lowcase", Required = false, DefaultValue = false, HelpText = "convert filed name to lowcase.")]
public bool Lowcase {
get;
set;
}
[Option('a', "array", Required = false, DefaultValue = false, HelpText = "export as array, otherwise as dict object.")]
public bool ExportArray {
get;
set;
}
[Option('d', "date", Required = false, DefaultValue = "yyyy/MM/dd", HelpText = "Date Format String, example: dd / MM / yyy hh: mm:ss.")]
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;
}
[Option('x', "exclude_prefix", Required = false, DefaultValue = "", HelpText = "exclude sheet or column start with specified prefix.")]
public string ExcludePrefix {
get;
set;
}
[Option('l', "cell_json", Required = false, DefaultValue = false, HelpText = "convert json string in cell")]
public bool CellJson {
get;
set;
}
[Option('l', "all_string", Required = false, DefaultValue = false, HelpText = "all string")]
public bool AllString
{
get;
set;
}
}
}
}
觉得有用别忘了给这位大佬点个星星