摘要:在Web應(yīng)用系統(tǒng)中,用戶(hù)們經(jīng)常要求將數(shù)據(jù)庫(kù)數(shù)據(jù)進(jìn)行導(dǎo)入導(dǎo)出處理。Microsoft Excel是用戶(hù)非常熟悉的辦公軟件,因此,Excel作為數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)入導(dǎo)出的中間介質(zhì)成為最佳選擇。為了達(dá)到此目的,采用Java Excel API操作Excel文件的方法,實(shí)現(xiàn)了僅通過(guò)在客戶(hù)端的操作,將數(shù)據(jù)庫(kù)中的數(shù)據(jù)導(dǎo)出到Excel表格中,并將Excel表格中的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)的功能。滿(mǎn)足了用戶(hù)進(jìn)行數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)入導(dǎo)出的要求。
關(guān)鍵詞:Java Excel API; 數(shù)據(jù)庫(kù); 數(shù)據(jù)導(dǎo)入; 數(shù)據(jù)導(dǎo)出
中圖分類(lèi)號(hào):TN91134文獻(xiàn)標(biāo)識(shí)碼:A文章編號(hào):1004373X(2012)04004503
Implementation of data import and export from database based on Java Excel API
ZHANG Minghui, WANG Qingxin, WANG Zhengyu
(College of Information Engineering and Automation, Kunming University of Technology, Kunming 650051, China)
Abstract: In Web application system, clients always call for the data import and export from a database. Microsoft Excel is an office software which is very familiar to many clients, so Excel as a intermedia becomes the first option for data import and export from database. In order to achieve the goal, this method is adopted to operate Excel by Java Excel API. Only through the process on the client side, can the data be exported from database into Excel and data be imported from Excel into database. It meet the requirements of clients for data import and export.
Keywords: Java Excel API; database; data import; data export
收稿日期:201109260引言
Microsoft Excel是Microsoft Office系列辦公軟件重要成員之一,Excel可以用來(lái)存儲(chǔ),管理,計(jì)算,分析各種數(shù)據(jù)文件,擁有著極大的用戶(hù)群體。在Web應(yīng)用系統(tǒng)中,用戶(hù)常會(huì)要求將在Excel中保存的各種格式的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中,或?qū)?shù)據(jù)庫(kù)中的數(shù)據(jù)導(dǎo)出到Excel表格中,這就需要在客戶(hù)端為客戶(hù)提供一個(gè)工具,該工具能夠?qū)崿F(xiàn)數(shù)據(jù)庫(kù)中的數(shù)據(jù)與Excel中的數(shù)據(jù)相互傳導(dǎo)。
在Java世界里,有兩套成熟的開(kāi)源工具支持對(duì)Excel文件的操作:一個(gè)是Apache POI;另一個(gè)則是本文所描述方法要使用的Java Excel API。Apache POI是Apache軟件基金會(huì)的開(kāi)放源碼函式庫(kù),POI提供API給Java程式對(duì)Microsoft Office格式檔案讀/寫(xiě)功能。該項(xiàng)目分為幾個(gè)組件,其中包括一個(gè)HSSF的組件,它是一個(gè)非常正規(guī)和嚴(yán)謹(jǐn)?shù)腁PI。利用HSSF,你可以用純Java代碼來(lái)讀取、寫(xiě)入、修改Excel文件[12]。
另一個(gè)針對(duì)Excel操作的開(kāi)放源碼項(xiàng)目是Java Excel API(JXL),它是一套純粹使用Java開(kāi)發(fā)的Excel表格操作組件,使用它,即使在非Windows操作系統(tǒng)下,程序員也可以通過(guò)純 Java 應(yīng)用來(lái)創(chuàng)建Excel文件,并能夠讀取,寫(xiě)入,更新Excel中的內(nèi)容,因此,基于JXL可以實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)中數(shù)據(jù)導(dǎo)入導(dǎo)出的操作[3]。與POI相比,JXL擁有更小的內(nèi)存占用率和對(duì)中文更好的支持,因此,本文所介紹的數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)入導(dǎo)出方法基于JXL。
1數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)出Excel
為了方便用戶(hù)操作,除了將數(shù)據(jù)庫(kù)中的數(shù)據(jù)導(dǎo)出至Excel,還需將數(shù)據(jù)庫(kù)表名和字段名一同導(dǎo)出,因此,數(shù)據(jù)導(dǎo)出操作流程如圖1所示。
3結(jié)語(yǔ)
通過(guò)以上方法,使用JXL組件可以簡(jiǎn)單方便地將數(shù)據(jù)庫(kù)中數(shù)據(jù)導(dǎo)出到Excel表,并能將Excel中的數(shù)據(jù)導(dǎo)入至數(shù)據(jù)庫(kù)中。實(shí)現(xiàn)了對(duì)Excel的讀取和寫(xiě)入操作,并支持對(duì)單元格及其內(nèi)容的格式化操作。因此,Java Excel API在Web應(yīng)用系統(tǒng)中得到了越來(lái)越廣泛的應(yīng)用。
參考文獻(xiàn)
[1]黃紀(jì)盛.POI與JXL在研究生選課系統(tǒng)的研究與應(yīng)用[J].電腦知識(shí)與技術(shù),2010,22(2):63426344.
[2]邱小彬,周南,虞萍.基于Java的批量數(shù)據(jù)導(dǎo)入導(dǎo)出探討[J].農(nóng)業(yè)網(wǎng)絡(luò)信息,2008(10):148150.
[3]張書(shū)鋒.基于Java Excel API的excel文件的操縱技術(shù)及其應(yīng)用[J].電腦知識(shí)與技術(shù),2010,16(6):45654566.
[4]李興華.Java開(kāi)發(fā)實(shí)戰(zhàn)經(jīng)典[M].北京:清華大學(xué)出版社,2009.
[5]\\[美\\]HORSTMANN C S,CORNELL G.Java 2核心技術(shù)[M].北京:機(jī)械工業(yè)出版社,2006.
[6]劉娟,楊麗君.基于Java的Excel文件操作[J].計(jì)算機(jī)教育,2007,12(2):17301731.
[7]雷之宇,朱訓(xùn)雨,張麟.Java實(shí)用組件集[M].北京:電子工業(yè)出版社,2008.
[8]李林珊,鄭麗婭.基于JXL的數(shù)據(jù)導(dǎo)入導(dǎo)出處理[J].軟件開(kāi)發(fā)與應(yīng)用,2008,27(9):9596.
[9]李興華,王月清.Java Web開(kāi)發(fā)實(shí)戰(zhàn)經(jīng)典[M].北京:清華大學(xué)出版社,2010.
[10]李聰玲.液氧/煤油發(fā)動(dòng)機(jī)試驗(yàn)控制系統(tǒng)軟件架構(gòu)與設(shè)計(jì)\\[J\\].火箭推進(jìn),2009,35(2):4653.