excel 批量数据导入数据库与数据库导出数据到excel



批量导入数据------spring Data JPA  整合 Hibernate

// 文件上传 ---- 批量导入-----------------------------------

	private File file;

	public void setFile(File file) {
		this.file = file;
	}

	// 文件上传
	@Action(value = "area_upload")
	public String upload() throws IOException {

		List<Area> list = new ArrayList<Area>();

		// 判断文件类型

		// 加载excel文件

		HSSFWorkbook hssf = new HSSFWorkbook(new FileInputStream(file));

		// 读取sheet并遍历
		HSSFSheet sheetAt = hssf.getSheetAt(0);
		for (Row row : sheetAt) {
			// 跳过第一行与空行

			if (row.getRowNum() == 0) {
				continue;
			}
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}

			Area pace = new Area();

			pace.setId(row.getCell(0).getStringCellValue());
			pace.setProvince(row.getCell(1).getStringCellValue());
			pace.setCity(row.getCell(2).getStringCellValue());
			pace.setDistrict(row.getCell(3).getStringCellValue());
			pace.setPostcode(row.getCell(4).getStringCellValue());

			// 生成拼音简码
			String province = pace.getProvince();
			String city = pace.getCity();
			String district = pace.getDistrict();
			// 截取字符串,去掉省市区关键字
			province = province.substring(0, province.length() - 1);
			city = city.substring(0, city.length() - 1);
			district = district.substring(0, district.length() - 1);

			// 生成简码
			String[] headArray = Pinyin4jUtils.getHeadByString(province + city + district);
			StringBuffer sb = new StringBuffer();

			for (String headStr : headArray) {
				sb.append(headStr);
			}
			String shortcode = sb.toString();
			pace.setShortcode(shortcode);

			// 城市编码
			String cityCode = Pinyin4jUtils.hanziToPinyin(city, "");
			pace.setCitycode(cityCode);

			list.add(pace);
		}

		// 调用业务层保存数据
		areaService.upload(list);
		return NONE;
	}



批量导出数据

// 文件导出
	@Action(value="export")
	public String export() {
		// 获取数据
		List<Area> list = areaService.findAll();
		// 创建表格数据
		// 创建excel文件
		HSSFWorkbook hssf = new HSSFWorkbook();

		// 创建sheet
		HSSFSheet sheet = hssf.createSheet("区域信息");
		// 创建第一行
		HSSFRow row1 = sheet.createRow(0);
		// 设置格式
		HSSFCellStyle firstStyle = hssf.createCellStyle();
		// 设置居中格式
		firstStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

		// 设置表头
		// list可以作为参数
		List<String> arr = new ArrayList<String>();
		arr.add("编码");
		arr.add("省市");
		arr.add("城市");
		arr.add("区域");
		arr.add("邮编");
		arr.add("城市编码");
		arr.add("简码");
		
		for (int i = 0; i < arr.size(); i++) {
			HSSFCell createCell = row1.createCell(i);
			createCell.setCellValue(arr.get(i));
			createCell.setCellStyle(firstStyle);

		}

		// 遍历列表将数据存入文件
		// 获取数据库信息
		for (int i = 0; i < list.size(); i++) {
			HSSFRow row2 = sheet.createRow(i + 1);
			Area ar = list.get(i);

			row2.createCell(0).setCellValue(ar.getId());

			row2.createCell(1).setCellValue(ar.getProvince());

			row2.createCell(2).setCellValue(ar.getCity());

			row2.createCell(3).setCellValue(ar.getDistrict());

			row2.createCell(4).setCellValue(ar.getPostcode());

			row2.createCell(5).setCellValue(ar.getCitycode());

			row2.createCell(6).setCellValue(ar.getShortcode());
		}
		try {
			// 导出application/vnd.ms-excel;charset=utf-8
			ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
			
			ServletActionContext.getResponse().setContentType("application/vnd.ms-excel;charset=utf-8");
			
			String filename  = "区域信息.xls";
			filename = new String(filename.getBytes(),"ISO-8859-1");
			ServletActionContext.getResponse().setHeader("Content-Disposition","attachment;filename="+filename);

			hssf.write(out);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return NONE;
	}



excel文件数据


编码省市城市区域邮编城市编码简码
QY044天津市天津市河北区120105tianjinTJTJHB
QY045天津市天津市红桥区120106tianjinTJTJHQ
QY046天津市天津市滨海新区120116tianjinTJTJBHX
QY047天津市天津市东丽区120110tianjinTJTJDL
QY048天津市天津市西青区120111tianjinTJTJXQ
QY049天津市天津市津南区120112tianjinTJTJJN
QY050天津市天津市北辰区120113tianjinTJTJBC
QY051天津市天津市武清区120114tianjinTJTJWQ
QY052天津市天津市宝坻区120115tianjinTJTJBC


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值