简易使用POI导入导出excel文件

首先需要导入5个jar包

poi-3.7-20101029.jar

poi-examples-3.7-20101029.jar

poi-ooxml-3.7-20101029.jar

poi-ooxml-schemas-3.7-20101029.jar

poi-scratchpad-3.7-20101029.jar


在poi中, HSSF开头的类是对后缀为.xls 文件的支持,也就是07之前的excel文件

以 XSSF开头的类是对后缀为 .xlsx文件的支持,也就是07之后包括07 的excel文件,如果在执行过程中 发现了一些诸如版本不对 之类的错误,极有可能是没有使用正确的类来导入excel文件,或者是 excel文件的后缀被修改了 


文件的导入,如果有需求可以直接建立数据库的DAO业务,来导入进数据库  导出同理

public void importStudent(String filePath) {
		List<Student> list = new ArrayList<Student>();
		IStudentDao isss = new StudentDaoImpl();
		// 读取文件
		InputStream is = null;
		try {
			is = new FileInputStream(filePath);
			// 创建表格工作簿,把文件流导入
			XSSFWorkbook hssfWorkbook = new XSSFWorkbook(is);
			Student stu = null;
			// 用来循环看该工作簿有多少个分页
			for (int pages = 0; pages < hssfWorkbook.getNumberOfSheets(); pages++) {
				// 创建分页对象
				XSSFSheet sheet = hssfWorkbook.getSheetAt(pages);
				// 如果该分页为空 则重新循环
				if (sheet == null) {
					continue;
				}
				// 如果不为空,那么用循环来查看该页面有多少行
				// 注意,默认行是从0开始的,如果有表头,则需要剔除
				for (int rows = 1; rows <= sheet.getLastRowNum(); rows++) {
					// 创建表中该行的对象
					XSSFRow hRow = sheet.getRow(rows);
					// 该行不为空则开始导入数据
					if (hRow != null) {
						stu = new bean.Student();
						Manager manger = new Manager();
						// 创建该行中详细列的对象,并将数据赋值给该对象
						// 表格中的列,下标是从0开始 默认第一列留给数据库自动设置的ID
						XSSFCell IDCard = hRow.getCell(1);
						// 某一列的值,需要进行判断是否是字符串数字或者布尔型
						// 该判断最好写为一个方法来专门判断
						// 在这也可以来做一个方法来对某一行的值是否重复做判断
						stu.setIDCard(this.getValue(IDCard));
						XSSFCell name = hRow.getCell(2);
						stu.setName(this.getValue(name));
						// 院系
						XSSFCell department = hRow.getCell(3);
						stu.setDepartment(this.getValue(department));
						// 年级
						XSSFCell grade = hRow.getCell(4);
						stu.setGrade(this.getValue(grade));
						// 班级
						XSSFCell sClass = hRow.getCell(5);
						stu.setsClass(this.getValue(sClass));
						// 方向
						XSSFCell direction = hRow.getCell(6);
						stu.setDirection(this.getValue(direction));
						// 学制
						XSSFCell educationalSystem = hRow.getCell(7);
						stu.setEducationalSystem(this.getValue(educationalSystem));
						list.add(stu);
					}
				}
			}
			System.out.println(list);
			for (Student bean : list) {
				isss.add(bean);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

其实大部分时候可以全部强制转化为字符串,在对字符串进行操作,excel默认不支持15位以上的数字,超过15位后 多出来的无论怎么修改都是0,如身份证 

	// 用来判断表格中具体项值的类型,最终把他们全部返回为字符串
	private String getValue(XSSFCell XSSFCell) {
		 if (XSSFCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
			// 返回数值类型的值
			return String.valueOf(XSSFCell.getNumericCellValue());
		} else {
			// 返回字符串类型的值
			return String.valueOf(XSSFCell.getStringCellValue());
		}
	}



导出

 * @param filePath
	 *            保存的文件路径
	 */
	public static void exportManager(String filePath) {
		// 先从数据库把manager数据导入集合
		List<Manager> list = mDao.findAll();
		// 建立要写入的excel工作簿
		XSSFWorkbook workbook = new XSSFWorkbook();
		// 控制页数
		int page = list.size() / 30;
		// 控制分行数,也可在循环中判断
		int rows = 0;
		for (int i = 0; i <= page; i++) {
			// 建立 页
			XSSFSheet sheet = workbook.createSheet("sheet" + i);
			XSSFRow row = sheet.createRow(0);
			XSSFCell id = row.createCell(0);
			id.setCellValue("ID");

			XSSFCell name = row.createCell(1);
			name.setCellValue("用户名");

			XSSFCell pwd = row.createCell(2);
			pwd.setCellType(pwd.CELL_TYPE_STRING);
			pwd.setCellValue("用户密码");

			XSSFCell email = row.createCell(3);
			email.setCellValue("用户邮箱");
			for (int j = 0; j < list.size(); j++) {
				if (j == 30) {
					// 30行分页
					break;
				}
				// 建立行
				 row = sheet.createRow(j + 2);
				// 得到要输出的对象 因为有分页,所有需要用页数诚意每页显示条数,如果不需要分页则可以去掉
				int x = j + i * 30;
				if (x >= list.size()) {
					break;
				}
				Manager bean = list.get(x);

				 id = row.createCell(0);
				id.setCellValue(bean.getID());

				 name = row.createCell(1);
				name.setCellValue(bean.getUserName());

				 pwd = row.createCell(2);
				pwd.setCellType(pwd.CELL_TYPE_STRING);
				pwd.setCellValue(bean.getUserPwd());

				 email = row.createCell(3);
				email.setCellValue(bean.getEmail());

			}
		}
		OutputStream file = null;
		try {
			file = new FileOutputStream(filePath);
			workbook.write(file);
			file.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (file != null) {
				try {
					file.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}



目前一个小项目中,使用的是sql server 2008 ,大概对1万多条数据,13个字段进行导入导出,不分页也就是30秒内。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值