首页 > 数据库 >Excel导入数据批量生产建表sql语句

Excel导入数据批量生产建表sql语句

时间:2022-10-31 22:01:57浏览次数:40  
标签:建表 String getCell Excel Cell sql import null row

一、导入jar包

1、commons-collections4-4.1.jar

2、jxl-2.6.12.jar

3、poi-3.17.jar

4、poi-ooxml-3.17.jar

5、poi-ooxml-schemas-3.17.jar

6、spring-core-4.3.19.RELEASE

7、xmlbeans-2.6.0.jar

二、excel模板构建

Excel导入数据批量生产建表sql语句_java

三、(普通java工程)编写java代码


import jxl.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test {

public static void main(String[] args) {

try {
FileInputStream fileInputStream = new FileInputStream(
new File("D:\\OneDrive\\桌面\\test.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook(fileInputStream);

int numberOfSheets = wb.getNumberOfSheets();//获取sheel页的个数
XSSFSheet sheet = null;
for (int i = 0; i < numberOfSheets; i++) {
sheet = wb.getSheetAt(i); // sheet 索引
createSql( sheet);
}

wb.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void createSql(XSSFSheet sheet) {
String sheetName = sheet.getSheetName();

int lastRowNum = sheet.getLastRowNum();
Row rowLast = sheet.getRow(lastRowNum);
String tableName = new DataFormatter().formatCellValue(rowLast.getCell(0));

Row row = null;
Cell cell = null;//英文表
Cell cell2 = null;//中文表
Cell cell3 = null;//字段名
Cell cell4 =null; //字段中文名
Cell cell5 = null ;//数据类型
Cell cell6 =null;//数据最大长度
Cell cell7 =null ;//小数位长度
Cell cell8 =null ;//是否可为空
Cell cell9=null; //是否主键
String columName = null; //列名
String columType = null; //列类型
String columComent = null;//列注释
String columLength = null;//列长度

String sqlCont = "";
StringBuilder strsql= new StringBuilder();

for (int i = 1; i <= lastRowNum; i++) {
row = sheet.getRow(i);
cell = row.getCell(0); // 表英文名
if (cell != null && !StringUtils.isEmpty(cell.getStringCellValue())) {
cell2 = row.getCell(1); // 表中文名
cell3 = row.getCell(4); // 英文字段
cell4= row.getCell(5);//字段中文
cell5 =row.getCell(9);//数据类型
cell6=row.getCell(11);//字段长度
cell7=row.getCell(12);//小数位数
cell8=row.getCell(14);//是否可为空
cell9=row.getCell(15);//是否主键

columName = cell3.getStringCellValue();
columType = cell5.getStringCellValue();
columComent = cell4.getStringCellValue();
columLength=new DataFormatter().formatCellValue(cell6);

if (cell5!=null && cell5.toString().toLowerCase().equals("varchar2")){
sqlCont += columName + " " + columType.replace("varchar2","varchar") +"("+columLength+")"+ " DEFAULT NULL COMMENT '" + columComent
+ "',\n";
}
else if (cell5!=null && cell5.toString().toLowerCase().equals("char")){
sqlCont += columName + " " + columType.replace("varchar2","varchar") +"("+columLength+")"+ " DEFAULT NULL COMMENT '" + columComent
+ "',\n";
}else if (cell5!=null && cell5.toString().toLowerCase().equals("number") && cell7!=null && cell7.getNumericCellValue()!=0){
sqlCont += columName + " " + columType +"("+columLength+","+(int)cell7.getNumericCellValue()+")"+ " DEFAULT NULL COMMENT '" + columComent
+ "',\n";
}else{
sqlCont += columName + " " + columType +"("+columLength+")"+ " DEFAULT NULL COMMENT '" + columComent
+ "',\n";
}

}
}

String sqlStart = "CREATE TABLE test_" + cell+ "(\n";
String sqlEnd = "\n)\nCOMMENT '" + cell2 + "'\nclustered by ( xxxx ) into 13 buckets STORED AS ORC tblproperties(\"transactional\"=\"true\"\");";

String sql = sqlStart + sqlCont.substring(0, sqlCont.length() - 2) + sqlEnd;

System.out.println(sql);
}
}


标签:建表,String,getCell,Excel,Cell,sql,import,null,row
From: https://blog.51cto.com/liqiangbk/5811318

相关文章

  • MySQL快速入门
    MySQL快速入门1.初识MySQLJavaEE:企业级Java开发、Web前端(页面:展示——数据);后端(连接点:连接数据库JDBC,连接前端——控制视图跳转和给前端传递数据);数据库(存数据,Txt,Excel......
  • FlinkSql之TableAPI详解
    一、FlinkSql的概念核心概念Flink的TableAPI和SQL是流批统一的API。这意味着TableAPI&SQL在无论有限的批式输入还是无限的流式输入下,都具有相同的语义。......
  • CentOS9上面使用rpm方式安装SQLServer2022的简单总结
    CentOS9上面使用rpm方式安装SQLServer2022的简单总结下载需要的资料下载CentOS9Stream的安装介质https://mirrors.bfsu.edu.cn/centos-stream/9-stream/BaseOS/x86_64......
  • Linux源码安装MySQL
    在Linux中源码安装MySQL​​A、必备条件​​​​a>CMake​​​​b>Boost​​​​c>Mysql​​​​B、安装详情​​​​a>添加mysql用户​​​​b>配置mysql预编译参数​​​......
  • OS + CentOS 8.2 / MySQL 8.0.31
    smysql-8.0.31-1.el8.x86_64.rpm-bundle.tar解压https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.31-1.el8.x86_64.rpm-bundle.tarmysql-community-client-8.0.3......
  • Go开发 之 Go如何读取Mysql数据
    目录​​1、简介​​​​2、下载包,并创建包路径​​​​3、引用包​​​​4、举例说明​​​​5、效果图​​1、简介Go语言是个很方便的具有动态写法的静态语言,读取mysql是g......
  • C# sqlserver 分页查询
     C#sqlserver分页查询#region----商家列表查询请求类----///<summary>///商家列表查询请求类///</summary>publicclassSellerListRequest......
  • C# Sqlserver 分页查询-微商城
      C#Sqlserver分页查询-微商城#region----分页查询订单流水状态----///<summary>///分页查询订单流水状态///</summary>///......
  • sqlserver 游标
     sqlserver游标declare@BIDInt,@GiveToUserIDint,@NickNamenvarchar(100),@StartTimedatetime,@EndTimedatetime,@PageNumberint,@PageSizeintset@BI......
  • sqlserver执行语句返回刚刚插入的ID
     sqlserver执行语句返回刚刚插入的ID #region----新增一条活动表数据----///<summary>///新增一条活动表数据///</summary>/......