Пример #1
0
    static void ExportCode(DataTable sheet, string path)
    {
        string       fileName   = Path.GetFileNameWithoutExtension(path);
        string       outputPath = mCodePath + fileName + ".cs";
        FileStream   fs         = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite);
        StreamWriter sw         = new StreamWriter(fs);

        DataRow nameRow    = sheet.Rows[0];
        DataRow commentRow = sheet.Rows[1];
        DataRow typeRow    = sheet.Rows[2];
        DataRow csRow      = sheet.Rows[3];
        int     rowNum     = sheet.Rows.Count;
        int     columnNum  = ExportExcelUtility.GetRealColumns(sheet.Columns.Count, nameRow);

        StringBuilder sb = new StringBuilder();

        sb.AppendLine("using System.Collections.Generic;");
        sb.AppendLine("namespace BinaryConfig");
        sb.AppendLine("{");
        sb.AppendFormat("    class {0} : IBinaryData\r\n", fileName);
        sb.AppendLine("    {");

        for (int i = 0; i < columnNum; i++)
        {
            if (csRow[i].ToString().Contains("c"))
            {
                sb.AppendLine("        /// <summary>");
                sb.AppendFormat("        /// {0}\r\n", System.Text.RegularExpressions.Regex.Replace(commentRow[i].ToString(), "[\r\n\t]", ""));
                sb.AppendLine("        /// <summary>");
                sb.AppendFormat("        private {0} m{1};\r\n", SwitchTypeToRealType(typeRow[i].ToString()), nameRow[i]);
                sb.AppendFormat("        public {0} {1}\r\n", SwitchTypeToRealType(typeRow[i].ToString()), nameRow[i]);
                sb.AppendLine("        {");
                sb.AppendLine("            get" + "{" + " return m" + nameRow[i] + ";}");
                sb.AppendLine("        }");
            }
        }

        sb.AppendLine("        public void Init(BinaryConfigRow row)");
        sb.AppendLine("        {");
        int index = 0;

        for (int i = 0; i < columnNum; i++)
        {
            if (csRow[i].ToString().Contains("c"))
            {
                sb.AppendFormat("            m{0} = row.GetFieldInfo({1}).{2}();\r\n", nameRow[i], index, SwitchTypeToFunName(typeRow[i].ToString()));
                index++;
            }
        }
        sb.AppendLine("        }");
        sb.AppendLine("    }");
        sb.AppendLine("}\r\n");
        sw.Write(sb);

        sw.Close();
        fs.Close();
    }
