Ejemplo n.º 1
0
        private Hash DoCompilerExcelReader(string path, SimpleExcelFile excelFile, string compileToFilePath = null)
        {
            //3. DataSet - The result of each spreadsheet will be created in the result.Tables
            //DataSet result = excelReader.AsDataSet();

            //4. DataSet - Create column names from first row
            //excelFile.IsFirstRowAsColumnNames = true;


            //DataSet result = excelFile.AsDataSet();

            //if (result.Tables.Count <= 0)
            //    throw new InvalidExcelException("No Sheet!");


            var renderVars = new RenderTemplateVars();

            renderVars.FieldsInternal = new List <RenderFieldVars>();

            //var sheet1 = result.Tables[0];

            var strBuilder = new StringBuilder();

            var ignoreColumns = new HashSet <int>();
            var ignoreRows    = new HashSet <int>();

            //// 寻找注释行,1,或2行
            //var hasStatementRow = false;
            //var statementRow = sheet1.Rows[0].ItemArray;
            var regExCheckStatement = new Regex(@"\[(.*)\]");

            //foreach (var cellVal in statementRow)
            //{
            //    if ((cellVal is string))
            //    {
            //        var matches = regExCheckStatement.Matches(cellVal.ToString());
            //        if (matches.Count > 0)
            //        {
            //            hasStatementRow = true;
            //        }
            //    }

            //    break;
            //}

            //// 获取注释行
            //var commentRow = hasStatementRow ? sheet1.Rows[1].ItemArray : sheet1.Rows[0].ItemArray;
            //var commentsOfColumns = new List<string>();
            //foreach (var cellVal in commentRow)
            //{
            //    commentsOfColumns.Add(cellVal.ToString());
            //}

            // Header Column
            foreach (var colNameStr in excelFile.ColName2Index.Keys)
            {
                var colIndex = excelFile.ColName2Index[colNameStr];
                if (!string.IsNullOrEmpty(colNameStr))
                {
                    var isCommentColumn = CheckCommentColumn(colNameStr);
                    if (isCommentColumn)
                    {
                        ignoreColumns.Add(colIndex);
                    }
                    else
                    {
                        if (colIndex > 0)
                        {
                            strBuilder.Append("\t");
                        }
                        strBuilder.Append(colNameStr);

                        string typeName   = "string";
                        string defaultVal = "";

                        var attrs = excelFile.ColName2Statement[colNameStr].Split(',');
                        // Type
                        if (attrs.Length > 0)
                        {
                            typeName = attrs[0];
                        }
                        // Default Value
                        if (attrs.Length > 1)
                        {
                            defaultVal = attrs[1];
                        }
                        if (attrs.Length > 2)
                        {
                            if (attrs[2] == "pk")
                            {
                                renderVars.PrimaryKey = colNameStr;
                            }
                        }

                        renderVars.FieldsInternal.Add(new RenderFieldVars
                        {
                            Index        = colIndex,
                            Type         = typeName,
                            Name         = colNameStr,
                            DefaultValue = defaultVal,
                            Comment      = excelFile.ColName2Comment[colNameStr],
                        });
                    }
                }
            }
            strBuilder.Append("\n");

            // Statements rows, keeps
            foreach (var kv in excelFile.ColName2Statement)
            {
                var colName      = kv.Key;
                var statementStr = kv.Value;
                var colIndex     = excelFile.ColName2Index[colName];

                if (ignoreColumns.Contains(colIndex)) // comment column, ignore
                {
                    continue;
                }
                if (colIndex > 0)
                {
                    strBuilder.Append("\t");
                }
                strBuilder.Append(statementStr);
            }
            strBuilder.Append("\n");

            // Data Rows
            //var rowIndex = 1;
            //foreach (DataRow dRow in sheet1.Rows)
            for (var startRow = 0; startRow < excelFile.GetRowsCount(); startRow++)
            {
                var columnCount = excelFile.GetColumnCount();
                for (var loopColumn = 0; loopColumn < columnCount; loopColumn++)
                {
                    if (ignoreColumns.Contains(loopColumn)) // comment column, ignore
                    {
                        continue;
                    }
                    if (loopColumn > 0)
                    {
                        strBuilder.Append("\t");
                    }
                    var columnName = excelFile.Index2ColName[loopColumn];
                    var cellStr    = excelFile.GetString(columnName, startRow);
                    //        // 如果单元格是字符串,换行符改成\\n
                    //        if (item is string)
                    //        {
                    //            var sItme = item as string;
                    //            cloneItem = sItme.Replace("\n", "\\n");
                    //        }
                    //        strBuilder.Append(cloneItem);
                    //        colIndex++;

                    cellStr = cellStr.Replace("\n", "\\n");
                    strBuilder.Append(cellStr);
                }
                strBuilder.Append("\n");
            }
            //if (hasStatementRow)
            //{
            //    // 有声明行,忽略第2行
            //    if (rowIndex == 2)
            //    {
            //            rowIndex++;
            //            continue;

            //        }
            //    }
            //    else
            //    {
            //        // 无声明行,忽略第1行
            //        if (rowIndex == 1)
            //        {
            //            rowIndex++;
            //            continue;
            //        }
            //    }

            //    colIndex = 0;
            //foreach (var item in dRow.ItemArray)
            //    {
            //        if (ignoreColumns.Contains(colIndex)) // comment column, ignore
            //            continue;

            //        if (colIndex > 0)
            //            strBuilder.Append("\t");

            //        var cloneItem = item;
            //        // 如果单元格是字符串,换行符改成\\n
            //        if (item is string)
            //        {
            //            var sItme = item as string;
            //            cloneItem = sItme.Replace("\n", "\\n");
            //        }
            //        strBuilder.Append(cloneItem);
            //        colIndex++;
            //    }
            //    strBuilder.Append("\n");
            //    rowIndex++;
            //}

            var    fileName = Path.GetFileNameWithoutExtension(path);
            string exportPath;

            if (!string.IsNullOrEmpty(compileToFilePath))
            {
                exportPath = compileToFilePath;
            }
            else
            {
                // use default
                exportPath = string.Format("{0}{1}", fileName, _config.ExportTabExt);
            }
            File.WriteAllText(exportPath, strBuilder.ToString());


            renderVars.ClassName   = string.Join("", (from name in fileName.Split('_') select System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(name)).ToArray());
            renderVars.TabFilePath = exportPath;

            return(Hash.FromAnonymousObject(renderVars));
        }
