您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java导入excel到数据库插件 导入到数据库

2023/9/12 20:31:21发布27次查看
1、用到的jar包如下(可以直接到poi官网上下载也可以在文章的附件中下载):
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
xmlbeans-2.3.0.jar
可能有冲突的jar包,如果工程lib中存在,需要删除。
2、xbean-2.1.0.jar具体代码如下:
java代码
package com.yusj;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.outputstream;
import org.apache.poi.hssf.usermodel.hssfworkbook;
import org.apache.poi.ss.usermodel.row;
import org.apache.poi.ss.usermodel.sheet;
import org.apache.poi.ss.usermodel.workbook;
import org.apache.poi.xssf.usermodel.xssfworkbook;/*3、导入和导出excel文件类,支持2003(xls)和2007(xlsx)版本的excel文件
@author yxm
*/
public class operationexcelforpoi {
public static void main(string[] args) {
// 文件所在路径
string execelfile = c:/book2007.xlsx ;
//string execelfile = c:/book2003.xls ;
// 导入excel
new operationexcelforpoi().impexcel(execelfile) ;
// 导出excel
string expfilepath = c:/testbook.xls ;
new operationexcelforpoi().expexcel(expfilepath);
}
/**
* 导入excel
* @param execelfile
*/
public void impexcel(string execelfile){
try {
// 构造 workbook 对象,execelfile 是传入文件路径(获得excel工作区)
workbook book = null;
try {
// excel 2007获取方法
book = new xssfworkbook(new fileinputstream(execelfile));
} catch (exception ex) {
// excel 2003获取方法
book = new hssfworkbook(new fileinputstream(execelfile)); }
// 读取表格的第一个sheet页
sheet sheet = book.getsheetat(0);
// 定义 row、cell
row row;
string cell;
// 总共有多少行,从0开始
int totalrows = sheet.getlastrownum() ;
// 循环输出表格中的内容,首先循环取出行,再根据行循环取出列
for (int i = 1; i <= totalrows; i++) {
row = sheet.getrow(i);
// 处理空行
if(row == null){
continue ;
}
// 总共有多少列,从0开始
int totalcells = row.getlastcellnum() ;
for (int j = row.getfirstcellnum(); j < totalcells; j++) {
// 处理空列
if(row.getcell(j) == null){
continue ; }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product