码迷,mamicode.com
首页 > 数据库 > 详细

使用java技术将Excel表格内容导入mysql数据库

时间:2015-04-10 11:46:19      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

1、添加POI jar包到项目的lib目录下­
2、Excel文件目录:d://excel.xls­
3、数据库字段为:num1 num2 num3 num4 num5 num6­
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;

 

使用java技术将Excel表格内容导入mysql数据库

标签:

原文地址:http://my.oschina.net/u/587105/blog/398402

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!