Exemple #1
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);
                }


                if (book.Tables.Count > 0)
                {
                    foreach (DataTable sheet in book.Tables)
                    {
                        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;
                                }
                            }
                        }

                        if (sheet.TableName == "Config")
                        {
                        }
                        else
                        {
                            //-- 导出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);
                            }

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

                                JavaDefineGenerator exporter = new JavaDefineGenerator(sheet);
                                //exporter.ClassComment = string.Format("// Generate From {0}", excelName);
                                exporter.SaveToFile(options.JavaPath, cd);
                            }
                        }
                    }
                }
            }
        }
Exemple #2
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);
                }
            }
        }