乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > java 怎么从excel文件导入到oracle数据库中

java 怎么从excel文件导入到oracle数据库中

作者:乔山办公网日期:

返回目录:excel表格制作


public String[][] readExcel(String filePath) {
String[][] s = null;
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
filePath));
HSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
s = new String[rows][];
if (rows > 0) {
// 获取总列数`
int cells = sheet.getRow(0).getPhysicalNumberOfCells();
for (int r =0; r < rows; r++) {

HSSFRow row = sheet.getRow(r);
String[] cellsvalue = new String[cells+1];
for (short c = 0; c < cells; c++) {
String value = "";
HSSFCell cell = row.getCell(c);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
//
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
value = new java.text.SimpleDateFormat(
"yyyy-MM-dd").format(cell
.getDateCellValue());
} else {
value = String.valueOf(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
value="";
default:
break;
}
if (cell == null) {
value="";
}
}
cellsvalue[c] = value;
if(value.endsWith(".0")){
cellsvalue[c] = value.substring(0, value.length()-2);
}
}
s[r] = cellsvalue;
java.io.File myfile = new java.io.File(filePath);
if (myfile.exists()) {
myfile.delete();
}
}
}
} catch (Exception ex) {
// TODO 自动生成e799bee5baa6e78988e69d83338 catch 块

ex.printStackTrace();
}
return s;
}

java操作excel一般都使用poi来完成:

    1、下载poi相关jar,maven的集成如下:(把${poi.version}替换成你要的版本)

                <dependency>
7a686964616fe4b893e5b19e333 <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、写数据库操作。


我前段时间做项目用的一个读取excel 的读excel 的模块,功能是读取excel 后 返回一个 类的列表 ,在往后无非就是网数据里面写数据那些你可以自己去写啦 ,jar包什么的我给你发邮箱里

有专门的第三方类库,你百度下搜个就可以

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网