Beispiel #1
0
        public static bool Export(string PluginName, string ExcelPath, string SheetName, string OutputPath)
        {
            DataTable Table = null;

            //Microsoft.Office.Interop.Excel.Worksheet Table = null;

            try
            {
                var Plugin = PluginManager.GetExportPluginWithName(PluginName);
                if (Plugin == null)
                {
                    throw new Exception($"{PluginName}格式导出插件创建失败");
                }

                Table = ExcelAceHelper.OpenExcel(ExcelPath, ref SheetName, true);
                if (Table == null)
                {
                    throw new Exception($"{ExcelPath}表{SheetName}页读取错误");
                }

                var RowCount = ExcelAceHelper.GetExcelRowCount(Table); // 表的行数

                var Data = new IExportPlugin.ExportData
                {
                    Name       = SheetName,
                    InputPath  = ExcelPath,
                    OutputPath = OutputPath,
                    Data       = new List <List <string> >()
                };

                for (var Index = 0; Index < RowCount; ++Index)
                {
                    var Line = ExcelAceHelper.GetExcelDataLine(Table, Index);
                    Data.Data.Add(Line);
                }

                var CheckCode = Plugin.Check(Data);
                if (!string.IsNullOrWhiteSpace(CheckCode))
                {
                    throw new Exception(CheckCode);
                }

                return(Plugin.Export(Data));
            }
            catch (Exception Ex)
            {
                Logger.Error(Ex.Message);
                return(false);
            }
            finally
            {
                Table?.Dispose();
            }
        }
Beispiel #2
0
        public bool Export(IExportPlugin.ExportData data)
        {
            FileStream file = null;

            try
            {
                string fullPath = $"{data.OutputPath}{data.Name}.{GetExportExt()}";
                file = new FileStream(fullPath, FileMode.Create, FileAccess.Write);

                // 0 : descript
                // 1 : key
                // 2 : type
                // 3 - n : data

                FileUtil.WriteInt8(file, (byte)data.Data[0].Count);

                foreach (string key in data.Data[1])
                {
                    FileUtil.WriteString2(file, key);
                }

                foreach (string type in data.Data[2])
                {
                    if (type == "int")
                    {
                        FileUtil.WriteInt8(file, 1);
                    }
                    else if (type == "short")
                    {
                        FileUtil.WriteInt8(file, 2);
                    }
                    else if (type == "byte")
                    {
                        FileUtil.WriteInt8(file, 3);
                    }
                    else if (type == "bool")
                    {
                        FileUtil.WriteInt8(file, 4);
                    }
                    else
                    {
                        FileUtil.WriteInt8(file, 5);
                    }
                }

                for (int n = 3; n < data.Data.Count; ++n)
                {
                    for (int i = 0; i < data.Data[n].Count; ++i)
                    {
                        if (data.Data[2][i] == "int")
                        {
                            FileUtil.WriteInt32(file, int.Parse(data.Data[n][i]));
                        }
                        else if (data.Data[2][i] == "short")
                        {
                            FileUtil.WriteInt16(file, short.Parse(data.Data[n][i]));
                        }
                        else if (data.Data[2][i] == "byte")
                        {
                            FileUtil.WriteInt8(file, byte.Parse(data.Data[n][i]));
                        }
                        else if (data.Data[2][i] == "bool")
                        {
                            FileUtil.WriteBool(file, bool.Parse(data.Data[n][i]));
                        }
                        else
                        {
                            FileUtil.WriteString2(file, data.Data[n][i]);
                        }
                    }
                }

                return(true);
            }
            catch
            {
                return(false);
            }
            finally
            {
                FileUtil.SafeRelease(file);
            }
        }
