1、下载poi相关jar,maven的集成如下:(把${poi.version}替换成你要的版本)
<dependency><groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
2、根据poi相关api读取sheet、row、cell,获得excel的数据:
封装row的对象,即每一行数据为一个对象,每个cell为对象里的一个属性,
整个sheet的数据装进集合里;
3、处理数据,可以对数据进行验证或其他 *** 作;
4、写数据库 *** 作。
java通过poi把excel文件导入mysql数据库报错是因为excel中的数据类型要跟mysql中的数据类型和长度对应,否则类型转换异常是最常见的。所以插入到mysql数据库的时候需要做类型检查。
1、Excel中的测试数据:
2、数据库表结构:
CREATE TABLE `student_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`no` varchar(20) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`age` varchar(10) DEFAULT NULL,
`score` float DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
3、java源码部分ReadExcel.java:
/**
* 读取excel中的数据并插入db
*/
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
import java.util.ArrayList
import java.util.List
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
import com.b510.common.Common
import com.b510.excel.vo.Student
/**
* @author pieryon
* @created 2016-5-18
*/
public class ReadExcel {
public List<Student>readXls() throws IOException {
InputStream is = new FileInputStream(Common.EXCEL_PATH)
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is)
Student student = null
List<Student>list = new ArrayList<Student>()
// 循环工作表Sheet
for (int numSheet = 0numSheet <hssfWorkbook.getNumberOfSheets()numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet)
if (hssfSheet == null) {
continue
}
// 循环行Row
for (int rowNum = 1rowNum <= hssfSheet.getLastRowNum()rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum)
if (hssfRow != null) {
student = new Student()
HSSFCell no = hssfRow.getCell(0)
HSSFCell name = hssfRow.getCell(1)
HSSFCell age = hssfRow.getCell(2)
HSSFCell score = hssfRow.getCell(3)
student.setNo(getValue(no))
student.setName(getValue(name))
student.setAge(getValue(age))
student.setScore(Float.valueOf(getValue(score)))
list.add(student)
}
}
}
return list
}
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue())
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue())
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue())
}
}
}
2、SaveData2DB.java
/**
* 插入数据到db
*/
import java.io.IOException
import java.sql.SQLException
import java.util.List
import com.b510.common.Common
import com.b510.excel.util.DbUtil
import com.b510.excel.vo.Student
/**
* @author pieryon
* @created 2016-5-18
*/
public class SaveData2DB {
@SuppressWarnings({ "rawtypes" })
public void save() throws IOException, SQLException {
ReadExcel xlsMain = new ReadExcel()
Student student = null
List<Student>list = xlsMain.readXls()
for (int i = 0i <list.size()i++) {
student = list.get(i)
List l = DbUtil.selectOne(Common.SELECT_STUDENT_SQL + "'%" + student.getName() + "%'", student)
if (!l.contains(1)) {
DbUtil.insert(Common.INSERT_STUDENT_SQL, student)
} else {
System.out.println("The Record was Exist : No. = " + student.getNo() + " , Name = " + student.getName() + ", Age = " + student.getAge() + ", and has been throw away!")
}
}
}
}
保存结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)