1 Star 0 Fork 2

ZZQ/excel2json

forked from wwwlib/excel2json 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Program.cs 3.57 KB
一键复制 编辑 原始数据 按行查看 历史
neil3d 提交于 2019-02-03 13:00 . add Date Time Format string
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace excel2json {
/// <summary>
/// 应用程序
/// </summary>
sealed partial class Program {
/// <summary>
/// 应用程序入口
/// </summary>
/// <param name="args">命令行参数</param>
[STAThread]
static void Main(string[] args) {
if (args.Length <= 0) {
//-- GUI MODE ----------------------------------------------------------
Console.WriteLine("Launch excel2json GUI Mode...");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new GUI.MainForm());
}
else {
//-- COMMAND LINE MODE -------------------------------------------------
//-- 分析命令行参数
var options = new Options();
var parser = new CommandLine.Parser(with => with.HelpWriter = Console.Error);
if (parser.ParseArgumentsStrict(args, options, () => Environment.Exit(-1))) {
//-- 执行导出操作
try {
DateTime startTime = DateTime.Now;
Run(options);
//-- 程序计时
DateTime endTime = DateTime.Now;
TimeSpan dur = endTime - startTime;
Console.WriteLine(
string.Format("[{0}]:\tConversion complete in [{1}ms].",
Path.GetFileName(options.ExcelPath),
dur.TotalMilliseconds)
);
}
catch (Exception exp) {
Console.WriteLine("Error: " + exp.Message);
}
}
}// end of else
}
/// <summary>
/// 根据命令行参数,执行Excel数据导出工作
/// </summary>
/// <param name="options">命令行参数</param>
private static void Run(Options options) {
//-- Excel File
string excelPath = options.ExcelPath;
string excelName = Path.GetFileNameWithoutExtension(options.ExcelPath);
//-- Header
int header = options.HeaderRows;
//-- Encoding
Encoding cd = new UTF8Encoding(false);
if (options.Encoding != "utf8-nobom") {
foreach (EncodingInfo ei in Encoding.GetEncodings()) {
Encoding e = ei.GetEncoding();
if (e.HeaderName == options.Encoding) {
cd = e;
break;
}
}
}
//-- Date Format
string dateFormat = options.DateFormat;
//-- Export path
string exportPath;
if (options.JsonPath != null && options.JsonPath.Length > 0) {
exportPath = options.JsonPath;
}
else {
exportPath = Path.ChangeExtension(excelPath, ".json");
}
//-- Load Excel
ExcelLoader excel = new ExcelLoader(excelPath, header);
//-- export
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat);
exporter.SaveToFile(exportPath, cd);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhao_zi_qi/excel2json.git
git@gitee.com:zhao_zi_qi/excel2json.git
zhao_zi_qi
excel2json
excel2json
master

搜索帮助