Пример #2
0
    static byte[] ExportByte(DataTable sheet, string path)
    {
        string outputPath = mBinaryPath + Path.GetFileNameWithoutExtension(path);
        //FileStream fs = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite);
        MemoryStream ms = new MemoryStream();
        BinaryWriter bw = new BinaryWriter(ms);

        DataRow nameRow    = sheet.Rows[0];
        DataRow commentRow = sheet.Rows[1];
        DataRow typeRow    = sheet.Rows[2];
        DataRow csRow      = sheet.Rows[3];
        int     rowNum     = sheet.Rows.Count;
        int     columnNum  = ExportExcelUtility.GetRealColumns(sheet.Columns.Count, nameRow);

        //写入行数
        bw.Write(rowNum - 4);
        //写入列数
        bw.Write(ExportExcelUtility.GetClientColumns(sheet.Columns.Count, csRow));
        //写入名字
        for (int i = 0; i < columnNum; i++)
        {
            if (csRow[i].ToString().Contains("c"))
            {
                bw.Write(nameRow[i].ToString());
            }
        }
        //写入类型
        for (int i = 0; i < columnNum; i++)
        {
            if (csRow[i].ToString().Contains("c"))
            {
                bw.Write((int)ExportExcelUtility.SwitchTypeToEnumType(typeRow[i].ToString()));
            }
        }
        //写入剩余数据
        for (int i = 4; i < rowNum; i++)
        {
            DataRow dataRow = sheet.Rows[i];
            for (int j = 0; j < columnNum; j++)
            {
                if (csRow[j].ToString().Contains("c"))
                {
                    string   strValue = dataRow[j].ToString();
                    DataType type     = ExportExcelUtility.SwitchTypeToEnumType(typeRow[j].ToString());
                    switch (type)
                    {
                    case DataType.eInt:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            int value = 0;
                            bw.Write(value);
                        }
                        else
                        {
                            int value = int.Parse(strValue);
                            bw.Write(value);
                        }
                    }
                    break;

                    case DataType.eFloat:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            float value = 0;
                            bw.Write(value);
                        }
                        else
                        {
                            float value = float.Parse(strValue);
                            bw.Write(value);
                        }
                    }
                    break;

                    case DataType.eString:
                    {
                        bw.Write(strValue);
                    }
                    break;

                    case DataType.eBool:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bool value = false;
                            bw.Write(value);
                        }
                        else
                        {
                            bool value = false;
                            strValue = strValue.ToLower();
                            if (strValue == "false")
                            {
                                value = false;
                            }
                            else if (strValue == "true")
                            {
                                value = true;
                            }
                            else
                            {
                                Debug.LogError(string.Format("{0},{1}的bool 值填写错误", i, j));
                                continue;
                            }

                            bw.Write(value);
                        }
                    }
                    break;

                    case DataType.eShort:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            short value = 0;
                            bw.Write(value);
                        }
                        else
                        {
                            short value = short.Parse(strValue);
                            bw.Write(value);
                        }
                    }
                    break;

                    case DataType.eIntList:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bw.Write(0);
                        }
                        else
                        {
                            string[] list = strValue.Split(',');
                            bw.Write(list.Length);
                            for (int k = 0; k < list.Length; k++)
                            {
                                int value = int.Parse(list[k]);
                                bw.Write(value);
                            }
                        }
                    }
                    break;

                    case DataType.eFloatList:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bw.Write(0);
                        }
                        else
                        {
                            string[] list = strValue.Split(',');
                            bw.Write(list.Length);
                            for (int k = 0; k < list.Length; k++)
                            {
                                float value = float.Parse(list[k]);
                                bw.Write(value);
                            }
                        }
                    }
                    break;

                    case DataType.eStringList:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bw.Write(0);
                        }
                        else
                        {
                            string[] list = strValue.Split(',');
                            bw.Write(list.Length);
                            for (int k = 0; k < list.Length; k++)
                            {
                                bw.Write(list[k]);
                            }
                        }
                    }
                    break;

                    case DataType.eBoolList:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bw.Write(0);
                        }
                        else
                        {
                            string[] list = strValue.Split(',');
                            bw.Write(list.Length);
                            for (int k = 0; k < list.Length; k++)
                            {
                                bool value = false;
                                if (list[k] == "true")
                                {
                                    value = true;
                                }
                                else if (list[k] == "false")
                                {
                                    value = false;
                                }
                                else
                                {
                                    Console.WriteLine("bool list value error");
                                }

                                bw.Write(value);
                            }
                        }
                    }
                    break;

                    case DataType.eIntIntDic:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bw.Write(0);
                        }
                        else
                        {
                            string[] list = strValue.Split(',');
                            bw.Write(list.Length);
                            for (int k = 0; k < list.Length; k++)
                            {
                                string[] pairs = list[k].Split(':');
                                int      key   = int.Parse(pairs[0]);
                                int      value = int.Parse(pairs[1]);
                                bw.Write(key);
                                bw.Write(value);
                            }
                        }
                    }
                    break;

                    case DataType.eIntStringDic:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bw.Write(0);
                        }
                        else
                        {
                            string[] list = strValue.Split(',');
                            bw.Write(list.Length);
                            for (int k = 0; k < list.Length; k++)
                            {
                                string[] pairs = list[k].Split(':');
                                int      key   = int.Parse(pairs[0]);
                                string   value = pairs[1];
                                bw.Write(key);
                                bw.Write(value);
                            }
                        }
                    }
                    break;

                    case DataType.eStringIntDic:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bw.Write(0);
                        }
                        else
                        {
                            string[] list = strValue.Split(',');
                            bw.Write(list.Length);
                            for (int k = 0; k < list.Length; k++)
                            {
                                string[] pairs = list[k].Split(':');
                                string   key   = pairs[0];
                                int      value = int.Parse(pairs[1]);
                                bw.Write(key);
                                bw.Write(value);
                            }
                        }
                    }
                    break;

                    case DataType.eStringStringDic:
                    {
                        if (string.IsNullOrEmpty(strValue))
                        {
                            bw.Write(0);
                        }
                        else
                        {
                            string[] list = strValue.Split(',');
                            bw.Write(list.Length);
                            for (int k = 0; k < list.Length; k++)
                            {
                                string[] pairs = list[k].Split(':');
                                string   key   = pairs[0];
                                string   value = pairs[1];
                                bw.Write(key);
                                bw.Write(value);
                            }
                        }
                    }
                    break;
                    }
                }
            }
        }

        bw.Flush();
        ms.Flush();

        return(ms.ToArray());
    }
