日期:2014-05-16  浏览次数:20513 次

导入Excel格式保存到数据库
package com.jwy.excel;

import java.io.FileInputStream;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;




/**
* 读取Excel文档内容
*
* @author Jingweiyu
*/
public class ReadExcel {
public static void main(String[] args) throws Exception {
// 创建文件输入流对象
FileInputStream is = new FileInputStream("readExcel.xls");
// 创建 POI文件系统对象
POIFSFileSystem ts = new POIFSFileSystem(is);
// 获取文档对象
HSSFWorkbook wb = new HSSFWorkbook(ts);
// 获取工作薄
HSSFSheet sheet = wb.getSheetAt(0);
// 声明行对象
HSSFRow row = null;
// 通过循环获取每一行

int totalRows = sheet.getLastRowNum();

System.out.println("输出总行数:" + totalRows);

for (int i = 2; sheet.getRow(i) != null; i++) {

row = sheet.getRow(i);

        Users  users=  getEntity(row);
        save(users);   

           HSSFCell idCell = row.getCell(0);
          
// HSSFCell accountCell = row.getCell(1);
// HSSFCell departmentCell = row.getCell(2);

//System.out.println("userNameCell=="
// + userNameCell.getRichStringCellValue());

//System.out.println("accountCell==" + accountCell);

//System.out.println("departmentCell==" + departmentCell);
// 循环获取一行的中列
for (int j = 0; row.getCell(j) != null; j++) {

// System.out.print(row.getCell(j).toString()+" ");

}

System.out.println();
}

}

public static Users getEntity(HSSFRow row) {

Users us = new Users();

    us.setUserid(Integer.parseInt(row.getCell(0).getRichStringCellValue().toString()));
us.setUsername(row.getCell(1).toString());
us.setUserpwd(row.getCell(4).toString());

return us;

}


public static void  save(Users users){



System.out.println("保存第次");
System.out.println( users.getUserid());
System.out.println( "name---"+users.getUsername());
System.out.println( "pwd----"+users.getUserpwd());



}



}