Ejemplo n.º 2
0
        private Hash DoCompilerExcelReader(string path, IExcelDataReader excelReader, string compileToFilePath = null)
        {
            //3. DataSet - The result of each spreadsheet will be created in the result.Tables
            //DataSet result = excelReader.AsDataSet();

            //4. DataSet - Create column names from first row
            excelReader.IsFirstRowAsColumnNames = true;
            DataSet result = excelReader.AsDataSet();

            if (result.Tables.Count <= 0)
                throw new InvalidExcelException("No Sheet!");

            var renderVars = new RenderTemplateVars();
            renderVars.FieldsInternal = new List<RenderFieldVars>();

            var sheet1 = result.Tables[0];

            var strBuilder = new StringBuilder();

            var ignoreColumns = new HashSet<int>();
            var ignoreRows = new HashSet<int>();

            // 寻找注释行,1,或2行
            var hasStatementRow = false;
            var statementRow = sheet1.Rows[0].ItemArray;
            var regExCheckStatement = new Regex(@"\[(.*)\]");
            foreach (var cellVal in statementRow)
            {
                if ((cellVal is string))
                {
                    var matches = regExCheckStatement.Matches(cellVal.ToString());
                    if (matches.Count > 0)
                    {
                        hasStatementRow = true;
                    }
                }

                break;
            }

            // 获取注释行
            var commentRow = hasStatementRow ? sheet1.Rows[1].ItemArray : sheet1.Rows[0].ItemArray;
            var commentsOfColumns = new List<string>();
            foreach (var cellVal in commentRow)
            {
                commentsOfColumns.Add(cellVal.ToString());
            }

            // Header
            int colIndex = 0;
            foreach (DataColumn column in sheet1.Columns)
            {
                var colNameStr = column.ColumnName.Trim();
                if (!string.IsNullOrEmpty(colNameStr))
                {
                    var isCommentColumn = false;
                    foreach (var commentStartsWith in _config.CommentColumnStartsWith)
                    {
                        if (colNameStr.StartsWith(commentStartsWith))
                        {
                            isCommentColumn = true;
                            break;
                        }
                    }
                    if (isCommentColumn)
                    {
                        ignoreColumns.Add(colIndex);
                    }
                    else
                    {
                        if (colIndex > 0)
                            strBuilder.Append("\t");
                        strBuilder.Append(colNameStr);

                        string typeName = "string";
                        string defaultVal = "";

                        if (hasStatementRow)
                        {
                            var match = regExCheckStatement.Match(statementRow[colIndex].ToString());
                            var attrs = match.Groups[1].ToString().Split(':');
                            // Type

                            if (attrs.Length > 0)
                            {
                                typeName = attrs[0];
                            }
                            // Default Value
                            if (attrs.Length > 1)
                            {
                                defaultVal = attrs[1];
                            }
                            if (attrs.Length > 2)
                            {
                                if (attrs[2] == "pk")
                                {
                                    renderVars.PrimaryKey = colNameStr;
                                }
                            }

                        }

                        renderVars.FieldsInternal.Add(new RenderFieldVars
                        {
                            Index = colIndex,
                            Type = typeName,
                            Name = colNameStr,
                            DefaultValue = defaultVal,
                            Comment = commentsOfColumns[colIndex],
                        });
                        //codeGentor.Columns2DefaultValus.Add(colNameStr, defaultVal);
                    }
                }
                colIndex++;
            }
            strBuilder.Append("\n");

            // Rows
            var rowIndex = 1;
            foreach (DataRow dRow in sheet1.Rows)
            {
                if (hasStatementRow)
                {
                    // 有声明行,忽略第2行
                    if (rowIndex == 2)
                    {
                        rowIndex++;
                        continue;

                    }
                }
                else
                {
                    // 无声明行,忽略第1行
                    if (rowIndex == 1)
                    {
                        rowIndex++;
                        continue;
                    }
                }

                colIndex = 0;
                foreach (var item in dRow.ItemArray)
                {
                    if (ignoreColumns.Contains(colIndex)) // comment column, ignore
                        continue;

                    if (colIndex > 0)
                        strBuilder.Append("\t");

                    var cloneItem = item;
                    // 如果单元格是字符串,换行符改成\\n
                    if (item is string)
                    {
                        var sItme = item as string;
                        cloneItem = sItme.Replace("\n", "\\n");
                    }
                    strBuilder.Append(cloneItem);
                    colIndex++;
                }
                strBuilder.Append("\n");
                rowIndex++;
            }

            var fileName = Path.GetFileNameWithoutExtension(path);
            string exportPath;
            if (!string.IsNullOrEmpty(compileToFilePath))
            {
                exportPath = compileToFilePath;
            }
            else
            {
                // use default
                exportPath = string.Format("{0}{1}", fileName, _config.ExportTabExt);
            }
            File.WriteAllText(exportPath, strBuilder.ToString());

            renderVars.ClassName = string.Join("", (from name in fileName.Split('_') select System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(name)).ToArray());
            renderVars.TabFilePath = exportPath;

            return Hash.FromAnonymousObject(renderVars);
        }
