java操作Excel最常用的開源組件有poi與jxl。jxl是韓國人開發(fā)的,發(fā)行較早,但是更新的很慢,目前似乎還不支持excel2007。 poi是apache下的一個子項目,poi應該是處理ms的office系列文檔最好的組件了。poi3.7版本已經(jīng)開始支持excel2007了。所以這里我們選擇poi作為開發(fā)工具。
*
* java讀取excel文件
*
* 一個Excel文件的層次:Excel文件->工作表->行->單元格 對應到POI中,為:workbook->sheet->row->cell
*
*/
public class PoiExcel {
public static String outputFile = "E:/users.xls";
//public static String fileToBeRead = "E:/蔣和杰.xls";
static String createTableSql="";//創(chuàng)建數(shù)據(jù)庫的sql
static String colType="TEXT";//字段類型
static String key="id";//主鍵
static String charSet="utf8";//表格字符類型
static String ENGINE="InnoDB";//表格類型
static String tableName="tempExcelToMysql";//表名稱
static String colName="col";//默認字段名
static Connection conn = null;
/* 數(shù)據(jù)庫連接*/static void getConntion(){
try {
String driver_class = "com.mysql.jdbc.Driver";
String connection_url = "jdbc:mysql://localhost:3306/databasename?useUnicode=true&characterEncoding=gb2312";
String user_name = "username";
String db_password = "password";
Class.forName(driver_class);
conn = DriverManager.getConnection(connection_url, user_name,db_password);
}catch(Exception e){
e.printStackTrace();
}
}
public void CreateExcel() {
try {
// 創(chuàng)建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名為缺省值
// 如要新建一名為"效益指標"的工作表,其語句為:
// HSSFSheet sheet = workbook.createSheet("效益指標");
HSSFSheet sheet = workbook.createSheet();
// 在索引0的位置創(chuàng)建行(最頂端的行)
HSSFRow row = sheet.createRow((short) 0);
//在索引0的位置創(chuàng)建單元格(左上端)
HSSFCell cell = row.createCell((short) 0);
// 定義單元格為字符串類型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 在單元格中輸入一些內(nèi)容
cell.setCellValue("sweater");
// 新建一輸出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相應的Excel 工作簿存盤
workbook.write(fOut);
fOut.flush();
// 操作結(jié)束,關(guān)閉文件
fOut.close();
System.out.println("文件生成...");
} catch (Exception e) {
System.out.println("已運行 xlCreate() : " + e);
}
}
/**
*
* 讀取excel,遍歷各個小格獲取其中信息,并判斷其是否是手機號碼,并對正確的手機號碼進行顯示
* 注意: 1.sheet, 以0開始,以workbook.getNumberOfSheets()-1結(jié)束 2.row,
* 以0開始(getFirstRowNum),以getLastRowNum結(jié)束 3.cell,
* 以0開始(getFirstCellNum),以getLastCellNum結(jié)束, 結(jié)束的數(shù)目不知什么原因與顯示的長度不同,可能會偏長
*
*/
public void readExcel(String fileToBeRead,String addacct) {
//將被表示成1.3922433397E10的手機號轉(zhuǎn)化為13922433397,不一定是最好的轉(zhuǎn)換方法
DecimalFormat df = new DecimalFormat("#");
try {
// 創(chuàng)建對Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets);//獲得一個sheet
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
System.out.print("行"+rowNumOfSheet);//其它格式的數(shù)據(jù)
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
ArrayList recode=new ArrayList();
for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);
int cellType = aCell.getCellType();
switch (cellType) {
case 0://Numeric
String strCell = df.format(aCell .getNumericCellValue());
System.out.print(strCell);
recode.add(strCell);
break;
case 1://String
strCell = aCell.getStringCellValue();
System.out.print(strCell);
recode.add(strCell);
break;
default:
System.out.println("格式不對不讀");//其它格式的數(shù)據(jù)
}
}
} //end of 遍歷所有數(shù)據(jù)項結(jié)束
getConntion();
String sql="insert into tab_student_info (member_no,member_name,sex," +
"contact_mobile,province_no,city_no,county_no," +
"village_no,address,status,oper_date," +
"oper_code,oper_type,domain_no,att1,"+
"values("+0+",'"+recode.get(0)+"',"+0+",'"+ recode.get(1) +"',"+ 1 +"," + 1 +","+ 1 +","+ 1 +",'"+ recode.get(2) +"'," +
1+","+"now()"+",'"+addacct+"',"+2+","+1+"," +0+","+
")";
System.out.println(sql);
statement = conn.createStatement();
statement.executeUpdate(sql);
//conn.commit();
} //end of if (null != aSheet.getRow(rowNumOfSheet))
System.out.println("");
} //end of for 遍歷所有行結(jié)束
}
} //end of for 遍歷所有sheet結(jié)束
} catch (Exception e) {
System.out.println("ReadExcelError" + e);
}
}
}
————————————————
版權(quán)聲明:本文為CSDN博主「weixin_39531594」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_39531594/article/details/114460183