C# 根据EXCEL自动生成oracle建表语句

  1. Excel示例
    在这里插入图片描述
  2. 支持多表生成,多加几个sheet即可
  3. 生成效果
    在这里插入图片描述
    直接放码
using Microsoft.Office.Interop.Excel;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;


namespace SQLCreate
{
    internal class Program
    {
        public class TableModel
        {
            
            public string tableName;
            public string tableCommit;
            public List<string> chName;
            public List<string> enName;
            public List<string> type;
            public List<string> commit;

            public TableModel()
            {
                chName = new List<string>();
                enName = new List<string>();                
                type = new List<string>();
                commit = new List<string>();
            }
        }

        

        static void Main(string[] args)
        {
            string excelPath = "./test.xlsx";

            List<TableModel> tables = ReadFromExcel(excelPath);

            OutputTxt(tables);
        }

        public static void OutputTxt(List<TableModel> tables)
        {
            
            foreach (var item in tables)
            {
                string str = $"CREATE table {item.tableName}\n";
                str += "(\n";

                for (int i = 0; i < item.enName.Count; i++)
                {
                    str += $"{item.enName[i]} {item.type[i]},\n";
                }
                str += ");\n";

                str += $"comment on table {item.tableName} is '{item.tableCommit}';\n";

                for (int i = 0; i < item.enName.Count; i++)
                {
                    str += $"comment on table {item.tableName}.{item.enName[i]} is '{item.commit[i]}';\n";
                }

                OutputFile($"./result_{DateTime.Now.ToString("yyyymm")}.txt", str);
            }
        }

        public static void OutputFile(string strFilePath, string strContent)
        {
            StreamWriter swOut = new StreamWriter(strFilePath, true, Encoding.Default);
            swOut.WriteLine(strContent);
            swOut.Flush();
            swOut.Close();
        }



        public static List<TableModel> ReadFromExcel(string filePath)
        {
            #region xls
            //HSSFWorkbook = new HSSFWorkbook(fs);
            # endregion

            FileStream fs = File.OpenRead(filePath);

            XSSFWorkbook wk = new XSSFWorkbook(fs);

            fs.Close();

            List<TableModel> tables = new List<TableModel>();

            for (int i = 0; i < wk.NumberOfSheets; i++)
            {
                TableModel table = new TableModel();
                ISheet sheet = wk.GetSheetAt(i);

                string[] title = sheet.GetRow(0).GetCell(0).ToString().Split(' ');

                table.tableName = title[1];
                table.tableCommit = title[0];

                for (int j = 2; j <= sheet.LastRowNum; j++)
                {
                    IRow row = sheet.GetRow(j);  
                    if (row != null)
                    {
                        table.chName.Add(row.GetCell(0).ToString());
                        table.enName.Add(row.GetCell(1).ToString());
                        table.type.Add(row.GetCell(2).ToString());
                        table.commit.Add(row.GetCell(3).ToString());
                    }
                }
                tables.Add(table);
            }
            return tables;
        }
    }
}

当然可以,以下是一个简单的 C# 类示例,可以用于生成 Oracle 数据库建表语句: ```csharp using System; using System.Reflection; using System.Text; public class OracleTableBuilder { public string BuildTableCreationScript<T>(string tableName) { var sb = new StringBuilder(); sb.AppendFormat("CREATE TABLE {0} (", tableName); Type type = typeof(T); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { string columnName = property.Name; string columnType = GetOracleType(property.PropertyType); sb.AppendFormat("{0} {1},", columnName, columnType); } sb.Remove(sb.Length - 1, 1); // 移除最后一个逗号 sb.Append(")"); return sb.ToString(); } private string GetOracleType(Type type) { if (type == typeof(int)) { return "NUMBER"; } else if (type == typeof(decimal)) { return "NUMBER(18,2)"; } else if (type == typeof(DateTime)) { return "DATE"; } else if (type == typeof(string)) { return "VARCHAR2(50)"; } else { throw new NotImplementedException(string.Format("Oracle type mapping not implemented for type {0}.", type.Name)); } } } ``` 该类的 `BuildTableCreationScript` 方法接受一个类型参数并返回一个字符串,其中包含用于创建该类型对应的 Oracle 的 SQL 语句。你可以使用以下代码调用该方法: ```csharp OracleTableBuilder builder = new OracleTableBuilder(); string sql = builder.BuildTableCreationScript<MyClass>("MyTable"); ``` 其中,`MyClass` 是你想要创建的类型名称,`MyTable` 是你想要创建的名称。你可以根据需要调整 `GetOracleType` 方法中的类型映射。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou.cs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值