Beispiel #3
0
        public string Check(IExportPlugin.ExportData data)
        {
            try
            {
                // 类型检测
                for (int i = 0; i < data.Data[2].Count; ++i)
                {
                    string type = data.Data[2][i];

                    if (type != "string" && type != "int" && type != "short" && type != "byte" && type != "bool")
                    {
                        return($"{data.Name}表 第{i + 1}列不支持的类型{type}");
                    }
                }

                // 空单元格检测
                for (int n = 3; n < data.Data.Count; ++n)
                {
                    for (int i = 0; i < data.Data[n].Count; ++i)
                    {
                        if (string.IsNullOrWhiteSpace(data.Data[n][i]))
                        {
                            return($"{data.Name}表 第{n + 1}行第{i + 1}列为空");
                        }

                        if (data.Data[2][i] != "string" && data.Data[n][i].Contains(" "))
                        {
                            return($"{data.Name}表 第{n + 1}行第{i + 1}列包含空");
                        }

                        if (data.Data[2][i] == "string")
                        {
                            if (string.IsNullOrWhiteSpace(data.Data[n][i]))
                            {
                                return($"{data.Name}表 第{n + 1}行第{i + 1}列为空");
                            }
                        }
                        else if (data.Data[2][i] == "int")
                        {
                            int v = 0;
                            if (!int.TryParse(data.Data[n][i], out v))
                            {
                                return($"{data.Name}表 第{n + 1}行第{i + 1}列不是int型");
                            }
                        }
                        else if (data.Data[2][i] == "short")
                        {
                            short v = 0;
                            if (!short.TryParse(data.Data[n][i], out v))
                            {
                                return($"{data.Name}表 第{n + 1}行第{i + 1}列不是short型");
                            }
                        }
                        else if (data.Data[2][i] == "byte")
                        {
                            byte v = 0;
                            if (!byte.TryParse(data.Data[n][i], out v))
                            {
                                return($"{data.Name}表 第{n + 1}行第{i + 1}列不是byte型");
                            }
                        }
                        else if (data.Data[2][i] == "bool")
                        {
                            bool v = true;
                            if (!bool.TryParse(data.Data[n][i], out v))
                            {
                                return($"{data.Name}表 第{n + 1}行第{i + 1}列不是bool型");
                            }
                        }
                    }
                }

                // key重复检测
                List <string> keys = new List <string>();
                for (int n = 3; n < data.Data.Count; ++n)
                {
                    string key   = data.Data[n][0];
                    int    index = keys.IndexOf(key);

                    if (index != -1)
                    {
                        throw new Exception($"{data.Name}表 第{index + 4}行与第{n + 1}行 Id重复 {key}");
                    }

                    keys.Add(key);
                }

                return(string.Empty);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #4
0
        public string Check(IExportPlugin.ExportData Data)
        {
            try
            {
                // 0 : descript
                // 3 : key
                // 4 : type
                // 10 - n : data

                var Find = false;
                for (var RowIndex = 0; RowIndex < 4 && !Find; ++RowIndex)
                {
                    for (var ColumnIndex = 0; ColumnIndex < Data.Data[RowIndex].Count && !Find; ++ColumnIndex)
                    {
                        if (Data.Data[RowIndex][ColumnIndex].StartsWith("!"))
                        {
                            KeyRow       = RowIndex;
                            TypeRow      = RowIndex + 1;
                            DataStartRow = RowIndex + 7;
                            Find         = true;
                            break;
                        }
                    }
                }

                // 类型检测
                for (var Index = 0; Index < Data.Data[KeyRow].Count; ++Index)
                {
                    var Type = Data.Data[TypeRow][Index];

                    if (string.IsNullOrWhiteSpace(Type))
                    {
                        continue;
                    }

                    // 0:number
                    // 1:string
                    // 3:bool
                    // 2:array
                    // 4:doublearray
                    if (Type != "0" && Type != "1" && Type != "3" && Type != "20" && Type != "21" && Type != "23" && Type != "40" && Type != "41" && Type != "43")
                    {
                        return($"{Data.Name}表 第{Index + 1}列不支持的类型{Type}");
                    }
                }

                // 空单元格检测
                for (var RowIndex = DataStartRow; RowIndex < Data.Data.Count; ++RowIndex)
                {
                    for (var ColumnIndex = 0; ColumnIndex < Data.Data[RowIndex].Count; ++ColumnIndex)
                    {
                        if (string.IsNullOrWhiteSpace(Data.Data[TypeRow][ColumnIndex]))
                        {
                            continue;
                        }

                        if (string.IsNullOrWhiteSpace(Data.Data[RowIndex][ColumnIndex]))
                        {
                            return($"{Data.Name}表 第{RowIndex + 1}行第{ColumnIndex + 1}列为空");
                        }

                        if (Data.Data[TypeRow][ColumnIndex] != "1" && Data.Data[RowIndex][ColumnIndex].Contains(" "))
                        {
                            return($"{Data.Name}表 第{RowIndex + 1}行第{ColumnIndex + 1}列包含空");
                        }

                        if (Data.Data[TypeRow][ColumnIndex].Contains("1"))
                        {
                            if (string.IsNullOrWhiteSpace(Data.Data[RowIndex][ColumnIndex]))
                            {
                                return($"{Data.Name}表 第{RowIndex + 1}行第{ColumnIndex + 1}列为空");
                            }
                        }
                        else if (Data.Data[TypeRow][ColumnIndex].Contains("0"))
                        {
                            if (Data.Data[RowIndex][ColumnIndex] == "null")
                            {
                                continue;
                            }

                            if (!double.TryParse(Data.Data[RowIndex][ColumnIndex], out double v))
                            {
                                return($"{Data.Name}表 第{RowIndex + 1}行第{ColumnIndex + 1}列不是数字");
                            }
                        }
                        else if (Data.Data[TypeRow][ColumnIndex].Contains("3"))
                        {
                            if (Data.Data[RowIndex][ColumnIndex] == "null")
                            {
                                continue;
                            }

                            if (!bool.TryParse(Data.Data[RowIndex][ColumnIndex], out bool v))
                            {
                                return($"{Data.Name}表 第{RowIndex + 1}行第{ColumnIndex + 1}列不是boolean型");
                            }
                        }
                    }
                }

                // key重复检测
                var Keys = new List <string>();
                for (var Index = DataStartRow; Index < Data.Data.Count; ++Index)
                {
                    var Key      = Data.Data[Index][0];
                    var OldIndex = Keys.IndexOf(Key);

                    if (OldIndex != -1)
                    {
                        return($"{Data.Name}表 第{Index + 1}行与第{OldIndex + 1}行 Id重复 {Key}");
                    }

                    Keys.Add(Key);
                }

                return(string.Empty);
            }
            catch (Exception Ex)
            {
                return(Ex.Message);
            }
        }
Beispiel #5
0
        public bool Export(IExportPlugin.ExportData Data)
        {
            try
            {
                var FullPath = $"{Data.OutputPath}{Data.Name}.{GetExportExt()}";
                OutStream_ = new StreamWriter(FullPath, false, new UTF8Encoding(false));
                Data_      = Data.Data;

                // 0 : descript
                // 3 : key
                // 4 : type
                // 10 - n : data

                var Find = false;
                for (var RowIndex = 0; RowIndex < 4 && !Find; ++RowIndex)
                {
                    for (var ColumnIndex = 0; ColumnIndex < Data_[RowIndex].Count && !Find; ++ColumnIndex)
                    {
                        if (Data_[RowIndex][ColumnIndex].StartsWith("!"))
                        {
                            KeyRow         = RowIndex;
                            TypeRow        = RowIndex + 1;
                            DataStartRow   = RowIndex + 7;
                            MainKeyColumn_ = ColumnIndex;
                            Find           = true;
                            break;
                        }
                    }
                }

                for (var Index = 0; Index < Data_[KeyRow].Count; ++Index)
                {
                    if (Data_[KeyRow][Index].StartsWith("!"))
                    {
                        MainKeyColumn_       = Index;
                        Data_[KeyRow][Index] = Data_[KeyRow][Index].Substring(1);
                        break;
                    }
                }

                OutStream_.Write($"local {Data.Name} = " + "{\n");

                for (var RowIndex = DataStartRow; RowIndex < Data.Data.Count; ++RowIndex)
                {
                    WriteTableLine(RowIndex, 1);
                }

                OutStream_.Write("}\n\n");
                OutStream_.Write($"return {Data.Name}");

                return(true);
            }
            catch
            {
                return(false);
            }
            finally
            {
                OutStream_?.Close();
                OutStream_?.Dispose();
                OutStream_ = null;
            }
        }