Javaテクノロジーを使用して、Excelテーブルの内容をmysqlデータベースにインポート

4327 ワード

1、POI jarパッケージをプロジェクトのlibディレクトリの下­に追加する.2、Excelファイルディレクトリ:d://excel.xls­3、データベースフィールドはnum 1 num 2 num 3 num 4 num 5 num 6­4、データベース名:blog­5、表名:test­6、編纂クラス:mysqlを接続する文字列方法、挿入方法、実体クラス­­、
 
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 {­
      //        ­
      static Log log = LogFactory.getLog(TestExcel.class); ­
      //  Excel     ­
      public static String filePath = "D://excel.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++) {&shy;
                        //         &shy;
                        HSSFRow row = sheet.getRow(i);&shy;
                        //     &shy;
                        if (row != null) {&shy;
                              //   Excel        &shy;
                              int cells = row.getPhysicalNumberOfCells();&shy;
                              String value = "";     &shy;
                              //   &shy;
                              for (int j = 0; j < cells; j++) {&shy;
                                    //      &shy;
                                    HSSFCell cell = row.getCell(j);&shy;
                                    if (cell != null) {&shy;
                                          switch (cell.getCellType()) {&shy;
                                                case HSSFCell.CELL_TYPE_FORMULA:&shy;
                                                break;&shy;
                                                case HSSFCell.CELL_TYPE_NUMERIC:&shy;
                                                      value += cell.getNumericCellValue() + ",";        &shy;
                                                break;  &shy;
                                                case HSSFCell.CELL_TYPE_STRING:&shy;
                                                      value += cell.getStringCellValue() + ",";&shy;
                                                break;&shy;
                                                default:&shy;
                                                      value += "0";&shy;
                                                break;&shy;
                                    }&shy;
                              }      
                        }&shy;
                        //       mysql    &shy;
                        String[] val = value.split(",");&shy;
                        TestEntity entity = new TestEntity();&shy;
                        entity.setNum1(val[0]);&shy;
                        entity.setNum2(val[1]);&shy;
                        entity.setNum3(val[2]);&shy;
                        entity.setNum4(val[3]);&shy;
                        entity.setNum5(val[4]);&shy;
                        entity.setNum6(val[5]);&shy;
                        TestMethod method = new TestMethod();&shy;
                        method.Add(entity);&shy;
                  }&shy;
             }&shy;
      } catch (FileNotFoundException e) {&shy;
            e.printStackTrace();&shy;
      } catch (IOException e) {&shy;
            e.printStackTrace();&shy;
      }&shy;
   }&shy;
}&shy;