ssm利用poi将excel数据导入数据库
来源:互联网 发布:手机流水账软件 编辑:程序博客网 时间:2024/06/15 18:41
POI导入数据库
1.前台页面
<form action="<%=request.getContextPath()%>/doFile.action" method="post" enctype="multipart/form-data">
请选择文件:<input type="file" name="excelFile"><br/>
<input type="submit" value="提交">
</form>
excelFile封装 用 MultipartFile封装一个实体类
2.spring中进行上传配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize"><value>100000</value></property>
<property name="defaultEncoding"><value>UTF-8</value></property>
</bean>
3.后台代码
4.ExcelUtils为工具类(将Excel中的数据 读取成为List集合)
public static List<Object> ExcelForList(MultipartFile file, Class<?> beanclazz,Boolean titleExist) {
List<Object> list = new ArrayList<Object>();
try {
// IO流读取文件
InputStream input = file.getInputStream();
// 创建文档
Workbook wb = new HSSFWorkbook(input);
// 得到第一张工作表
Sheet sheet = wb.getSheetAt(0);
int i;
if (titleExist) {
i = 1;
} else {
i = 0;
}
// 行的遍历
for (; i <= sheet.getLastRowNum(); i++) {
// 得到行
Row row = sheet.getRow(i);
// 单元格的遍历
// 实例化对象
Object object = beanclazz.newInstance();
Field[] fields = beanclazz.getDeclaredFields();
int j = 0;
for (Field field : fields) {
String fieldName = field.getName();
PropertyDescriptor pd = new PropertyDescriptor(fieldName, beanclazz);
Method getMethod = pd.getWriteMethod();
Cell cell = row.getCell(j++);
try{
int type = cell.getCellType();
if (type == cell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
boolean value = cell.getBooleanCellValue();
getMethod.invoke(object, value);
System.out.println(object);
System.out.println(value);
} else if (type == cell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
Double d = cell.getNumericCellValue();
int value = d.intValue();
getMethod.invoke(object, new Integer(value));
} else if (type == cell.CELL_TYPE_STRING) {
// 返回字符串类型的值
String value = cell.getStringCellValue();
getMethod.invoke(object, new String(value));
}
}catch (Exception e) {
System.out.println("");
}
}
list.add(object);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
5.lib包
- ssm利用poi将excel数据导入数据库
- 利用POI将数据库数据导入Excel
- 利用POI实现将Excel表格中的数据批量导入数据库中
- java利用poi和jfinal框架将excel导入数据库
- Java 利用poi把数据库中数据导入Excel
- 利用poi从excel中导入数据到数据库
- 【copy】利用poi将数据库数据导出excel
- 利用POI将数据表导入Excel
- 利用poi把excel数据导入mysql
- 利用ado将excel数据直接导入数据库
- 利用PHPExcel将数据库数据导入excel表格的方法
- java jxl利用反射将数据库数据导入到Excel
- 利用poi将数据写入到excel
- SSM中利用POI导出数据库到excel表格
- Java 利用POI实现将数据库中内容导入到EXcel中
- 利用POI将数据库表结构信息导入excel模板中
- Java 利用POI实现将数据库中内容导入到EXcel中
- 将数据库数据导入Excel
- Lua创建一个类 继承
- vs2013配置opencv2.4.9
- NOIP2013模拟最小比例
- 大家好,这是我第一篇在CSDN上的博文,本篇主要讨论小型内存池的设计
- 《剑指offer》-02字符串替换
- ssm利用poi将excel数据导入数据库
- Leetcode 130. Surrounded Regions
- Handler
- Qt---安装
- 关于 LINK : fatal error LNK1561: 必须定义入口点 的解决方法
- Java SE-标识符与命名规则
- 2016下半年工作计划
- 北理工Java实验2.5(文件读入与写出)
- toString()没有参数