1 Star 0 Fork 0

阳光灿烂的日子/golangMsOffice

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 3.28 KB
一键复制 编辑 原始数据 按行查看 历史
阳光灿烂的日子 提交于 2015-04-14 22:33 . 2015-04-14
package main
import (
"bytes"
"fmt"
"github.com/tealeg/xlsx"
"io/ioutil"
_ "os"
_ "strconv"
"strings"
)
func main() {
excelFileName := "SVP-表定义(ORACLE)_FACT_采购.xlsx"
xlFile, err := xlsx.OpenFile(excelFileName)
if err != nil {
panic(err)
}
for _, sheet := range xlFile.Sheets {
//DataTable := make(map[int]map[int]map[string]string)
DataTable := make(map[int]map[string]string)
header := make(map[int]string)
index := 0
var pk []string
for rIndex, row := range sheet.Rows {
cellTip := make(map[string]string)
for i, cell := range row.Cells {
if rIndex == 1 {
header[i] = strings.Trim(cell.Value, " ")
continue
}
if len(header) == 0 {
continue
} else {
cellTip[header[i]] = strings.Trim(cell.Value, " ")
}
//fmt.Printf("%s\n", cell.String())
//fmt.Printf("%s\n", cell.Value)
}
//fmt.Println(cellTip)
if len(cellTip) != 0 {
DataTable[index] = cellTip
index++
}
}
//fmt.Println(DataTable[0])
buf := bytes.NewBufferString("--创建表脚本")
buf.WriteString("\r\n")
buf.WriteString("create table ")
buf.WriteString(sheet.Name)
buf.WriteString("\r\n")
buf.WriteString("(")
buf.WriteString("\r\n")
rowCount := 0
for _, Values := range DataTable {
buf.WriteString(" ")
buf.WriteString(Values["字段 物理名"])
buf.WriteString(" ")
buf.WriteString(Values["数据类型"])
if strings.ToUpper(Values["数据类型"]) == "VARCHAR2" {
buf.WriteString("(")
buf.WriteString(Values["最大长度"])
buf.WriteString(")")
}
buf.WriteString(" ")
if strings.Trim(Values["缺省值"], " ") != "" {
buf.WriteString("default")
buf.WriteString(" ")
if strings.ToUpper(Values["数据类型"]) == "VARCHAR2" {
buf.WriteString("'")
buf.WriteString(Values["缺省值"])
buf.WriteString("'")
} else {
buf.WriteString(Values["缺省值"])
}
}
buf.WriteString(" ")
if strings.ToUpper(Values[`IS
NULL`]) == "N" {
buf.WriteString("NOT NULL ")
}
if rowCount != len(DataTable)-1 {
buf.WriteString(",")
}
buf.WriteString("\r\n")
if strings.ToUpper(Values[`主键
(PK)`]) == "○" {
pk = append(pk, Values["字段 物理名"])
}
rowCount++
}
buf.WriteString(")\r\n")
buf.WriteString(`
tablespace ELINK11
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);`)
if len(pk) > 0 {
buf.WriteString("\r\n")
buf.WriteString("-- Create/Recreate primary, unique and foreign key constraints ")
buf.WriteString("\r\n")
buf.WriteString("alter table ")
buf.WriteString(sheet.Name)
buf.WriteString("\r\n")
buf.WriteString("add constraint PK_")
buf.WriteString(sheet.Name)
buf.WriteString(" primary key (")
for j, v := range pk {
buf.WriteString(v)
if j != len(pk)-1 {
buf.WriteString(",")
}
}
buf.WriteString(")")
buf.WriteString("\r\n")
buf.WriteString(` using index
tablespace ELINK11
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);`)
}
//fmt.Println(buf.String())
err := ioutil.WriteFile("sql/"+sheet.Name+".txt", []byte(buf.String()), 0666)
if err != nil {
fmt.Println(err)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yinqunjun/golangMsOffice.git
[email protected]:yinqunjun/golangMsOffice.git
yinqunjun
golangMsOffice
golangMsOffice
master

搜索帮助