以前作過一個從Oracle導出到Excel的批處理,簡單點說就是經過Sqlplus將表數據Spool到一個文本文件中或簡化的Excel格式文檔中,而這裏我要作的事剛好相反,工具則是使用sqlldr, sqlldr的用法沒有研究太多,這裏主要用寫寫如何使用批處理來調用實現導入的自動化過程。sql
主要工做涉及到如下幾塊:數據庫
1. 將Excel轉換成csv格式的文件,這裏要藉助工具xls2csv.exe實現excel到csv的轉換;app
xls2csv的用法: xls2csv excelfile.xls gbkide
以上命令執行後,程序會將excel文件中的每一張sheet都生成一個csv文件,並以excel文件的名字_sheet名字.csv命名工具
2. 使用Sqlldr將數據導入Oracle,excel
Sqlldr的用法:sqlldr user/pass@tnsname control=example.ctl data=data.csvorm
以上命令執行後,sqlldr會將data.txt數據文件中數據按照control指定的規則插入到數據庫中,具體的插入規則和表名都在control文件中指定。ip
Control文件寫法:文檔
load data
infile 'data.csv'
append
into table t_tab_data
fields terminated by ';' optionally enclosed by '"'
(
filed1,
filed2,
filed3
)get以上語法文件中指定了從文件data.csv中導數據,導入到表t_tab_data中,
fields terminated by ';' optionally enclosed by '"' 表示數據文件中字段以;分隔,各字段中的"忽略掉,這些規則也能夠寫到各字段後,分別對各字段進行約束。
另外,Control文件中的infile ‘data.csv’也能夠寫成infile ‘*', 這樣data.csv中的數據能夠直接放到control文件的最後,sqlldr會在讀完control文件的規則部分後,再去讀取數據部分導入數據庫。
3. 將上面要作的工做寫成批處理
@Echo Off
Color 0A
Title Network Config Assistant By Eric
Pushd %CD%
Prompt $G
MODE CON COLS=100 LINES=30
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
Set OraUserPass=icd/icd@EricEcho 1. Start importing agent list to database...
Echo ========================================================Set /P xlsfilename=Agent List in Excel format(agents.xls as default):
IF /I "%xlsfilename%"=="" Set xlsfilename=agents.xlsEcho Generating csv file for agent list from excel...
start /min /wait xls2csv.exe %xlsfilename% gbkFOR /F %%i in ('DIR /B *.CSV /O:-D') DO (
SET FILENAME=%%i
GOTO :EXITFOR
):EXITFOR
Echo Generate csv file "%FILENAME%" successfully.Echo\
Set tmpAgentTab=t_test_agent
REM ==================Check table for Agent Configed in IEX=================
Echo Start checking table !tmpAgentTab!...
Echo select * from !tmpAgentTab! where 1^<^>1;>%temp%\tmpCheckTab.sql
Echo exit;>>%temp%\tmpCheckTab.sql
Sqlplus %OraUserPass% @"%temp%\tmpCheckTab.sql" | find /I "ERROR">NUL && IF errorlevel 0 (
Echo Table !tmpAgentTab! is not exist.
CALL :CreateAgentTable
Goto :AgentClear
)REM ==================Truncate table t_test_agent================
Echo Table !tmpAgentTab! is already exist.
Echo\Echo Truncating table !tmpAgentTab!...
Echo Truncate table !tmpAgentTab!;>%temp%\tmpTruncate.sql
Echo Exit>>%temp%\tmpTruncate.sql
Sqlplus %OraUserPass% @"%temp%\tmpTruncate.sql" 1>NUL
Echo Truncate table !tmpAgentTab! finished.:AgentClear
Echo\
REM ==================Import data from list to db=================
REM ----Generate control file----
Set tmpAgentCtrlFile="%CD%\agent.ctl"REM Echo load data>%tmpAgentCtrlFile%
REM Echo infile '%FILENAME%'>>!tmpAgentCtrlFile!
REM Echo append>>!tmpAgentCtrlFile!
REM Echo into table icd.t_test_agent>>!tmpAgentCtrlFile!
REM Echo fields terminated by ';' optionally enclosed by '^"'>>!tmpAgentCtrlFile!
REM Echo (>>!tmpAgentCtrlFile!
REM Echo agentid,>>!tmpAgentCtrlFile!
REM Echo agentname,>>!tmpAgentCtrlFile!
REM Echo agentskill>>!tmpAgentCtrlFile!
REM Echo ^)>>!tmpAgentCtrlFile!Echo Importing data to table !tmpAgentTab!...
sqlldr %OraUserPass% control=agent.ctl 1>NUL
Echo Importing data to table !tmpAgentTab! finished.Echo\
REM ============Create table t_test_agent================
:CreateAgentTable
IF /I "%0"==":CreateAgentTable" (
Echo Start creating table !tmpAgentTab!...
Echo Create table t_test_agent ^(>%temp%\tmpAgentTab.sql
Echo agentid number(5^),>>%temp%\tmpAgentTab.sql
Echo agentname varchar2(50^),>>%temp%\tmpAgentTab.sql
Echo agentskill varchar2(2000^)>>%temp%\tmpAgentTab.sql
Echo ^);>>%temp%\tmpAgentTab.sql
Echo Exit>>%temp%\tmpAgentTab.sqlSqlplus %OraUserPass% @"%temp%\tmpAgentTab.sql" 1>NUL
Echo Create table !tmpAgentTab! successfully.
Echo\
Goto :EOF
)Echo 2. Export data report of discrepancy...
Echo ========================================================
Echo Exporting report from database, please wait...
Start /WAIT ExportBySql.bat %OraUserPass% report.xls
Echo Export report from database finished.
Echo\
Echo\:EXIT Pause