当前位置:网站首页>C# 根据EXCEL自动生成oracle建表语句
C# 根据EXCEL自动生成oracle建表语句
2022-06-10 16:33:00 【ou.cs】
Excel示例
支持多表生成,多加几个sheet即可- 生成效果

直接放码
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;
}
}
}
边栏推荐
- HTML+PHP+Mysql登录注册页面
- adb不是内部或外部命令,也不是可运行的程序或批处理文件
- 智慧景区视频监控 5G智慧灯杆网关组网综合杆
- Thread interview related questions
- 线程面试相关问题
- Complete AHK function commands
- Fabric. JS zoom canvas
- CAP 6.1 版本发布通告
- Swift 3pThread tool Promise Pipeline Master/Slave Serial Thread confinement Serial queue
- Knowledge based bert: a method to extract molecular features like a computational chemist
猜你喜欢

Cannot locate a 64-bit Oracle Client library:The specified module could not be found.

C#_ Serial port communication project

HTML+PHP+Mysql登录注册页面

防洪排涝监控 防洪排涝自动监测遥测终端

“禁塑令”下,中宝新材深挖可降解塑料,港交所买单吗?

Take you to a preliminary understanding of the basic mechanism of classes and objects

Eliminate if Five ways of else

Pytorch Foundation (I) -- anaconda and pytorch installation

2022年茶艺师(中级)操作证考试题库及模拟考试

Make good use of the industrial chain to attract investment, create industrial cluster effect and realize the coordinated development of industries
随机推荐
See how advanced technology changes human life
Eliminate if Five ways of else
Fabric. JS centered element
AlphaFold 和 NMR 测定溶液中蛋白质结构的准确性
虚拟机ping不通的几种原因及解决办法
简易的站点备份 Shell 脚本
What should be done to improve the service level of the park and optimize the business environment
[play with Huawei cloud] Kunpeng devkit migration practice
2022年G2电站锅炉司炉考试模拟100题及模拟考试
海外数据中心需要为不可预测的灾难做好准备
What is the highest compound interest insurance product? How much does it cost a year?
Flood control and drainage monitoring automatic monitoring and telemetering terminal for flood control and drainage
Nat. Rev. Drug Discov. | Application of AI in small molecule drug discovery: an upcoming wave?
B站不想成为“良心版爱优腾”
Detailed steps for installing redis image in docker (easy to understand, suitable for novices to get started quickly)
毕业季:致走向辽远未知的你
Importerror: libgl.so.1: cannot open shared object file: no such file or directory
Who is using my server? what are you doing? when?
“禁塑令”下,中宝新材深挖可降解塑料,港交所买单吗?
Complete AHK function commands