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数据到数据库的更多相关文章

  1. 【转】 如何导入excel数据到数据库,并解决导入时间格式问题

    在办公环境下,经常会用到处理excel数据,如果用写程序导入excel数据到数据库那就太麻烦了,涉及解析excel,还要各种格式问题,下面简单利用数据库本身支持的功能解决这类导入问题. 准备 创建表 ...

  2. .net导入excel数据到数据库中

    在开发过程中我们经常面临着需要将数据导出或者导入到系统中,例如一些生产管理系统,项目管理系统等等都会有这样的需求: 将excel数据到系统中思路:获取excel中每一行的数据,然后存入集合中,批量添加 ...

  3. java 使用poi 导入Excel 数据到数据库

    由于我个人电脑装的Excel是2016版本的,所以这地方我使用了XSSF 方式导入 . 1先手要制定一个Excel 模板 把模板放入javaWeb工程的某一个目录下如图: 2模板建好了后,先实现模板下 ...

  4. PHP 导入Excel数据 到数据库

    /** * 导入excel * @throws \PHPExcel_Exception * @throws \PHPExcel_Reader_Exception */ public function ...

  5. Web服务器与数据库服务器分离 导入 Excel数据至数据库

    一般情况一般项目WEB服务器与数据库均部署在一台服务器,文件上传,数据导入在一台服务器完成.web服务器与数据库服务器分离,文件上传与数据导入将分布在两台服务器或多台服务器之间.本案例为两台服务器,具 ...

  6. java 对excel操作导入excel数据到数据库

    加入jar包jxl.jar ===================services层掉用工具类==================================== // 导入 public Lis ...

  7. 如何批量导入excel数据至数据库(MySql)--工具phpMyAdmin

    之前由于数据储存使用excel保存了所有数据,经过初步数据筛选,数据量近4000条.一条一条录入数据库显然是不可行的.以下是我所操作的步骤: 1.只保留excel的数据部分,去除第一行的具体说明 2. ...

  8. MYSQL 导入Excel数据到数据库中

    1,先把excel的数据整理整齐,如每列都要保持同样的格式:就一列一列的数据: 2,导出excel的数据为CSV格式,即把excel的数据另存为xxxx.csv;: 3,用EditPlus工具将xxx ...

  9. Winform导入Excel数据到数据库

    public partial class ImportExcel : Form { AceessHelpers accessHelper = new AceessHelpers(); public I ...

随机推荐

  1. 通过接口实现JAVA和.NET互调用-JNInterface

    使用C#编程多年,也十分感激微软在语言架构.语法糖.编辑器等方面给自己带来的便利.但因为最近工作中有接触到JAVA,渐渐地发现的确像大家说的那样,JAVA的生态很好,要找点什么几乎都有现成的,于是自然 ...

  2. [转]iostat命令详解

    iostat iostat用于输出CPU和磁盘I/O相关的统计信息.  命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ devi ...

  3. IntelliJ工程导入

    如果build.gradle中的sourceCompatibility=1.5,那么无法使用钻石运算符,如下语句报错. List<String>a=new ArrayList<> ...

  4. php根据地址的经纬度查询周围的城市例子

    目前的工作是需要对用户的一些数据进行分析,每个用户都有若干条记录,每条记录中有用户的一个位置,是用经度和纬度表示的.还有一个给定的数据库,存储的是一些已知地点以及他们的经纬度,内有43W多条的数据.现 ...

  5. 使用antd UI组件有感

    公司使用的的react.js的版本提14.7的,JS版本使用的是ES6语法,因此在使用antd过程中,有些许不愉快的记录,分享给大家,一起学习: 如果是react 14.7版本时,使用getField ...

  6. 解决:Win 10安装软件时提示:文件系统错误 (-1073740940)

    1.win+R输入 gpedit.msc 2.左边计算机配置 windows设置——安全设置——本地策略——安全选项 3.在安全选项右边选择 用户账户控制:管理员批准模式中管理员的提升权限提示的行为, ...

  7. POJ2417 Discrete Logging

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. 深度分析mysql GROUP BY 与 ORDER BY

    鉴于项目的需要,就从网上找到该文章,文章分析得很详细也很易懂,在android里,(不知道是不是现在水平的限制,总之我还没找到在用ContentProvider时可以使用子查询),主要方法是用SQLi ...

  9. C#中时间的比较

    项目中需求,要求一个线程必须待够一定时间才允许停止,那么就涉及到一个时间的比较与线程的sleep var threadTimeOut= DateTime.Now.AddMinutes(timeOutN ...

  10. Google Map API Version3 :代码添加和删除marker标记

    转自:http://blog.sina.com.cn/s/blog_4cdc44df0100u80h.html Google Map API Version3 教程:在地图 通过代添加和删除mark标 ...