JAVA把excel表格的資料匯入到資料庫

來源:互聯網
上載者:User

標籤:excel   匯入資料庫   

package com.insertdatebase;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDateUtil;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import com.dao.DKInfoDao;import com.dao.DaiKuanInfoIm;import com.entity.DaiKuanInfo;/* * 把excel表格的資料插入資料冷庫 * @auther guo-xqi */public class TestExcel {// 記錄類的輸出資訊static Log log = LogFactory.getLog(TestExcel.class);// 擷取Excel文檔的路徑­public static String filePath = "D://daizhanghexiao.xls";public static void main(String[] args) {try {// 建立對Excel活頁簿檔案的引用HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(filePath));// 在Excel文檔中,第一張工作表的預設索引是0// 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);HSSFSheet sheet = wookbook.getSheet("Sheet1");// 擷取到Excel檔案中的所有行數int rows = sheet.getPhysicalNumberOfRows();// 遍曆行for (int i = 0; i < rows; i++) {//讀取左上端儲存格HSSFRow row = sheet.getRow(i);// 行不為空白if (row != null) {// 擷取到Excel檔案中的所有的列int cells = row.getPhysicalNumberOfCells();String value = "";// 遍曆列for (int j = 0; j < cells; j++) {// 擷取到列的值?HSSFCell cell = row.getCell((short) j);if (cell != null) {switch (cell.getCellType()) {case HSSFCell.CELL_TYPE_FORMULA:break;case HSSFCell.CELL_TYPE_NUMERIC:if (HSSFDateUtil.isCellDateFormatted(cell)) {value += cell.getDateCellValue() + ",";}else {value += cell.getNumericCellValue() + ",";}break;case HSSFCell.CELL_TYPE_STRING:value += cell.getStringCellValue() + ",";break;default:value += "0";break;}}}// 將資料插入到mysql資料庫中//System.out.println(value);//String string="Tue Feb 04 00:00:00 CST 2014";// SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US);// try {//Date d=sdf.parse(string);//sdf=new SimpleDateFormat("yyyy-MM-dd");////System.out.println(sdf.format(d));//} catch (ParseException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//把得到的字串拆分String[] val = value.split(",");//給建立的對象設定值DaiKuanInfo dkinfo = new DaiKuanInfo();dkinfo.setDKPerson(val[0]);SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US);Date dkDate;try {dkDate = sdf.parse(val[1]);sdf=new SimpleDateFormat("yyyy-MM-dd");String dkdateString=sdf.format(dkDate);dkinfo.setDKDate(java.sql.Date.valueOf(dkdateString));dkinfo.setDkje(Double.parseDouble(val[2]));dkinfo.setDBPerson(val[3]);SimpleDateFormat simdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US);Date hkdate=simdf.parse(val[4]);simdf=new SimpleDateFormat("yyyy-MM-dd");dkinfo.setHKDate(java.sql.Date.valueOf(simdf.format(hkdate)));} catch (ParseException e1) {e1.printStackTrace();}dkinfo.setYhje(Double.parseDouble(val[5]));dkinfo.setQkje(Double.parseDouble(val[6]));//插入資料庫DKInfoDao dkdao = new DaiKuanInfoIm();int boo = dkdao.addDaiKuanInfo(dkinfo);if (boo == 0) {continue;} else {break;}}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

JAVA把excel表格的資料匯入到資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.