导入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 ...
随机推荐
- DayPilot 7.8 DLL去DEMO字样下载
来自 DayPilot 的 7.8.3169.1 版本的DLL,微调去掉了“DEMO”字样,供参考,商用请支持正版! 此处下载: http://files.cnblogs.com/files/pcca ...
- JavaScript模板引擎artTemplate.js——为什么使用模板引擎?
作为一个工作一年的菜鸟,在公司做了几个外包项目,也接触到了不同形式的web开发.其实也没多少,就是javaweb开发和HTML5移动开发,这两者在页面展示的时候的解决方案还是有所不同的. 1.vo+e ...
- 深入理解图优化与g2o:g2o篇
内容提要 讲完了优化的基本知识,我们来看一下g2o的结构.本篇将讨论g2o的代码结构,并带着大家一起写一个简单的双视图bundle adjustment:从两张图像中估计相机运动和特征点位置.你可以把 ...
- 【bzoj1913】 Apio2010—signaling 信号覆盖
http://www.lydsy.com/JudgeOnline/problem.php?id=1913 (题目链接) 题意 给出一个平面上n个点,求任选3个点画一个圆所包含的点的期望值. Solut ...
- 学习笔记——git
恩没错,又是个新东西 使用Git提交文件到版本库有两步: 第一步:是使用 git add 把文件添加进去,实际上就是把文件添加到暂存区. 第二步:使用git commit提交更改,实际上就是把暂存区的 ...
- 放下恩怨,曝小米中兴投关键性一票让华为顺利取得5G短码控制权
如果说最近国内科技公司最牛逼最令人振奋的新闻是啥,显然,就是两天前在3GPP RAN1 87次会议的5G短码方案讨论中,华为顺利碾压高通,战胜列强,拿下了5G的控制编码方案的标准. 虽然说,目前,华为 ...
- NOIp2016 十连测 round1
Claris大爷出的一套模拟题.问别人要到了一份题,加深了自己NOIp要滚粗的感觉. Matser zzDP题,我只能说我第一遍写的时候还写崩了QAQ. //master //by Cydiater ...
- Promise deeply lookup
Motivation Consider the following synchronous JavaScript function to read a file and parse it as JSO ...
- JS 原型的妙用
原型是JS的一个重要的特征,通过它可以实现类和实例直接的继承关系. 1.原型来来实现数据备份 // 通过原型来来实现数据备份 function p(x){ this.x = x; } p.protot ...
- Hibernate映射文件如何配置触发器
Hibernate映射文件之触发器生成(generated属性.database-object元素) (2013-02-27 12:28:49) 转载▼ 标签: it 分类: JAVA学习笔记 这里分 ...