/// <summary>
        /// 导出cs代码。
        /// </summary>
        /// <param name="excelDataTable"></param>
        public void ExportDataTable(ExcelDataTable excelDataTable)
        {
            WriteLog(LogLevel.Debug, $"开始导出数据表: {excelDataTable.DataFileName}");
            string classname = StringUtils.ToCamel(excelDataTable.DataFileName);
            string filename  = _csharpTableCodeOutDirectory
                               + Path.DirectorySeparatorChar
                               + _codeGenerateClassPrefix
                               + classname + ".cs";

            using (FileStream fileStream = new FileStream(filename, FileMode.Create))
                using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
                {
                    streamWriter.WriteLine("using System;");
                    streamWriter.WriteLine("using System.IO;");
                    streamWriter.WriteLine("using System.Linq;");
                    streamWriter.WriteLine("using System.Collections.Generic;");
                    streamWriter.WriteLine("");
                    streamWriter.WriteLine($"namespace {_chsarpCodeNameSpace}");
                    streamWriter.WriteLine("{");
                    streamWriter.WriteLine($"    public partial class {classname}: DataRow");
                    streamWriter.WriteLine("    {");
                    // 字段.
                    foreach (var columnInfo in excelDataTable.GetColumnInfos(ColumnBelong.Client))
                    {
                        IColumnParser parser = _columnParserHelp.GetColumnParser(columnInfo.ColumnType);
                        streamWriter.WriteLine($"        /// <summary> {columnInfo.Title} </summary>");
                        streamWriter.WriteLine($"        public {parser.ToCSharpTypeString()} {columnInfo.Name}");
                        streamWriter.WriteLine("        {");
                        streamWriter.WriteLine("            get;");
                        streamWriter.WriteLine("            private set;");
                        streamWriter.WriteLine("        }");
                    }
                    // 解析函数
                    streamWriter.WriteLine($"        public override void ReadRowData(BinaryReader reader)");
                    streamWriter.WriteLine("        {");
                    foreach (var columnInfo in excelDataTable.GetColumnInfos(ColumnBelong.Client))
                    {
                        IColumnParser parser = _columnParserHelp.GetColumnParser(columnInfo.ColumnType);
                        streamWriter.WriteLine($"            {columnInfo.Name} = {parser.ReadFromBinaryReaderExpression()} ;");
                    }
                    streamWriter.WriteLine("        }");
                    // 主键函数
                    ColumnInfo pkInfo = excelDataTable.GetPrimaryColumnInfo();
                    streamWriter.WriteLine("        public override int GetPk()");
                    streamWriter.WriteLine("        {");
                    if (pkInfo == null)
                    {
                        streamWriter.WriteLine($"            return 0;");
                    }
                    else
                    {
                        streamWriter.WriteLine($"            return {pkInfo.Name};");
                    }
                    streamWriter.WriteLine("        }");
                    streamWriter.WriteLine("    }");
                    streamWriter.WriteLine("}");
                }
        }
예제 #2
0
        /// <summary>
        /// 检查主键是否有重复。
        /// </summary>
        /// <returns></returns>
        public int CheckPrimaryKey()
        {
            ColumnInfo primaryColumnsInfo = _excelDataTable.GetPrimaryColumnInfo();

            if (primaryColumnsInfo == null)
            {
                return(0);
            }
            return(CheckColumnUniqueValue(primaryColumnsInfo.Name, primaryColumnsInfo.ColumnIndex, false));
        }