Excel导入数据库
导入jxl,junit包
下面是代码
import com.mavenTest.bean.User; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import org.junit.Test; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Created by Administrator on 2018/4/16. */ public class EcxelToDb { @Test public void test(){ File file=new File("book.xls"); try { //获取表 //这里注意使用excel表格工具创建的文件否则报错 Workbook wb= Workbook.getWorkbook(file); //获取工作簿 Sheet sh=wb.getSheet(0); //行数 int c=sh.getColumns(); //列数 int r=sh.getRows(); List<User> users=new ArrayList<User>(); for (int i = 1; i < r; i++) { User user = new User(); for (int j = 0; j < c; j++) { if ((j + 1) % 2 == 0) { user.setPassword(sh.getCell(j, i).getContents()); } else { user.setName(sh.getCell(j, i).getContents()); } } user.setDate(new Date()); users.add(user); } System.out.println(users); } catch (IOException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } } }