Exemplo n.º 1
0
        /// <summary>
        /// 根据命令行参数,执行Excel数据导出工作
        /// </summary>
        /// <param name="options">命令行参数</param>
        private static void Run(Options options)
        {
            //-- Excel File
            string excelPath = options.ExcelPath;
            string excelName = Path.GetFileNameWithoutExtension(options.ExcelPath);

            //-- Header
            int header = options.HeaderRows;

            //-- Encoding
            Encoding cd = new UTF8Encoding(false);

            if (options.Encoding != "utf8-nobom")
            {
                foreach (EncodingInfo ei in Encoding.GetEncodings())
                {
                    Encoding e = ei.GetEncoding();
                    if (e.HeaderName == options.Encoding)
                    {
                        cd = e;
                        break;
                    }
                }
            }

            //-- Date Format
            string dateFormat = options.DateFormat;

            //-- Export path
            string exportPath;

            if (options.JsonPath != null && options.JsonPath.Length > 0)
            {
                exportPath = options.JsonPath;
            }
            else
            {
                exportPath = Path.ChangeExtension(excelPath, ".json");
            }

            //-- Load Excel
            ExcelLoader excel = new ExcelLoader(excelPath, header);

            //-- export
            JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName, header, options.ExcludePrefix, options.CellJson);

            exporter.SaveToFile(exportPath, cd);

            //-- 生成C#定义文件
            if (options.CSharpPath != null && options.CSharpPath.Length > 0)
            {
                CSDefineGenerator generator = new CSDefineGenerator(excelName, excel, options.ExcludePrefix);
                generator.SaveToFile(options.CSharpPath, cd);
            }
        }
