当前位置:网站首页>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;
}
}
}
边栏推荐
- Swift 3pThread tool Promise Pipeline Master/Slave Serial Thread confinement Serial queue
- Overseas data centers need to be prepared for unpredictable disasters
- A few misunderstandings about programmers are very harmful!
- Cannot locate a 64-bit Oracle Client library:The specified module could not be found.
- 为 Chocolatey 设置代理
- Fabric. JS activation input box
- Station B doesn't want to be a "conscience aiyouteng"
- VBA determines whether a long string contains another short string
- Cap version 6.1 Release Notice
- 消除业务代码中if....else的五种方式
猜你喜欢

Numpy learning notes

【报表工具的第二次革命】基于SPL语言优化报表结构、提升报表运算性能

Solve the problem that idea is stuck in opening a project

Web3最全搞钱秘籍,看这一篇就够了

What should be done to improve the service level of the park and optimize the business environment

【BSP视频教程】BSP视频教程第17期:单片机bootloader专题,启动,跳转配置和调试下载的各种用法(2022-06-10)

Hidden Markov model and its training (1)

二十多年了,安全套市场还只有杜蕾斯、冈本、杰士邦

运筹说 第64期丨动态规划奠基人——理查德·贝尔曼

A few misunderstandings about programmers are very harmful!
随机推荐
[BSP video tutorial] BSP video tutorial issue 17: single chip microcomputer bootloader topic, startup, jump configuration and various usage of debugging and downloading (2022-06-10)
Pytorch Foundation (I) -- anaconda and pytorch installation
Swing visualization plug-in jformdesigner of idea
毕业季:致走向辽远未知的你
2022年焊工(初级)试题模拟考试平台操作
flex布局语法
adb不是内部或外部命令,也不是可运行的程序或批处理文件
MFC basic knowledge and course design ideas
Accuracy of alphafold and NMR determination of protein structure in solution
2022年上海市安全员C证操作证考试题库模拟考试平台操作
Full link tracking & performance monitoring tool skywalking practice
品牌难立,IPO难行,中国茶企困于“传统”?
目标客户匹配数据表格成功案例展示
2022G1工业锅炉司炉考题及在线模拟考试
fail-fast和fail-safe
Fabric. JS centered element
Fabric.js 精简输出的JSON
Create and install the visual studio offline installation package
Fabric.js 元素被选中时保持原有层级
Docker安装Redis镜像详细步骤(简单易懂,适合新手快速上手)