Java 将excel中的内容导入数据库中
生活随笔
收集整理的這篇文章主要介紹了
Java 将excel中的内容导入数据库中
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ExcelReader類(lèi),用來(lái)從excel中讀取數(shù)據(jù)的,網(wǎng)上版本的修改版。
package dataDML;import java.io.IOException; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; 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 org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * 操作Excel表格的功能類(lèi) * @author: * @version */ public class ExcelReader { private POIFSFileSystem fs; private HSSFWorkbook wb; private HSSFSheet sheet; private HSSFRow row; /** * 讀取Excel表格表頭的內(nèi)容 * @param InputStream * @return String 表頭內(nèi)容的數(shù)組 * */ public String[] readExcelTitle(InputStream is) { try { fs = new POIFSFileSystem(is); wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); } sheet = wb.getSheetAt(0); row = sheet.getRow(0); //標(biāo)題總列數(shù) int colNum = row.getPhysicalNumberOfCells(); String[] title = new String[colNum]; for (int i=0; i<colNum; i++) { title[i] = getTitleValue(row.getCell((short) i)); } return title; } /** * 獲取單元格數(shù)據(jù)內(nèi)容為字符串類(lèi)型的數(shù)據(jù) * @param cell Excel單元格 * @return String 單元格數(shù)據(jù)內(nèi)容,若為字符串的要加單引號(hào) */ public String getStringCellValue(HSSFCell cell) { String strCell = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: strCell = "'" + cell.getStringCellValue() + "'"; break; case HSSFCell.CELL_TYPE_NUMERIC: strCell = String.valueOf(cell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: strCell = "''"; break; default: strCell = "''"; break; } if (strCell.equals("''") || strCell == null) { return ""; } if (cell == null) { return ""; } return strCell; } public String getTitleValue(HSSFCell cell) { String strCell = cell.getStringCellValue(); return strCell; } }下面是利用這個(gè)類(lèi)讀取數(shù)據(jù)并存儲(chǔ)到已有的數(shù)據(jù)表中,這里要注意的一點(diǎn)是,從excel中讀取的日期數(shù)據(jù),除非是以文本形式存儲(chǔ)的,取得的數(shù)據(jù)都不是日期形式,而是數(shù)字,是該日期距離1900年1月日的天數(shù),為了能夠正確存儲(chǔ)到數(shù)據(jù)庫(kù)中,我對(duì)該數(shù)據(jù)所對(duì)應(yīng)的字段名進(jìn)行了判斷,如果包含"date",也就是日期字段的數(shù)據(jù),就對(duì)其進(jìn)行轉(zhuǎn)化。具體見(jiàn)下面代碼:
package dataDML;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.text.DateFormat; import java.text.SimpleDateFormat;import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class DataInsert {public static String driver = "com.mysql.jdbc.Driver";public static String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";//之所以鏈接地址后面會(huì)添加參數(shù),是為了防止中文亂碼public static Connection conn;public static void main(String[] args) {try {Class.forName(driver);conn = DriverManager.getConnection(url,"test", "test123");insertData("tbname");//tbname,為要插入的數(shù)據(jù)表名} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void insertData(String tbName){try {//casilin:插入數(shù)據(jù),先從excel中讀取數(shù)據(jù)InputStream is = new FileInputStream("D://test.xls");ExcelReader excelReader = new ExcelReader();String[] colName = excelReader.readExcelTitle(is);//開(kāi)始建立插入的sql語(yǔ)句,每一次插入的開(kāi)頭都是不變的,都是字段名StringBuffer sqlBegin = new StringBuffer("insert into " + tbName + "(");//獲取字段名,并添加入sql語(yǔ)句中for (int i = 0; i < colName.length; i ++){sqlBegin.append(colName[i]);if (i != colName.length -1) {sqlBegin.append(",");}}sqlBegin.append(") values(");is.close();//下面讀取字段內(nèi)容POIFSFileSystem fs;HSSFWorkbook wb;HSSFSheet sheet;HSSFRow row;is = new FileInputStream("D://casilin//testFiles//test.xls");fs = new POIFSFileSystem(is); wb = new HSSFWorkbook(fs); sheet = wb.getSheetAt(0);//得到總行數(shù) int rowNum = sheet.getLastRowNum(); row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); //正文內(nèi)容應(yīng)該從第二行開(kāi)始,第一行為表頭的標(biāo)題 String sql = new String(sqlBegin);String temp;for (int i = 1; i <= rowNum; i++) { row = sheet.getRow(i); int j = 0; while (j<colNum) { temp = excelReader.getStringCellValue(row.getCell((short) j)).trim();//日期的特殊處理if (colName[j].indexOf("date") != -1){temp = temp.substring(0, temp.length()-2);//excel是以1990年為基數(shù)的,而java中的date是以1970年為基數(shù)的。所以要扣除差 25569天Date d = new Date((Long.valueOf(temp) - 25569) * 24 * 3600 * 1000);DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");temp = "'" + formater.format(d) + "'";}sql = sql + temp;if (j != colNum-1){sql = sql + ",";}j ++; } sql = sql + ")";System.out.println(sql.toString());PreparedStatement ps = conn.prepareStatement(sql.toString());ps.execute();ps.close();sql = "";sql = sqlBegin.toString();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) { e.printStackTrace(); } catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} }}這里會(huì)用到一個(gè)Jar包:poi.jar,自己可以到網(wǎng)上搜一下,我就不提供地址了。
轉(zhuǎn)載于:https://www.cnblogs.com/zhangdp/archive/2012/12/14/2817863.html
總結(jié)
以上是生活随笔為你收集整理的Java 将excel中的内容导入数据库中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: UltraEdit 使用操作
- 下一篇: volatile 和 sig_atomi