Пример #3
0
    private static void ExportLua(DataTable sheet, string path)
    {
        string       fileName   = Path.GetFileNameWithoutExtension(path);
        string       outputPath = mCodePath + fileName + ".lua";
        FileStream   fs         = File.Open(outputPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        StreamWriter sw         = new StreamWriter(fs);

        StreamReader sr         = new StreamReader(fs);
        string       content    = sr.ReadToEnd();
        string       startFlag  = "--@start 自动导出,请勿修改";
        string       endFlag    = "--@end";
        int          endIndex   = content.IndexOf(endFlag);
        string       endContent = "";

        if (endIndex != -1)
        {
            endContent = content.Substring(endIndex + endFlag.Length + 2);
        }
        fs.Seek(0, SeekOrigin.Begin);
        fs.SetLength(0);

        DataRow nameRow    = sheet.Rows[0];
        DataRow commentRow = sheet.Rows[1];
        DataRow typeRow    = sheet.Rows[2];
        DataRow csRow      = sheet.Rows[3];
        int     rowNum     = sheet.Rows.Count;
        int     columnNum  = ExportExcelUtility.GetRealColumns(sheet.Columns.Count, nameRow);

        //寻找作为lua table的键值
        int keyColumn = -1;

        for (int i = 0; i < columnNum; i++)
        {
            string value = commentRow[i].ToString();
            if (value.ToLower() == "id")
            {
                keyColumn = i;
            }
        }
        string keyType = "";

        if (keyColumn != -1)
        {
            keyType = typeRow[keyColumn].ToString().ToLower();
        }



        StringBuilder sb = new StringBuilder();

        sb.AppendLine(startFlag);
        sb.AppendFormat("local {0} = {1}\r\n", fileName, "{");

        for (int i = 4; i < rowNum; i++)
        {
            DataRow dataRow = sheet.Rows[i];

            //处理该行的键值
            if (keyColumn != -1)
            {
                if (keyType == "int")
                {
                    sb.Append(string.Format("[{0}] = ", dataRow[keyColumn]));
                }
                else
                {
                    sb.Append(string.Format("[\"{0}\"] = ", dataRow[keyColumn]));
                }
            }
            else
            {
                sb.Append(string.Format("[{0}] = ", i - 4 + 1));
            }

            //导出一行数据
            sb.Append("{");
            for (int j = 0; j < columnNum; j++)
            {
                if (csRow[j].ToString().Contains("c"))
                {
                    string   strValue = dataRow[j].ToString();
                    DataType type     = ExportExcelUtility.SwitchTypeToEnumType(typeRow[j].ToString());
                    switch (type)
                    {
                    case DataType.eInt:
                    case DataType.eFloat:
                    case DataType.eBool:
                    case DataType.eShort:
                    {
                        sb.AppendFormat("{0} = {1}, ", nameRow[j], strValue);
                    }
                    break;

                    case DataType.eString:
                    {
                        sb.AppendFormat("{0} = \"{1}\", ", nameRow[j], strValue);
                    }
                    break;

                    case DataType.eIntList:
                    case DataType.eBoolList:
                    case DataType.eFloatList:
                    {
                        sb.AppendFormat("{0} = {1}{2}{3}, ", nameRow[j], "{", strValue, "}");
                    }
                    break;

                    case DataType.eStringList:
                    {
                        sb.AppendFormat("{0} = {1}", nameRow[j], "{");
                        if (!string.IsNullOrEmpty(strValue))
                        {
                            string[] list = strValue.Split(',');
                            for (int k = 0; k < list.Length; k++)
                            {
                                sb.AppendFormat("\"{0}\", ", list[k]);
                            }
                        }

                        sb.Append("},");
                    }
                    break;

                    case DataType.eIntIntDic:
                    {
                        sb.AppendFormat("{0} = {1}", nameRow[j], "{");
                        if (!string.IsNullOrEmpty(strValue))
                        {
                            string[] list = strValue.Split(',');
                            for (int k = 0; k < list.Length; k++)
                            {
                                string[] keyValues = list[k].Split(':');
                                if (keyValues.Length == 2)
                                {
                                    sb.AppendFormat("[{0}] = {1}, ", keyValues[0], keyValues[1]);
                                }
                            }
                        }
                        sb.Append("}, ");
                    }
                    break;

                    case DataType.eIntStringDic:
                    {
                        sb.AppendFormat("{0} = {1}", nameRow[j], "{");
                        if (!string.IsNullOrEmpty(strValue))
                        {
                            string[] list = strValue.Split(',');
                            for (int k = 0; k < list.Length; k++)
                            {
                                string[] keyValues = list[k].Split(':');
                                if (keyValues.Length == 2)
                                {
                                    sb.AppendFormat("[{0}] = \"{1}\", ", keyValues[0], keyValues[1]);
                                }
                            }
                        }
                        sb.Append("}, ");
                    }
                    break;

                    case DataType.eStringIntDic:
                    {
                        sb.AppendFormat("{0} = {1}", nameRow[j], "{");
                        if (!string.IsNullOrEmpty(strValue))
                        {
                            string[] list = strValue.Split(',');
                            for (int k = 0; k < list.Length; k++)
                            {
                                string[] keyValues = list[k].Split(':');
                                if (keyValues.Length == 2)
                                {
                                    sb.AppendFormat("{0} = {1}, ", keyValues[0], keyValues[1]);
                                }
                            }
                        }
                        sb.Append("}, ");
                    }
                    break;

                    case DataType.eStringStringDic:
                    {
                        sb.AppendFormat("{0} = {1}", nameRow[j], "{");
                        if (!string.IsNullOrEmpty(strValue))
                        {
                            string[] list = strValue.Split(',');
                            for (int k = 0; k < list.Length; k++)
                            {
                                string[] keyValues = list[k].Split(':');
                                if (keyValues.Length == 2)
                                {
                                    sb.AppendFormat("{0} = \"{1}\", ", keyValues[0], keyValues[1]);
                                }
                            }
                        }
                        sb.Append("}, ");
                    }
                    break;

                    default:
                    {
                        Debug.LogError(string.Format("配置表{0}, 第{1}列类型指定错误, 请检查配置表!", fileName, j + 1));
                        throw new System.ArgumentException("Parameter problem");
                    }
                    }
                }
            }
            sb.Append("},\r\n");
        }

        sb.AppendLine("}");
        sb.AppendLine(endFlag);
        sb.Append(endContent);
        string returnStr = string.Format("return {0}", fileName);

        if (!endContent.Contains(returnStr))
        {
            sb.Append(returnStr);
        }
        sw.Write(sb);

        sw.Close();
        fs.Close();
    }