网站备案由别人代,维护网站,做平面的网站,手机端网站建设的注意事项java操作Excel最常用的开源组件有poi与jxl。jxl是韩国人开发的#xff0c;发行较早#xff0c;但是更新的很慢#xff0c;目前似乎还不支持excel2007。 poi是apache下的一个子项目#xff0c;poi应该是处理ms的office系列文档最好的组件了。poi3.7版本已经开始支持excel200…java操作Excel最常用的开源组件有poi与jxl。jxl是韩国人开发的发行较早但是更新的很慢目前似乎还不支持excel2007。 poi是apache下的一个子项目poi应该是处理ms的office系列文档最好的组件了。poi3.7版本已经开始支持excel2007了。所以这里我们选择poi作为开发工具。** 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;//创建数据库的sqlstatic String colTypeTEXT;//字段类型static String keyid;//主键static String charSetutf8;//表格字符类型static String ENGINEInnoDB;//表格类型static String tableNametempExcelToMysql;//表名称static String colNamecol;//默认字段名static Connection conn null;/* 数据库连接*/static void getConntion(){try {String driver_class com.mysql.jdbc.Driver;String connection_url jdbc:mysql://localhost:3306/databasename?useUnicodetruecharacterEncodinggb2312;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 {// 创建新的Excel 工作簿HSSFWorkbook workbook new HSSFWorkbook();// 在Excel工作簿中建一工作表其名为缺省值// 如要新建一名为效益指标的工作表其语句为// HSSFSheet sheet workbook.createSheet(效益指标);HSSFSheet sheet workbook.createSheet();// 在索引0的位置创建行(最顶端的行)HSSFRow row sheet.createRow((short) 0);//在索引0的位置创建单元格(左上端)HSSFCell cell row.createCell((short) 0);// 定义单元格为字符串类型cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 在单元格中输入一些内容cell.setCellValue(sweater);// 新建一输出文件流FileOutputStream fOut new FileOutputStream(outputFile);// 把相应的Excel 工作簿存盘workbook.write(fOut);fOut.flush();// 操作结束关闭文件fOut.close();System.out.println(文件生成...);} catch (Exception e) {System.out.println(已运行 xlCreate() : e);}}/**** 读取excel遍历各个小格获取其中信息并判断其是否是手机号码并对正确的手机号码进行显示* 注意 1.sheet 以0开始以workbook.getNumberOfSheets()-1结束 2.row* 以0开始(getFirstRowNum)以getLastRowNum结束 3.cell* 以0开始(getFirstCellNum)以getLastCellNum结束, 结束的数目不知什么原因与显示的长度不同可能会偏长**/public void readExcel(String fileToBeRead,String addacct) {//将被表示成1.3922433397E10的手机号转化为13922433397,不一定是最好的转换方法DecimalFormat df new DecimalFormat(#);try {// 创建对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);//获得一个sheetfor (int rowNumOfSheet 0; rowNumOfSheet aSheet.getLastRowNum(); rowNumOfSheet) {System.out.print(行rowNumOfSheet);//其它格式的数据if (null ! aSheet.getRow(rowNumOfSheet)) {HSSFRow aRow aSheet.getRow(rowNumOfSheet);ArrayList recodenew 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://NumericString strCell df.format(aCell .getNumericCellValue());System.out.print(strCell);recode.add(strCell);break;case 1://StringstrCell aCell.getStringCellValue();System.out.print(strCell);recode.add(strCell);break;default:System.out.println(格式不对不读);//其它格式的数据}}} //end of 遍历所有数据项结束getConntion();String sqlinsert 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 遍历所有行结束}} //end of for 遍历所有sheet结束} catch (Exception e) {System.out.println(ReadExcelError e);}}}