linux指令碼實現excel檔案內容讀取到資料庫

2022-07-18 01:06:12 字數 1329 閱讀 2151

假設我現在有乙個表,需要插入excel的資料

建立表的**如下

create table student(

sid varchar(10),

sname varchar(20),

sgender char(1),

sage int

)

現在要從excel匯入資料

首先我們可以把資料轉化成csv或者txt,這裡我用csv做例子

檢視這個檔案

可以看到csv是用逗號分隔的

以下是shell指令碼

# !/bin/bash

# read file an create insert statements for mysql

out='stu.sql'

ifs=','

while read sid sname sgender sage

do cat >> $out << eof

insert into student (sid,sname,sgender,sage)values

('$sid','$sname','$sgender','$sage');

eofdone

其中ifs叫做內部字段分隔符(internal field separator),預設是空格換行和tab鍵

'>>' 是追加重定向

需要加可執行的許可權

chmod u+x stu.sh
執行**

./stu.sh < stu.csv 

cat stu.sql

可以發現csv檔案已經寫入到了stu.sql檔案之中。

stu.sql檔案內容如下

insert into student (sid,sname,sgender,sage)values

('1001','yangmi','f','20

'); insert into student (sid,sname,sgender,sage)values

('1002','pengyueyan','m','40

'); insert into student (sid,sname,sgender,sage)values

('1003','huge','m','36

'); insert into student (sid,sname,sgender,sage)values

('1004','luhan','m','37

');

然後執行sql檔案就插入到了表中。

datagridview 匯出Excel檔案

以datagridview 匯出excel檔案 public void exportexcel datagridview dgv microsoft.office.interop.excel.workbook workbook workbooks.add microsoft.office.inter...

HSSFWorkbook生成Excel檔案並匯出

利用hssfworkbook生成excel檔案的 網上有很多例子,此處是我自己專案中用到,稍加修改的demo,由於生成excel檔案中記錄數行數超過6萬多時會報錯,本例中對資料量多的情況做了保護,超過5萬條,再生成新的sheet,具體 如下 import org.apache.poi.hssf.us...

python pandas讀取excel檔案

python pandas讀取excel檔案 data pd.read excel c users 86188 python dummy.xlsx 會報錯 syntaxerror unicode error unicodeescape codec can t decode bytes in posi...