예제 #1
0
        private static void CreateConfigClass(ExcelSheet sheet)
        {
            string        firstField = null;
            string        savePath   = $"{ExcelExportOutSetting.OutClassDir}/{sheet.ConfigName}.cs";
            StringBuilder fieldStrs  = new StringBuilder();

            fieldStrs.AppendLine("//------------------------------------------------------------");
            fieldStrs.AppendLine("// 此文件由工具自动生成,请勿直接修改。");
            fieldStrs.AppendLine("// 生成时间:" + DateTime.Now);
            fieldStrs.AppendLine("//------------------------------------------------------------");
            fieldStrs.AppendLine();
            fieldStrs.AppendLine("using System;");
            fieldStrs.AppendLine("using System.Collections.Generic;");
            fieldStrs.AppendLine();
            fieldStrs.AppendLine("namespace " + ExcelExportOutSetting.NameSpace);
            {
                fieldStrs.AppendLine("{");
                fieldStrs.AppendLine($"\t/// <summary>{sheet.NameDes}</summary>");
                fieldStrs.AppendLine($"\tpublic class {sheet.ConfigName} : BaseConfig");
                {
                    fieldStrs.AppendLine("\t{");
                    fieldStrs.AppendLine("\t\t/// <summary>唯一ID</summary>");
                    if (!sheet.IsVert)
                    {
                        fieldStrs.AppendLine("\t\tpublic override object UniqueID => id;");
                    }
                    else
                    {
                        fieldStrs.AppendLine($"\t\tpublic override object UniqueID => {sheet.Fields[0].Name};");
                    }


                    foreach (ExcelSheetTableField filed in sheet.Fields)
                    {
                        bool flag      = filed.IsInterface;
                        var  filedProp = flag ? " { get; set; }" : ";";

                        bool flag2 = firstField == null;
                        if (flag2)
                        {
                            firstField = filed.Name;
                        }

                        fieldStrs.AppendLine();
                        fieldStrs.AppendLine("\t\t/// <summary>");
                        string[] desStrings = filed.Des.Split('\n');
                        foreach (var variable in desStrings)
                        {
                            fieldStrs.AppendLine($"\t\t/// {variable}");
                        }
                        fieldStrs.AppendLine("\t\t/// </summary>");
                        fieldStrs.AppendLine(
                            $"\t\tpublic {ExcelUtil.GetCSStringType(filed.Type, false)} {filed.Name}{filedProp}");
                    }

                    fieldStrs.AppendLine("\t}");
                }
                fieldStrs.AppendLine("}");
            }
            ExcelUtil.SaveFile(savePath, fieldStrs.ToString(), true, true);
        }
