1. Add the POI jar package to the project's Lib directory ­
2. Excel file directory:d://excel.xls­
3. Database field: Num1 num2 num3 num4 num5 num6­
4. Database name:blog­
5. Table name:test­
6. Writing class: String method to connect MySQL, insert method, entity class ­­,
import java.io.fileinputstream;­import java.io.filenotfoundexception;­import java.io.ioexception;­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.hssfrow;­import org.apache.poi.hssf.usermodel.hssfsheet ; ­import org.apache.poi.hssf.usermodel.hssfworkbook;­public class testexcel {­ //output information for the record class ­ Static log log = logfactory.getlog (Testexcel.class); ­ //gets the path to the Excel document ­ public static string filePath = "D://excel.xls"; ­ public static void main (String[] args) {­ try {­ // Create a reference to an Excel workbook file ­ hssfworkbook wookbook = new hssfworkbook (New FileInputStream ( FilePath));­ // in an Excel document, the default index of the first sheet is 0 // its statement is:hssfsheet sheet = Workbook.getsheetat (0);­ hssfsheet sheet = wookbook.getsheet ("Sheet1");­ //gets the number of lines in the Excel file ­ int rows = sheet.getphysicalnumberofrows (); //traversing rows ­ for (int i = 0; i < rows; i++) {­ // read upper left cell ­ hssfrow row = sheet.getrow (i);­ // line is not empty & shy; if (row != null) {­ //get all the columns in the Excel file ­ int cells = row.getphysicalnumberofcells (); ­ string value = ""; ­ //traversing Columns ­ for (int j = 0; j < cells; j++) {­ //gets the value of the column ­ hssfcell cell = row.getcell (j); ­ if (cell != null) {­ switch (Cell.getcelltype ()) {& shy; case hssfcell.cell_type_ formula:­ break;­ case HSSFCell.CELL_TYPE_NUMERIC:­ value += Cell.getnumericcellvalue () + ","; ­ break; ­ case HSSFCell.CELL_TYPE_STRING:­ value += Cell.getstringcellvalue () + ","; ­ break;­ default:­ value += "0";­ break;­ }­ } }& shy; // inserting data into the MySQL database ­ String[] val = Value.split (",");­ TestEntity entity = new Testentity ();­ &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENTITY.SETNUM1 (val[0]);­ entity.setnum2 (val[1]);­ &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENTITY.SETNUM3 (val[2]); ­ &NBSP;&NBSP;ENTITY.SETNUM4 (val[3]);­ &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENTITY.SETNUM5 (Val[4]);­ &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENTITY.SETNUM6 (val[5]);­ Testmethod method = new testmethod ();­ method. ADD (Entity);­ }­ }­ } catch (filenotfoundexception e) {­ e.printstacktrace ();­ } catch (ioexception e) {­ e.printstacktrace ();­ }­ }­} ­
Import Excel table content into MySQL database using Java technology