李博


摘? 要:Excel在日常辦公中占據著極其重要的地位,微軟也在不斷的增加新功能,使它的能力越來越強,眾多集成的函數方法讓使用者能輕松操作某個電子表格。如用戶寫一個VLOOKUP公式就能簡單的根據ID精確匹配出相關內容,但這種匹配準則相對簡單[1],本文基于Excel表的匹配問題,提出利用Java編程完成復雜條件匹配,和另一種借助MySQL數據庫查詢的匹配方案,比較兩者之間的執行情況,加以分析,得出借助MySQL數據庫查詢匹配方案優于前者。
關鍵詞:Excel;Java;MySQL;數據匹配
中圖分類號:TP39;TP31 文獻標識碼:A 文章編號:2096-4706(2019)01-0013-03
Excel Data Matching Research
LI Bo
(Southwest Minzu University,Chengdu? 610041,China)
Abstract:Excel occupies an important position in daily office,Microsoft is constantly adding new features,making it more and more powerful,and many integrated function methods make it easy for users to operate a spreadsheet. For example,if the user writes a VLOOKUP formula,the relevant content can be accurately matched according to the ID,but the matching criterion is relatively simple[1]. Based on the matching problem of Excel tables,this paper puts forward a matching scheme using Java programming to complete complex condition matching and another matching scheme using MySQL database query,compares the implementation of the two schemes,and analyses them. It is concluded that the matching scheme using MySQL database query is better than the former.
Keywords:Excel;Java;MySQL;data matching
1? Java解析Excel表
Java解析Excel表常用的框架有兩個,一種是借助日本開源的jxl.jar,一種是借助Apache POI.jar也是開源的,兩者的差距不大。由于JXL現已停更,本文使用Apache POI 3.17版本解析Excel表。針對Excel有兩個版本,可使用HSSFWorkbook讀取2003版本的Excel表,XSSFWorkbook讀取2007版本及以上的Excel表[2]。POI插件是Jakarta公司提供的一個完全用Java語言實現的免費插件,該插件可以把Excel表格指定Sheet表的指定行和列的Cell格的內容讀出,而且可以把數據按照用戶指定的格式寫入到Excel文檔[3]。
部分代碼如下:
public class Main {
public static Statement statement=null;
public static void main(String[] args) throws Exception{
statement = getConnectwithMysql.mainconn (“database”);
//準備匹配文檔TEST.XLSX
File file = new File(“J:\\TEST.XLSX”);
Workbook wb = null;
try {
//讀取file到緩存區,獲取工作簿
wb = new XSSFWorkbook(new FileInput Stream(file));
} catch (IOException e) {
e.printStackTrace();
}
//得到序號為0的工作表
Sheet sheet = wb.getSheetAt(0);
//獲取工作表數據行總行數
int n=sheet.getLastRowNum();
for (int i=1;i<=n;i++) {
//打印提示信息,開始匹配EXCEL表的第i+1行數據
System.out.println(“start to scan “+(i+1)+” row”);
Row row = sheet.getRow(i);
//獲取單行數據匹配關鍵信息
String hetongcode=
getCellContent(row.getCell(1));
….....
//在數據庫中匹配,匹配結果返回在結果集rs中
ResultSet rs = select (daytime,name,money);
//記錄匹配信息
while(rs.next()) {
hetongcode=hetongcode+”-(“+rs.getString (“@序號”)+”)”;
}
row.getCell(1).setCellValue(hetongcode);
}
//跟新工作薄,保存匹配信息
try (FileOutputStream fileOut = new FileOutput Stream(file)) {
wb.write(fileOut);
fileOut.close();
}
//封裝類select
public static ResultSet select(String daytime, String name, String money) throws SQLException {
//封裝查詢語句SQL
String SQL=”select from WHERE (`日期`-“+daytime+”)>=0 AND (`日期`-“+daytime+”) <=5 AND ((`支出`-“+money+”)=0 or (`支出`-“+ money+”)>1500) AND `戶名` LIKE \””+name +”\””;
//執行查詢,返回查詢結果集rs
ResultSetrs=
statement.executeQuery(SQL);
return rs;
}
//封裝類getCellContent讀取cell內容,返回String方便統一處理
public static String getCellContent(Cell cell){
String content=null;
CellType type = cell.getCellTypeEnum();
If(type.equals(CellType.NUMERIC)){
Content=cell.getNumericCellValue()+””;
}
….....
Return content;
}
}
2? 兩種方案思路……p>