Ejemplo n.º 3
0
        private Hash DoCompilerExcelReader(string path, IExcelDataReader excelReader, string compileToFilePath = null)
        {
            //3. DataSet - The result of each spreadsheet will be created in the result.Tables
            //DataSet result = excelReader.AsDataSet();

            //4. DataSet - Create column names from first row
            excelReader.IsFirstRowAsColumnNames = true;
            DataSet result = excelReader.AsDataSet();

            if (result.Tables.Count <= 0)
            {
                throw new InvalidExcelException("No Sheet!");
            }


            var renderVars = new RenderTemplateVars();

            renderVars.FieldsInternal = new List <RenderFieldVars>();

            var sheet1 = result.Tables[0];

            var strBuilder = new StringBuilder();

            var ignoreColumns = new HashSet <int>();
            var ignoreRows    = new HashSet <int>();

            // 寻找注释行,1,或2行
            var hasStatementRow     = false;
            var statementRow        = sheet1.Rows[0].ItemArray;
            var regExCheckStatement = new Regex(@"\[(.*)\]");

            foreach (var cellVal in statementRow)
            {
                if ((cellVal is string))
                {
                    var matches = regExCheckStatement.Matches(cellVal.ToString());
                    if (matches.Count > 0)
                    {
                        hasStatementRow = true;
                    }
                }

                break;
            }

            // 获取注释行
            var commentRow        = hasStatementRow ? sheet1.Rows[1].ItemArray : sheet1.Rows[0].ItemArray;
            var commentsOfColumns = new List <string>();

            foreach (var cellVal in commentRow)
            {
                commentsOfColumns.Add(cellVal.ToString());
            }

            // Header
            int colIndex = 0;

            foreach (DataColumn column in sheet1.Columns)
            {
                var colNameStr = column.ColumnName.Trim();
                if (!string.IsNullOrEmpty(colNameStr))
                {
                    var isCommentColumn = false;
                    foreach (var commentStartsWith in _config.CommentColumnStartsWith)
                    {
                        if (colNameStr.StartsWith(commentStartsWith))
                        {
                            isCommentColumn = true;
                            break;
                        }
                    }
                    if (isCommentColumn)
                    {
                        ignoreColumns.Add(colIndex);
                    }
                    else
                    {
                        if (colIndex > 0)
                        {
                            strBuilder.Append("\t");
                        }
                        strBuilder.Append(colNameStr);

                        string typeName   = "string";
                        string defaultVal = "";

                        if (hasStatementRow)
                        {
                            var match = regExCheckStatement.Match(statementRow[colIndex].ToString());
                            var attrs = match.Groups[1].ToString().Split(':');
                            // Type

                            if (attrs.Length > 0)
                            {
                                typeName = attrs[0];
                            }
                            // Default Value
                            if (attrs.Length > 1)
                            {
                                defaultVal = attrs[1];
                            }
                            if (attrs.Length > 2)
                            {
                                if (attrs[2] == "pk")
                                {
                                    renderVars.PrimaryKey = colNameStr;
                                }
                            }
                        }

                        renderVars.FieldsInternal.Add(new RenderFieldVars
                        {
                            Index        = colIndex,
                            Type         = typeName,
                            Name         = colNameStr,
                            DefaultValue = defaultVal,
                            Comment      = commentsOfColumns[colIndex],
                        });
                        //codeGentor.Columns2DefaultValus.Add(colNameStr, defaultVal);
                    }
                }
                colIndex++;
            }
            strBuilder.Append("\n");

            // Rows
            var rowIndex = 1;

            foreach (DataRow dRow in sheet1.Rows)
            {
                if (hasStatementRow)
                {
                    // 有声明行,忽略第2行
                    if (rowIndex == 2)
                    {
                        rowIndex++;
                        continue;
                    }
                }
                else
                {
                    // 无声明行,忽略第1行
                    if (rowIndex == 1)
                    {
                        rowIndex++;
                        continue;
                    }
                }

                colIndex = 0;
                foreach (var item in dRow.ItemArray)
                {
                    if (ignoreColumns.Contains(colIndex)) // comment column, ignore
                    {
                        continue;
                    }

                    if (colIndex > 0)
                    {
                        strBuilder.Append("\t");
                    }

                    var cloneItem = item;
                    // 如果单元格是字符串,换行符改成\\n
                    if (item is string)
                    {
                        var sItme = item as string;
                        cloneItem = sItme.Replace("\n", "\\n");
                    }
                    strBuilder.Append(cloneItem);
                    colIndex++;
                }
                strBuilder.Append("\n");
                rowIndex++;
            }

            var    fileName = Path.GetFileNameWithoutExtension(path);
            string exportPath;

            if (!string.IsNullOrEmpty(compileToFilePath))
            {
                exportPath = compileToFilePath;
            }
            else
            {
                // use default
                exportPath = string.Format("{0}{1}", fileName, _config.ExportTabExt);
            }
            File.WriteAllText(exportPath, strBuilder.ToString());


            renderVars.ClassName   = string.Join("", (from name in fileName.Split('_') select System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(name)).ToArray());
            renderVars.TabFilePath = exportPath;

            return(Hash.FromAnonymousObject(renderVars));
        }