예제 #2
0
        private static bool setSheetTable(ExcelSheet sheet, DataTable dtSource)
        {
            bool flag = dtSource.Columns.Count > 100;
            bool result;

            if (flag)
            {
                Debug.LogError(sheet.FullName + "表列数太多,请检查表格式");
                result = false;
            }
            else
            {
                bool isVert = sheet.IsVert;
                if (isVert)
                {
                    bool flag2 = dtSource.Rows.Count < 1 && dtSource.Columns.Count < 5;
                    if (flag2)
                    {
                        Debug.LogError(sheet.FullName + "表列数不对");
                        result = false;
                    }
                    else
                    {
                        int emptyRow = 0;
                        for (int i = 1; i < dtSource.Rows.Count; i++)
                        {
                            bool flag3 = dtSource.Rows[i][0].ToString() == string.Empty;
                            if (flag3)
                            {
                                emptyRow++;
                                bool flag4 = emptyRow > 3;
                                if (flag4)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                emptyRow = 0;
                                ExcelSheetTableField field = new ExcelSheetTableField();
                                field.Name        = dtSource.Rows[i][0].ToString();
                                field.Export      = dtSource.Rows[i][1].ToString();
                                field.Type        = dtSource.Rows[i][2].ToString().ToLower();
                                field.Value       = dtSource.Rows[i][3].ToString();
                                field.Des         = dtSource.Rows[i][4].ToString();
                                field.IsInterface = (field.Export.IndexOf('I') != -1);
                                sheet.Table.Columns.Add(field.Name, ExcelUtil.GetStringType(field.Type));
                                sheet.Fields.Add(field);
                            }
                        }

                        sheet.Export    = dtSource.Rows[1][1].ToString().ToUpper();
                        sheet.IsEncrypt = (sheet.Export.IndexOf('@') != -1);
                        result          = true;
                    }
                }
                else
                {
                    bool flag5 = dtSource.Rows.Count < 4 && dtSource.Columns.Count < 2;
                    if (flag5)
                    {
                        Debug.LogError(sheet.FullName + "表列数不对");
                        result = false;
                    }
                    else
                    {
                        ExcelSheetTableField field2 = null;
                        for (int j = 0; j < dtSource.Columns.Count; j++)
                        {
                            string[] arrInfo = dtSource.Rows[3][j].ToString().Split(new char[]
                            {
                                '#'
                            });
                            string fieldName = arrInfo[0];
                            bool   flag6     = fieldName == string.Empty;
                            if (flag6)
                            {
                                break;
                            }

                            bool flag7 = field2 == null || field2.Name != fieldName;
                            if (flag7)
                            {
                                field2             = new ExcelSheetTableField();
                                field2.Des         = dtSource.Rows[0][j].ToString();
                                field2.Export      = dtSource.Rows[1][j].ToString();
                                field2.Type        = dtSource.Rows[2][j].ToString().ToLower();
                                field2.Name        = fieldName;
                                field2.IsInterface = (field2.Export.IndexOf('I') != -1);
                                field2.FieldCount  = 1;
                                sheet.Fields.Add(field2);
                                sheet.Table.Columns.Add(field2.Name, ExcelUtil.GetStringType(field2.Type));
                            }
                            else
                            {
                                ExcelSheetTableField excelSheetTableField = field2;
                                excelSheetTableField.Des =
                                    excelSheetTableField.Des + "\n" + dtSource.Rows[0][j].ToString();
                                field2.FieldCount++;
                            }
                        }

                        sheet.Export    = dtSource.Rows[1][0].ToString().ToUpper();
                        sheet.IsEncrypt = (sheet.Export.IndexOf('@') != -1);
                        result          = true;
                    }
                }
            }

            return(result);
        }
예제 #3
0
        private static void addSheetTableRow(ExcelSheet sheet, DataTable dtSource)
        {
            bool isVert = sheet.IsVert;

            if (isVert)
            {
                DataRow expRow = sheet.Table.NewRow();
                for (int c = 0; c < sheet.Fields.Count; c++)
                {
                    ExcelSheetTableField field = sheet.Fields[c];
                    bool flag = field.Export == string.Empty;
                    if (!flag)
                    {
                        object val   = ExcelUtil.GetObjectValue(field.Type, field.Value, 1);
                        bool   flag2 = val == null;
                        if (flag2)
                        {
                            Debug.LogWarning(sheet.FullName + "表," + field.Name + "字段" + field.Type +
                                             "类型未写转换规则,已转换为string类型");
                            val = string.Empty;
                        }

                        expRow[c] = val;
                    }
                }

                sheet.Table.Rows.Add(expRow);
            }
            else
            {
                for (int i = 4; i < dtSource.Rows.Count; i++)
                {
                    DataRow expRow2 = sheet.Table.NewRow();
                    bool    flag3   = string.IsNullOrEmpty(dtSource.Rows[i][0].ToString());
                    if (!flag3)
                    {
                        int cellIndex = 0;
                        for (int f = 0; f < sheet.Fields.Count; f++)
                        {
                            ExcelSheetTableField field2 = sheet.Fields[f];
                            bool flag4 = field2.Export == string.Empty;
                            if (flag4)
                            {
                                cellIndex++;
                            }
                            else
                            {
                                string cellVal = dtSource.Rows[i][cellIndex].ToString();
                                cellIndex++;
                                object val2  = ExcelUtil.GetObjectValue(field2.Type, cellVal, field2.FieldCount);
                                bool   flag5 = val2 == null;
                                if (flag5)
                                {
                                    Debug.LogWarning(sheet.FullName + "表," + field2.Name + "字段" + field2.Type +
                                                     "类型未写转换规则,已转换为string类型");
                                    val2 = string.Empty;
                                }

                                bool flag6 = field2.FieldCount > 1;
                                if (flag6)
                                {
                                    for (int j = 1; j < field2.FieldCount; j++)
                                    {
                                        ExcelUtil.AddListValue(field2.Type, dtSource.Rows[i][cellIndex].ToString(),
                                                               ref val2);
                                        cellIndex++;
                                    }
                                }
                                expRow2[f] = val2;
                            }
                        }

                        sheet.Table.Rows.Add(expRow2);
                    }
                }
            }
        }