Exemplo n.º 2
0
        private static void Export2Json(string excelPath, int header, string encoding, string jsonPath, bool lowcase, string filter)
        {
            if (jsonPath == null || jsonPath == "")
            {
                return;
            }
            DataTable dt = ReadExcel(excelPath);
            Encoding  en = GetEncoding(encoding);

            //-- 导出JSON文件
            jsonPath = FormatPath(jsonPath);
            JsonExporter exporter = new JsonExporter(dt, header, lowcase, filter);

            exporter.SaveToFile(jsonPath, en);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 根据命令行参数,执行Excel数据导出工作
        /// </summary>
        /// <param name="options">命令行参数</param>
        private static void Run(string excelPath)
        {
            string file   = Path.GetFileNameWithoutExtension(excelPath);
            int    header = 1;

            // 加载Excel文件
            using (FileStream excelFile = File.Open(excelPath, FileMode.Open, FileAccess.Read))
            {
                // Reading from a OpenXml Excel file (2007 format; *.xlsx)
                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelFile);

                // The result of each spreadsheet will be created in the result.Tables
                excelReader.IsFirstRowAsColumnNames = true;
                DataSet book = excelReader.AsDataSet();

                // 数据检测
                if (book.Tables.Count < 1)
                {
                    throw new Exception("Excel文件中没有找到Sheet: " + excelPath);
                }

                // 取得数据
                DataTable sheet = book.Tables[0];
                if (sheet.Rows.Count <= 0)
                {
                    throw new Exception("Excel Sheet中没有数据: " + excelPath);
                }

                //-- UTF8编码
                Encoding cd = new UTF8Encoding(false);

                //-- 导出JSON文件
                JsonExporter exporter = new JsonExporter(sheet, header, false);
                exporter.SaveToFile("json/" + file + ".json", cd);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 根据命令行参数,执行Excel数据导出工作
        /// </summary>
        /// <param name="options">命令行参数</param>
        private static void Run(Options options)
        {
            string excelPath = options.ExcelPath;
            int    header    = options.HeaderRows;

            // 加载Excel文件
            using (FileStream excelFile = File.Open(excelPath, FileMode.Open, FileAccess.Read))
            {
                // Reading from a OpenXml Excel file (2007 format; *.xlsx)
                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelFile);

                // The result of each spreadsheet will be created in the result.Tables
                excelReader.IsFirstRowAsColumnNames = true;
                DataSet book = excelReader.AsDataSet();

                // 数据检测
                if (book.Tables.Count < 1)
                {
                    throw new Exception("Excel文件中没有找到Sheet: " + excelPath);
                }

                // 取得数据
                DataTable sheet = book.Tables[0];
                if (sheet.Rows.Count <= 0)
                {
                    throw new Exception("Excel Sheet中没有数据: " + excelPath);
                }

                //-- 确定编码
                Encoding cd = new UTF8Encoding(false);
                if (options.Encoding != "utf8-nobom")
                {
                    foreach (EncodingInfo ei in Encoding.GetEncodings())
                    {
                        Encoding e = ei.GetEncoding();
                        if (e.EncodingName == options.Encoding)
                        {
                            cd = e;
                            break;
                        }
                    }
                }

                //-- 导出JSON文件
                if (options.JsonPath != null && options.JsonPath.Length > 0)
                {
                    JsonExporter exporter = new JsonExporter(sheet, header, options.Lowcase);
                    exporter.SaveToFile(options.JsonPath, cd, options.ExportArray);
                }

                //-- 导出SQL文件
                if (options.SQLPath != null && options.SQLPath.Length > 0)
                {
                    SQLExporter exporter = new SQLExporter(sheet, header);
                    exporter.SaveToFile(options.SQLPath, cd);
                }

                //-- 生成C#定义文件
                if (options.CSharpPath != null && options.CSharpPath.Length > 0)
                {
                    string excelName = Path.GetFileName(excelPath);

                    CSDefineGenerator exporter = new CSDefineGenerator(sheet);
                    exporter.ClassComment = string.Format("// Generate From {0}", excelName);
                    exporter.SaveToFile(options.CSharpPath, cd);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 根据命令行参数,执行Excel数据导出工作
        /// </summary>
        /// <param name="options">命令行参数</param>
        private static void Run(Options options)
        {
            //-- Excel File
            string excelPath     = options.excelPath;
            string excelName     = Path.GetFileNameWithoutExtension(options.excelPath);
            var    excelFileInfo = new FileInfo(excelPath);

            if (!excelFileInfo.Exists)
            {
                throw new Exception("Excel文件不存在");
            }

            HashSet <string> exportTags = new HashSet <string>();

            exportTags.Add("");
            if (options.exportTags != null)
            {
                var tags = options.exportTags.Split(',');
                foreach (var tag in tags)
                {
                    exportTags.Add(tag);
                }
            }

            //-- Encoding
            Encoding cd = new UTF8Encoding(false);

            //-- Load Excel
            ExcelLoader excel = new ExcelLoader(excelPath);
            List <ExcelParser.TableInfo> tableInfos = null;

            //-- export json
            if (!string.IsNullOrEmpty(options.jsonDir))
            {
                var finalPath = Path.Combine(options.jsonDir, NameFormater.FormatFileName(excelName) + ".json");
                var fileInfo  = new FileInfo(finalPath);
                if (!fileInfo.Exists || fileInfo.LastWriteTimeUtc.Ticks < excelFileInfo.LastWriteTimeUtc.Ticks)
                {
                    if (tableInfos == null)
                    {
                        tableInfos = ExcelParser.ReadSheetData(excel.Sheets[0]);
                    }

                    JsonExporter jsonExporter = new JsonExporter(tableInfos, excelName, exportTags);
                    jsonExporter.SaveToFile(finalPath, cd);
                }
            }

            //-- export xml
            if (!string.IsNullOrEmpty(options.xmlDir))
            {
                var finalPath = Path.Combine(options.xmlDir, NameFormater.FormatCamelName(excelName, false) + ".xml");
                var fileInfo  = new FileInfo(finalPath);
                if (!fileInfo.Exists || fileInfo.LastWriteTimeUtc.Ticks < excelFileInfo.LastWriteTimeUtc.Ticks)
                {
                    if (tableInfos == null)
                    {
                        tableInfos = ExcelParser.ReadSheetData(excel.Sheets[0]);
                    }

                    XmlExporter xmlExporter = new XmlExporter(tableInfos, excelName, exportTags);
                    xmlExporter.SaveToFile(finalPath, cd);
                }
            }

            //-- export c#
            if (!string.IsNullOrEmpty(options.csharpDir))
            {
                var finalPath = Path.Combine(options.csharpDir, NameFormater.FormatCamelName(excelName, false) + ".cs");
                var fileInfo  = new FileInfo(finalPath);
                if (!fileInfo.Exists || fileInfo.LastWriteTimeUtc.Ticks < excelFileInfo.LastWriteTimeUtc.Ticks)
                {
                    if (tableInfos == null)
                    {
                        tableInfos = ExcelParser.ReadSheetData(excel.Sheets[0]);
                    }

                    CSharpExporter csharpExporter = new CSharpExporter(tableInfos, excelName, exportTags);
                    csharpExporter.SaveToFile(finalPath, cd);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 根据命令行参数,执行Excel数据导出工作
        /// </summary>
        /// <param name="options">命令行参数</param>
        private static void Run(Options options)
        {
            string excelPath = options.ExcelPath;
            int header = options.HeaderRows;

            // 加载Excel文件
            using (FileStream excelFile = File.Open(excelPath, FileMode.Open, FileAccess.Read))
            {
                // Reading from a OpenXml Excel file (2007 format; *.xlsx)
                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelFile);

                // The result of each spreadsheet will be created in the result.Tables
                excelReader.IsFirstRowAsColumnNames = true;
                DataSet book = excelReader.AsDataSet();

                // 数据检测
                if (book.Tables.Count < 1)
                {
                    throw new Exception("Excel文件中没有找到Sheet: " + excelPath);
                }

                // 取得数据
                DataTable sheet = book.Tables[0];
                if (sheet.Rows.Count <= 0)
                {
                    throw new Exception("Excel Sheet中没有数据: " + excelPath);
                }

                //-- 确定编码
                Encoding cd = new UTF8Encoding(false);
                if (options.Encoding != "utf8-nobom")
                {
                    foreach (EncodingInfo ei in Encoding.GetEncodings())
                    {
                        Encoding e = ei.GetEncoding();
                        if (e.EncodingName == options.Encoding)
                        {
                            cd = e;
                            break;
                        }
                    }
                }

                //-- 导出JSON文件
                if (options.JsonPath != null && options.JsonPath.Length > 0)
                {
                    JsonExporter exporter = new JsonExporter(sheet, header, options.Lowcase);
                    exporter.SaveToFile(options.JsonPath, cd);
                }

                //-- 导出SQL文件
                if (options.SQLPath != null && options.SQLPath.Length > 0)
                {
                    SQLExporter exporter = new SQLExporter(sheet, header);
                    exporter.SaveToFile(options.SQLPath, cd);
                }

                //-- 生成C#定义文件
                if (options.CSharpPath != null && options.CSharpPath.Length > 0)
                {
                    string excelName = Path.GetFileName(excelPath);

                    CSDefineGenerator exporter = new CSDefineGenerator(sheet);
                    exporter.ClassComment = string.Format("// Generate From {0}", excelName);
                    exporter.SaveToFile(options.CSharpPath, cd);
                }
            }
        }
Exemplo n.º 7
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.TxtCategory.Text.Trim()) ||
                String.IsNullOrEmpty(this.TxtDataPoint.Text.Trim()) ||
                String.IsNullOrEmpty(this.TxtExcel.Text.Trim()))
            {
                MessageBox.Show("Please fill the Excel, Category and Data Point information.");
            }
            else
            {
                DirectoryInfo diTestCaseDataPoint = CreateTestCaseDir();
                DirectoryInfo diEntityDataPoint   = CreateEntityDir();

                try
                {
                    string excelPath       = this.TxtExcel.Text.Trim();
                    String OutputExcelPath = Path.Combine(diTestCaseDataPoint.FullName, "Excel", new FileInfo(excelPath).Name);

                    File.Copy(excelPath, OutputExcelPath, true);

                    int header = 3;
                    // 加载Excel文件
                    using (FileStream excelFile = File.Open(excelPath, FileMode.Open, FileAccess.Read))
                    {
                        // Reading from a OpenXml Excel file (2007 format; *.xlsx)
                        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelFile);

                        // The result of each spreadsheet will be created in the result.Tables
                        excelReader.IsFirstRowAsColumnNames = true;
                        DataSet book = excelReader.AsDataSet();

                        // 数据检测
                        if (book.Tables.Count < 1)
                        {
                            throw new Exception("Excel文件中没有找到Sheet: " + excelPath);
                        }


                        foreach (DataTable sheet in book.Tables)
                        {
                            if (sheet.Rows.Count <= 0)
                            {
                                throw new Exception("Excel Sheet中没有数据: " + excelPath);
                            }

                            if (sheet.TableName == "Config")
                            {
                                //TODO:
                            }
                            else
                            {
                                String JsonPath = "";
                                if (sheet.TableName.ToLower().Contains("method") || sheet.TableName.ToLower().Contains("change"))
                                {
                                    JsonPath = Path.Combine(diTestCaseDataPoint.FullName, "MockData", sheet.TableName + "_" + this.TxtDataPoint.Text.Trim() + ".json");
                                    JsonExporter exporterForJson = new JsonExporter(sheet, header, false);
                                    exporterForJson.SaveToFile(JsonPath, new UTF8Encoding(false));

                                    String JavaPath = Path.Combine(diEntityDataPoint.FullName, sheet.TableName + ".java");
                                    JavaDefineGenerator exporterForJava = new JavaDefineGenerator(sheet);
                                    exporterForJava.SaveToFile(JavaPath, new UTF8Encoding(false), "." + this.TxtCategory.Text.Trim() + "." + this.TxtDataPoint.Text.Trim());
                                }
                                else if (sheet.TableName.ToLower().Contains("init"))
                                {
                                    JsonPath = Path.Combine(diTestCaseDataPoint.FullName, "InitData", sheet.TableName + "_" + this.TxtDataPoint.Text.Trim() + ".json");
                                    JsonExporter exporterForJson2 = new JsonExporter(sheet, header, false);
                                    exporterForJson2.SaveToFile(JsonPath, new UTF8Encoding(false));
                                }
                                else if (sheet.TableName.ToLower().Contains("clean"))
                                {
                                    JsonPath = Path.Combine(diTestCaseDataPoint.FullName, "CleanData", sheet.TableName + "_" + this.TxtDataPoint.Text.Trim() + ".json");
                                    JsonExporter exporterForJson = new JsonExporter(sheet, header, false);
                                    exporterForJson.SaveToFile(JsonPath, new UTF8Encoding(false));
                                }
                                else if (sheet.TableName.ToLower().Contains("delta"))
                                {
                                    JsonPath = Path.Combine(diTestCaseDataPoint.FullName, "DeltaData", sheet.TableName + "_" + this.TxtDataPoint.Text.Trim() + ".json");
                                    JsonExporter2 exporterForJson2 = new JsonExporter2(sheet, header, false);
                                    exporterForJson2.SaveToFile(JsonPath, new UTF8Encoding(false));
                                }
                            }
                        }
                    }
                    MessageBox.Show("Generate completed.", "Excel2Json", MessageBoxButtons.OK);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
            }
        }