Ejemplo n.º 4
0
        private Hash DoCompilerExcelReader(string path, SimpleExcelFile excelFile, string compileToFilePath = null, string compileBaseDir = null)
        {
            //3. DataSet - The result of each spreadsheet will be created in the result.Tables
            //DataSet result = excelReader.AsDataSet();

            //4. DataSet - Create column names from first row
            //excelFile.IsFirstRowAsColumnNames = true;


            //DataSet result = excelFile.AsDataSet();

            //if (result.Tables.Count <= 0)
            //    throw new InvalidExcelException("No Sheet!");


            var renderVars = new RenderTemplateVars();
            renderVars.FieldsInternal = new List<RenderFieldVars>();

            //var sheet1 = result.Tables[0];

            var strBuilder = new StringBuilder();

            var ignoreColumns = new HashSet<int>();
            //var ignoreRows = new HashSet<int>();

            //// 寻找注释行,1,或2行
            //var hasStatementRow = false;
            //var statementRow = sheet1.Rows[0].ItemArray;
            //var regExCheckStatement = new Regex(@"\[(.*)\]"); // 不要[ xx ]符号了
            //foreach (var cellVal in statementRow)
            //{
            //    if ((cellVal is string))
            //    {
            //        var matches = regExCheckStatement.Matches(cellVal.ToString());
            //        if (matches.Count > 0)
            //        {
            //            hasStatementRow = true;
            //        }
            //    }

            //    break;
            //}

            //// 获取注释行
            //var commentRow = hasStatementRow ? sheet1.Rows[1].ItemArray : sheet1.Rows[0].ItemArray;
            //var commentsOfColumns = new List<string>();
            //foreach (var cellVal in commentRow)
            //{
            //    commentsOfColumns.Add(cellVal.ToString());
            //}

            // Header Column
            foreach (var colNameStr in excelFile.ColName2Index.Keys)
            {
                var colIndex = excelFile.ColName2Index[colNameStr];
                if (!string.IsNullOrEmpty(colNameStr))
                {
                    var isCommentColumn = CheckCommentString(colNameStr);
                    if (isCommentColumn)
                    {
                        ignoreColumns.Add(colIndex);
                    }
                    else
                    {
                        if (colIndex > 0)
                            strBuilder.Append("\t");
                        strBuilder.Append(colNameStr);

                        string typeName = "string";
                        string defaultVal = "";

                        var attrs = excelFile.ColName2Statement[colNameStr].Split(new char[] {'|', '/'}, StringSplitOptions.RemoveEmptyEntries);
                        // Type
                        if (attrs.Length > 0)
                        {
                            typeName = attrs[0];
                        }
                        // Default Value
                        if (attrs.Length > 1)
                        {
                            defaultVal = attrs[1];
                        }
                        if (attrs.Length > 2)
                        {
                            if (attrs[2] == "pk")
                            {
                                renderVars.PrimaryKey = colNameStr;
                            }
                        }

                        renderVars.FieldsInternal.Add(new RenderFieldVars
                        {
                            Index = colIndex,
                            Type = typeName,
                            Name = colNameStr,
                            DefaultValue = defaultVal,
                            Comment = excelFile.ColName2Comment[colNameStr],
                        });
                    }
                }
            }
            strBuilder.Append("\n");

            // Statements rows, keeps
            foreach (var kv in excelFile.ColName2Statement)
            {
                var colName = kv.Key;
                var statementStr = kv.Value;
                var colIndex = excelFile.ColName2Index[colName];

                if (ignoreColumns.Contains(colIndex)) // comment column, ignore
                    continue;
                if (colIndex > 0)
                    strBuilder.Append("\t");
                strBuilder.Append(statementStr);
            }
            strBuilder.Append("\n");

            // Data Rows
            //var rowIndex = 1;
            //foreach (DataRow dRow in sheet1.Rows)
            for (var startRow = 0; startRow < excelFile.GetRowsCount(); startRow++)
            {
                var columnCount = excelFile.GetColumnCount();
                for (var loopColumn = 0; loopColumn < columnCount; loopColumn++)
                {
                    if (ignoreColumns.Contains(loopColumn)) // comment column, ignore
                        continue;
                    if (loopColumn > 0)
                        strBuilder.Append("\t");
                    var columnName = excelFile.Index2ColName[loopColumn];
                    
                    var cellStr = excelFile.GetString(columnName, startRow);

                    if (loopColumn == 0 && CheckCommentString(cellStr)) // 如果行首为#注释字符,忽略这一行
                        continue;

                    //        // 如果单元格是字符串,换行符改成\\n
                    //        if (item is string)
                    //        {
                    //            var sItme = item as string;
                    //            cloneItem = sItme.Replace("\n", "\\n");
                    //        }
                    //        strBuilder.Append(cloneItem);
                    //        colIndex++;

                    cellStr = cellStr.Replace("\n", "\\n");
                    strBuilder.Append(cellStr);
                }
                strBuilder.Append("\n");
            }
            //if (hasStatementRow)
            //{
            //    // 有声明行,忽略第2行
            //    if (rowIndex == 2)
            //    {
            //            rowIndex++;
            //            continue;

            //        }
            //    }
            //    else
            //    {
            //        // 无声明行,忽略第1行
            //        if (rowIndex == 1)
            //        {
            //            rowIndex++;
            //            continue;
            //        }
            //    }

            //    colIndex = 0;
            //foreach (var item in dRow.ItemArray)
            //    {
            //        if (ignoreColumns.Contains(colIndex)) // comment column, ignore
            //            continue;

            //        if (colIndex > 0)
            //            strBuilder.Append("\t");

            //        var cloneItem = item;
            //        // 如果单元格是字符串,换行符改成\\n
            //        if (item is string)
            //        {
            //            var sItme = item as string;
            //            cloneItem = sItme.Replace("\n", "\\n");
            //        }
            //        strBuilder.Append(cloneItem);
            //        colIndex++;
            //    }
            //    strBuilder.Append("\n");
            //    rowIndex++;
            //}

            var fileName = Path.GetFileNameWithoutExtension(path);
            string exportPath;
            if (!string.IsNullOrEmpty(compileToFilePath))
            {
                exportPath = compileToFilePath;
            }
            else
            {
                // use default
                exportPath = string.Format("{0}{1}", fileName, _config.ExportTabExt);
            }

            var exportDirPath = Path.GetDirectoryName(exportPath);
            if (!Directory.Exists(exportDirPath))
                Directory.CreateDirectory(exportDirPath);
            File.WriteAllText(exportPath, strBuilder.ToString());


            // 基于base dir路径
            var tabFilePath = exportPath;
            if (!string.IsNullOrEmpty(compileBaseDir))
            {
                tabFilePath = tabFilePath.Replace(compileBaseDir, ""); // 保留后戳
            }
            if (tabFilePath.StartsWith("/"))
                tabFilePath = tabFilePath.Substring(1);

            renderVars.ClassName = string.Join("",
                (from name in fileName.Split('_')
                 select System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(name)).ToArray());
            renderVars.TabFilePath = tabFilePath;

            return Hash.FromAnonymousObject(renderVars);
        }