导入excel数据到数据库
1.上传excel到服务器
jsp页面代码
<form action="actionname" method="post" id="form1" enctype="multipart/form-data"> <input type="file" name="excel" id="fileExecl" class="inputFile" onchange="uploadFile(this)" size="1" title=""/>
<input type="button" value="导入excel" onclick="importExcel()"> <form>
js代码
function importExcel() {
var fileExecl = document.getElementById("fileExecl").value;
if(fileExecl==null||fileExecl==""){
alert("请选择excel文件");
return false;
}
document.getElementById("action").value="importExcel";
document.getElementById("form1").submit();
}
function uploadFile(importObj) {
var path = importObj.value;
var type = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
if (type != "xlsx"&&type != "xls") {
alert("请上传xlsx或xls后缀的Excel");
importObj.value = "";
} else {
document.getElementById("action").value="importExcel";
}
}
action代码
private File excel;//上传的文件
private String excelFileName; // File属性名 + FileName固定的 private File uploadFile() {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(excel);
String uploadPath = this.getServletContext().getRealPath("/staticFiles");
//分解路径
//String filePath = getFileDirectory(uploadPath);
File destFile = new File(uploadPath, excelFileName);
os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
return destFile;
} catch (FileNotFoundException e) {
logger.error(e, e);
} catch (IOException e) {
logger.error(e, e);
}
return null;
} public String importExcel() { //文件上传路径
File file = uploadFile();
String filePath = file.getPath();
//获取导出数据
ImportExcel importExcel = new ImportExcel();
List<String[]> importList = importExcel.getImportList(filePath);
//删除上传文件
if(file.isFile() && file.exists()) {
file.delete();
}
for (int i = 1; i < importList.size(); i++) {
String[] rowData = importList.get(i);
//rowData[0] excel中第一列数据
} return null;
}
解析excel代码 xls和xlsx两种格式,代码可以优化
public class ImportExcel {
/**
*
* @param filePath 文件路径
* @return excel中一行数据储存在一个数组String[]
*/
public List<String[]> getImportList(String filePath) {
List<String[]> rowsData = new ArrayList<String[]>();;
if(filePath.endsWith("xls")){
try {
boolean importData = false;
HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(filePath));
HSSFSheet sheet = hwb.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();// 获取表格的行数
for (int r = 0; r < rows; r++) { // 循环遍历表格的行
importData = false;
String cellValue = "";
HSSFRow row = sheet.getRow(r);
if (row != null) {
int cells =row.getLastCellNum();
String[] rowData = new String[cells];
for (int c = row.getFirstCellNum(); c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { // 判断单元格的值是否为字符串类型
cellValue = cell.getStringCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { // 判断单元格的值是否为数字类型
cellValue = cell.getNumericCellValue() + "";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { // 判断单元格的值是否为布尔类型
cellValue = cell.getStringCellValue();
} else {
cellValue = "";
}
}
if (cellValue.trim().length() > 0) {
importData = true;
}
rowData[c] = cellValue;
}
//数据全为空,不导入
if (r > 0 && !importData) {
continue;
}
rowsData.add(rowData);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}else if(filePath.endsWith("xlsx")){
try {
boolean importData = false;
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(
filePath)); // 创建对Excel工作簿文件的引用
XSSFSheet sheet = workbook.getSheetAt(0); // 创建对第一个工作表的引用,多个工作表暂未实现
int rows = sheet.getPhysicalNumberOfRows();// 获取表格的行数
for (int r = 0; r < rows; r++) { // 循环遍历表格的行
importData = false;
String cellValue = "";
XSSFRow row = sheet.getRow(r); // 获取单元格中指定的行对象
if (row != null) {
int cells = row.getPhysicalNumberOfCells();// 获取单元格中指定列对象
String[] rowData = new String[cells];
for (short c = 0; c < cells; c++) { // 循环遍历单元格中的列
XSSFCell cell = row.getCell((short) c); // 获取指定单元格中的列
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { // 判断单元格的值是否为字符串类型
cellValue = cell.getStringCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { // 判断单元格的值是否为数字类型
cellValue = cell.getNumericCellValue() + "";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { // 判断单元格的值是否为布尔类型
cellValue = cell.getStringCellValue();
} else {
cellValue = "";
}
}
if (cellValue.trim().length() > 0) {
importData = true;
}
rowData[c] = cellValue;
}
//数据全为空,不导入
if (r > 0 && !importData) {
continue;
}
rowsData.add(rowData);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return rowsData;
}
}
导入excel数据到数据库的更多相关文章
- 【转】 如何导入excel数据到数据库,并解决导入时间格式问题
在办公环境下,经常会用到处理excel数据,如果用写程序导入excel数据到数据库那就太麻烦了,涉及解析excel,还要各种格式问题,下面简单利用数据库本身支持的功能解决这类导入问题. 准备 创建表 ...
- .net导入excel数据到数据库中
在开发过程中我们经常面临着需要将数据导出或者导入到系统中,例如一些生产管理系统,项目管理系统等等都会有这样的需求: 将excel数据到系统中思路:获取excel中每一行的数据,然后存入集合中,批量添加 ...
- java 使用poi 导入Excel 数据到数据库
由于我个人电脑装的Excel是2016版本的,所以这地方我使用了XSSF 方式导入 . 1先手要制定一个Excel 模板 把模板放入javaWeb工程的某一个目录下如图: 2模板建好了后,先实现模板下 ...
- PHP 导入Excel数据 到数据库
/** * 导入excel * @throws \PHPExcel_Exception * @throws \PHPExcel_Reader_Exception */ public function ...
- Web服务器与数据库服务器分离 导入 Excel数据至数据库
一般情况一般项目WEB服务器与数据库均部署在一台服务器,文件上传,数据导入在一台服务器完成.web服务器与数据库服务器分离,文件上传与数据导入将分布在两台服务器或多台服务器之间.本案例为两台服务器,具 ...
- java 对excel操作导入excel数据到数据库
加入jar包jxl.jar ===================services层掉用工具类==================================== // 导入 public Lis ...
- 如何批量导入excel数据至数据库(MySql)--工具phpMyAdmin
之前由于数据储存使用excel保存了所有数据,经过初步数据筛选,数据量近4000条.一条一条录入数据库显然是不可行的.以下是我所操作的步骤: 1.只保留excel的数据部分,去除第一行的具体说明 2. ...
- MYSQL 导入Excel数据到数据库中
1,先把excel的数据整理整齐,如每列都要保持同样的格式:就一列一列的数据: 2,导出excel的数据为CSV格式,即把excel的数据另存为xxxx.csv;: 3,用EditPlus工具将xxx ...
- Winform导入Excel数据到数据库
public partial class ImportExcel : Form { AceessHelpers accessHelper = new AceessHelpers(); public I ...
随机推荐
- Prototype原型(创建型模式)
依赖关系的倒置:抽象不应该依赖于实现的细节,实现细节应该依赖于抽象. 原型模式的定义 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.prototype模式允许一个对象再创建另外一个可 ...
- springMVC Aspect AOP 接口耗时统计
在接口开发中,我们通常需要统计接口耗时,为后续接口性能做统计.在springMVC中可以用它的aop来记录日志. 1.在spring配置文件中开启AOP <!--*************** ...
- Android 手机怎么录屏制成gif图片
参考:http://www.cnblogs.com/dasusu/p/4903511.html 上面的博主说的很详细了,但作为学习记录我就重新写一遍帮助自己加深记忆 一.准备条件 1.你搭建了Andr ...
- Java的多线程机制系列:(一)总述及基础概念
前言 这一系列多线程的文章,一方面是个人对Java现有的多线程机制的学习和记录,另一方面是希望能给不熟悉Java多线程机制.或有一定基础但理解还不够深的读者一个比较全面的介绍,旨在使读者对Java的多 ...
- bzoj2179: FFT快速傅立叶
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- 时间戳 时区 java mysql
当一个时间 比如2016年5月6日,生成时间戳.这个运算是与时区有关的.首先得确认这个时间是哪个时区的,然后转换成utc时区的时间.再减去1970,得到的秒数,就是时间戳. 时间戳是个一定的值,他与时 ...
- Oracle数据库开发
Oracle数据库开发之PL/SQL基础实战视频课程 1 PL/SQL 简介 2 入门实例(一) 3 入门实例(二) 4 PL/SQL 变量和常量 5 PL/SQL数据类型(一) 6 PL/SQL数据 ...
- ArcGIS Server开发教程系列(7)使用ArcGIS API for Javascript-Hello World
ArcGIS API for Javascript API下载地址:http://support.esrichina-bj.cn/2011/0223/960.html 选择最新的下载就好了,目前是3 ...
- Git 学习笔记参考
1.参考学习资料 网上资料: http://www.cnblogs.com/aoguren/p/4189086.html http://www.liaoxuefeng.com/wiki/0013739 ...
- C C++ 语法
非常酷的网站: http://yige.org/cpp/defined_data_types.php 在Linux下有一个目录/proc/$(pid),这个目录保存了进程号为pid的进程运行时的所有信 ...