예제 #4
0
        public static Dictionary <string, ExcelSheet> GetExcleSheet(List <string[]> fils)
        {
            Dictionary <string, ExcelSheet> dicSheet = new Dictionary <string, ExcelSheet>();

            foreach (string[] file in fils)
            {
                DataSet ds = ExcelUtil.ReadExcelSheetData(file[1]);
                foreach (object obj in ds.Tables)
                {
                    DataTable dt         = (DataTable)obj;
                    string[]  sheeltInfo = dt.TableName.Split(new char[]
                    {
                        '#'
                    });
                    bool flag2 = sheeltInfo.Length < 2;
                    if (!flag2)
                    {
                        string[] sheeltTypeInfo = sheeltInfo[0].Split(new char[]
                        {
                            '_'
                        });
                        string tableName = ExcelUtil.ToFirstUpper(sheeltTypeInfo[0]);
                        bool   isVert    = false;
                        bool   flag3     = sheeltTypeInfo.Length > 1;
                        if (flag3)
                        {
                            bool flag4 = sheeltTypeInfo[1].ToLower() == "v";
                            if (flag4)
                            {
                                isVert = true;
                            }
                        }

                        ExcelSheet sheet;
                        bool       flag5 = !dicSheet.TryGetValue(tableName, out sheet);
                        if (flag5)
                        {
                            sheet         = new ExcelSheet();
                            sheet.Name    = tableName;
                            sheet.NameDes = sheeltInfo[1];
                            bool flag6 = sheeltInfo.Length > 2;
                            if (flag6)
                            {
                                sheet.Interface = sheeltInfo[2];
                            }

                            sheet.IsVert   = isVert;
                            sheet.FileName = file[0];
                            sheet.Table    = new DataTable();
                            bool flag7 = !ExcelExportParse.setSheetTable(sheet, dt);
                            if (flag7)
                            {
                                return(null);
                            }

                            dicSheet.Add(tableName, sheet);
                        }
                        else
                        {
                            bool isVert2 = sheet.IsVert;
                            if (isVert2)
                            {
                                Debug.LogError("表" + sheet.FullName + "是竖表结构,不能做分表");
                                return(null);
                            }

                            ExcelSheet nSheet = new ExcelSheet();
                            nSheet.Table = new DataTable();
                            nSheet.Name  = dt.TableName;
                            bool flag8 = !ExcelExportParse.setSheetTable(nSheet, dt);
                            if (flag8)
                            {
                                return(null);
                            }

                            bool flag9 = sheet.IsVert != isVert || !ExcelExportParse.checkSheetColumns(sheet, nSheet);
                            if (flag9)
                            {
                                Debug.LogError("表" + sheet.FullName + "定义的表结构不一至,请检查!!!!,目标表:" + dt.TableName);
                                return(null);
                            }
                        }

                        ExcelExportParse.addSheetTableRow(sheet, dt);
                    }
                }
            }

            return(dicSheet);
        }