Program.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.IO;
  3. using System.Data;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. namespace excel2json
  7. {
  8. /// <summary>
  9. /// 应用程序
  10. /// </summary>
  11. sealed partial class Program
  12. {
  13. /// <summary>
  14. /// 应用程序入口
  15. /// </summary>
  16. /// <param name="args">命令行参数</param>
  17. [STAThread]
  18. static void Main(string[] args)
  19. {
  20. if (args.Length <= 0)
  21. {
  22. //-- GUI MODE ----------------------------------------------------------
  23. Console.WriteLine("Launch excel2json GUI Mode...");
  24. Application.EnableVisualStyles();
  25. Application.SetCompatibleTextRenderingDefault(false);
  26. Application.Run(new GUI.MainForm());
  27. }
  28. else
  29. {
  30. //-- COMMAND LINE MODE -------------------------------------------------
  31. //-- 分析命令行参数
  32. var options = new Options();
  33. var parser = new CommandLine.Parser(with => with.HelpWriter = Console.Error);
  34. if (parser.ParseArgumentsStrict(args, options, () => Environment.Exit(-1)))
  35. {
  36. //-- 执行导出操作
  37. try
  38. {
  39. DateTime startTime = DateTime.Now;
  40. Run(options);
  41. //-- 程序计时
  42. DateTime endTime = DateTime.Now;
  43. TimeSpan dur = endTime - startTime;
  44. Console.WriteLine(
  45. string.Format("[{0}]:\tConversion complete in [{1}ms].",
  46. Path.GetFileName(options.ExcelPath),
  47. dur.TotalMilliseconds)
  48. );
  49. }
  50. catch (Exception exp)
  51. {
  52. Console.WriteLine("Error: " + exp.Message);
  53. }
  54. }
  55. }// end of else
  56. }
  57. /// <summary>
  58. /// 根据命令行参数,执行Excel数据导出工作
  59. /// </summary>
  60. /// <param name="options">命令行参数</param>
  61. private static void Run(Options options)
  62. {
  63. //-- Excel File
  64. string excelPath = options.ExcelPath;
  65. string excelName = Path.GetFileNameWithoutExtension(options.ExcelPath);
  66. //-- Header
  67. int header = options.HeaderRows;
  68. //-- Encoding
  69. Encoding cd = new UTF8Encoding(false);
  70. if (options.Encoding != "utf8-nobom")
  71. {
  72. foreach (EncodingInfo ei in Encoding.GetEncodings())
  73. {
  74. Encoding e = ei.GetEncoding();
  75. if (e.HeaderName == options.Encoding)
  76. {
  77. cd = e;
  78. break;
  79. }
  80. }
  81. }
  82. //-- Date Format
  83. string dateFormat = options.DateFormat;
  84. //-- Export path
  85. string exportPath;
  86. if (options.JsonPath != null && options.JsonPath.Length > 0)
  87. {
  88. exportPath = options.JsonPath;
  89. }
  90. else
  91. {
  92. exportPath = Path.ChangeExtension(excelPath, ".json");
  93. }
  94. //-- Load Excel
  95. ExcelLoader excel = new ExcelLoader(excelPath, header);
  96. //-- export
  97. JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson);
  98. exporter.SaveToFile(exportPath, cd);
  99. //-- 生成C#定义文件
  100. if (options.CSharpPath != null && options.CSharpPath.Length > 0)
  101. {
  102. CSDefineGenerator generator = new CSDefineGenerator(excelName, excel, options.ExcludePrefix);
  103. generator.SaveToFile(options.CSharpPath, cd);
  104. }
  105. }
  106. }
  107. }