Beispiel #1
0
 /// <summary>
 /// 保存SQL
 /// </summary>
 /// <param name="filePath">保存路径</param>
 public void saveSQL(string filePath)
 {
     if (mSQL != null)
     {
         mSQL.SaveToFile(filePath, mEncoding);
     }
 }
Beispiel #2
0
        static void Export(Options options)
        {
            string excelPath = options.ExcelPath;
            int    header    = options.HeaderRows;

            Console.WriteLine(string.Format("export {0}", excelPath));
            // 加载Excel文件
            using (FileStream excelFile = File.Open(excelPath, FileMode.Open, FileAccess.Read))
            {
                IExcelDataReader excelReader = null;
                string           extension   = Path.GetExtension(excelPath);
                if (string.IsNullOrEmpty(extension) == false)
                {
                    extension = extension.ToLower();
                }

                if (extension == ".xlsx")
                {
                    excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelFile);
                }
                else
                {
                    throw new Exception("仅支持xlsx格式。");
                }
                string fileName = Path.GetFileNameWithoutExtension(excelPath);

                // 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");
                }

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

                //-- 确定编码
                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.json || options.xml)
                {
                    JsonExporter exporter = new JsonExporter(sheet, header, options.Lowcase);
                    if (options.json)
                    {
                        exporter.SaveToJsonFile(string.Format("{0}/{1}.json", options.WorkOut, fileName), cd, options.ExportArray);
                    }

                    if (options.xml)
                    {
                        exporter.SaveToXmlFile(string.Format("{0}/{1}.xml", options.WorkOut, fileName), cd, options.ExportArray);
                    }
                }

                //-- 导出SQL文件
                if (options.sqlite || options.mysql)
                {
                    SQLExporter exporter = new SQLExporter(sheet, header);
                    if (string.IsNullOrEmpty(options.SQLPath))
                    {
                        options.SQLPath = string.Format("{0}/{1}", options.WorkOut, fileName);
                    }
                    exporter.SaveToFile(options, cd, fileName);
                }

                //-- 生成C#定义文件
                if (options.csharp)
                {
                    string excelName = Path.GetFileName(excelPath);
                    if (string.IsNullOrEmpty(options.SQLPath))
                    {
                        options.XmlPath = string.Format("{0}/{1}.cs", options.WorkOut, fileName);
                    }
                    CSDefineGenerator exporter = new CSDefineGenerator(sheet);
                    exporter.ClassComment = string.Format("// Generate From {0}", excelName);
                    exporter.SaveToFile(options.CSharpPath, cd);
                }

                if (options.csv)
                {
                    if (string.IsNullOrEmpty(options.CSVPath))
                    {
                        options.CSVPath = string.Format("{0}/{1}.csv", options.WorkOut, fileName);
                    }
                    CSVExporter.SaveToCSVFile(sheet, options.CSVPath);
                }
            }
        }