/// <summary>
        /// 导出枚举类。
        /// </summary>
        /// <param name="excelEnum"></param>
        public void ExportEnumCode(ExcelEnum excelEnum)
        {
            WriteLog(LogLevel.Debug, $"开始导出枚举表: {excelEnum.EnumClassName}");
            string filename = _csharpEnumCodeOutDirectory
                              + Path.DirectorySeparatorChar
                              + _codeGenerateClassPrefix
                              + excelEnum.EnumClassName + ".cs";

            using (FileStream fileStream = new FileStream(filename, FileMode.Create))
                using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
                {
                    streamWriter.WriteLine($"namespace {_chsarpCodeNameSpace}");
                    streamWriter.WriteLine("{");
                    if (excelEnum.HeritType.Equals(""))
                    {
                        streamWriter.WriteLine($"    public enum {excelEnum.EnumClassName}");
                    }
                    else
                    {
                        streamWriter.WriteLine($"    public enum {excelEnum.EnumClassName}: {excelEnum.HeritType}");
                    }
                    streamWriter.WriteLine("    {");
                    foreach (var enumInfo in excelEnum.EnumInfos)
                    {
                        streamWriter.WriteLine($"        /// <summary>{enumInfo.Description}</summary>");
                        streamWriter.WriteLine($"        {enumInfo.Name} = {enumInfo.Value}, ");
                    }
                    streamWriter.WriteLine("    }");
                    streamWriter.WriteLine("}");
                }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 检查枚举字段列。
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="columnIndex"></param>
        /// <param name="excelEnum"></param>
        /// <returns></returns>
        private int CheckColumnEnumsValue(string columnName, int columnIndex, ExcelEnum excelEnum)
        {
            int    error = 0;
            string value = "";

            for (int i = 0; i < _excelDataTable.DataRowCount; i++)
            {
                try
                {
                    value = _excelDataTable.GetColumnValue(i, columnIndex);
                    if (!excelEnum.HasEnumValue(Integer.parseInt(value)))
                    {
                        error += 1;
                        throw new Exception($"未发现枚举值 {value}");
                    }
                }
                catch (Exception e)
                {
                    _excelContext.WriteLog(LogLevel.Error, $"表格 {_excelDataTable.DataFileName} {columnName} 列, {i+1} 行, 配置错误: {e.getMessage()}");
                }
            }

            return(error);
        }