spring整合poi配置学习总结

poi依赖

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>3.14</version>
</dependency>

代码示例

ssh示例

导出

action值处理
public void export() {
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.setHeader("Content-Disposition", "attachment;filename="+new String(("Orders_"+getId()+".xls").getBytes(), "iso-8859-1"));
			ordersBiz.export(response.getOutputStream(), getId());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
逻辑具体实现
public void export(OutputStream os, Long uuid) {
		Orders orders = ordersDao.get(uuid);
		String sheetName = "";
		if(Orders.TYPE_IN.equals(orders.getType())) {
			sheetName = "采 购 单";
		}
		if(Orders.TYPE_OUT.equals(orders.getType())) {
			sheetName = "销 售 单";
		}
		// 创建工作簿
		HSSFWorkbook wb = new HSSFWorkbook();
		// 创建工作表
		HSSFSheet sheet = wb.createSheet(sheetName);
		// 创建行,索引从0开始
		HSSFRow row = sheet.createRow(0);
		// 设置合并标题的单元格
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
		row.createCell(0).setCellValue(sheetName);
		
		// 设置表格样式
		HSSFCellStyle style_content = wb.createCellStyle();
		// 水平居中
		style_content.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 垂直居中
		style_content.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 设置边框
		style_content.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style_content.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style_content.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style_content.setBorderTop(HSSFCellStyle.BORDER_THIN);
		// 创建字体
		HSSFFont font_content = wb.createFont(); 
		font_content.setFontName("宋体");
		// 设置字体大小
		font_content.setFontHeightInPoints((short)11);
		// 设置字体
		style_content.setFont(font_content);
		
		HSSFCellStyle style_date = wb.createCellStyle();
		style_date.cloneStyleFrom(style_content);
		// 创建需要自定义的日期格式
		HSSFDataFormat format = wb.createDataFormat();
		// 设置日期格式,会对所有数值进行格式化
		// 因为并非所有数值都需要日期格式,所以需要克隆一个日期样式进行渲染
		style_date.setDataFormat(format.getFormat("yyyy-MM-dd HH:mm:ss"));
		
		HSSFCellStyle style_title = wb.createCellStyle();
		style_title.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style_title.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		
		// 创建字体
		HSSFFont font_title = wb.createFont(); 
		font_title.setFontName("黑体");
		// 设置字体大小
		font_title.setFontHeightInPoints((short)18);
		// 设置粗体
		font_title.setBold(true);
		// 设置字体
		style_title.setFont(font_title);
		
		// 设置第一行的高
		row.setHeight((short)1000);
		row.getCell(0).setCellStyle(style_title);
		// 获取订单明细
		List<Orderdetail> detail = orders.getOrderDetail();
		int rowCount = detail.size()+9;
		for (int i = 2; i <= rowCount; i++) {
			row = sheet.createRow(i);
			for(int j = 0; j < 4; j++) {
				row.createCell(j).setCellStyle(style_content);
				sheet.setColumnWidth(j, 6000);
			}
			row.setHeight((short)500);
		}
		// 设置采购明细
		// 因为对应的row,cell已经创建,所以可以通过sheet获取,如果没有创建,则为null值
		sheet.getRow(2).getCell(0).setCellValue("供应商");
		// 缓存已查询到的数据
		Map<Long, String> empNameMap = new HashMap<Long, String>();
		Map<Long, String> supplierNameMap = new HashMap<Long, String>();
		
		// 设置供应商名称
		sheet.getRow(2).getCell(1).setCellValue(getSupplierName(orders.getSupplieruuid(), supplierNameMap));
		
		// 设置日期标题
		sheet.getRow(3).getCell(0).setCellValue("下单日期");
		sheet.getRow(4).getCell(0).setCellValue("审核日期");
		sheet.getRow(5).getCell(0).setCellValue("采购日期");
		sheet.getRow(6).getCell(0).setCellValue("入库日期");

		// 设置获取的日期
		if(null != orders.getCreatetime()) {
			sheet.getRow(3).getCell(1).setCellValue(orders.getCreatetime());
		}
		if(null != orders.getChecktime()) {
			sheet.getRow(4).getCell(1).setCellValue(orders.getChecktime());
		}
		if(null != orders.getStarttime()) {
			sheet.getRow(5).getCell(1).setCellValue(orders.getStarttime());
		}
		if(null != orders.getEndtime()) {
			sheet.getRow(6).getCell(1).setCellValue(orders.getEndtime());
		}
		
		// 设置经办人标题
		for(int i = 3; i <= 6; i++) {
			sheet.getRow(i).getCell(2).setCellValue("经办人");
			sheet.getRow(i).getCell(2).setCellValue("经办人");
			sheet.getRow(i).getCell(2).setCellValue("经办人");
			sheet.getRow(i).getCell(2).setCellValue("经办人");

			// 设置日期格式
			sheet.getRow(i).getCell(1).setCellStyle(style_date);
			sheet.getRow(i).getCell(1).setCellStyle(style_date);
			sheet.getRow(i).getCell(1).setCellStyle(style_date);
			sheet.getRow(i).getCell(1).setCellStyle(style_date);
			
		}
		
		// 设置经办人
		sheet.getRow(3).getCell(3).setCellValue(getEmpName(orders.getCreater(), empNameMap, empDao));
		sheet.getRow(4).getCell(3).setCellValue(getEmpName(orders.getChecker(), empNameMap, empDao));
		sheet.getRow(5).getCell(3).setCellValue(getEmpName(orders.getStarter(), empNameMap, empDao));
		sheet.getRow(6).getCell(3).setCellValue(getEmpName(orders.getEnder(), empNameMap, empDao));
		
		// 设置合并供应商名称的单元格
		// 索引从0开始 逻辑第3行->第3行;第2列->第4列
		sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 3));
		
		// 设置合并订单明细单元格
		sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 3));
		sheet.getRow(7).getCell(0).setCellValue("订单明细");
		
		// 设置订单明细标题
		sheet.getRow(8).getCell(0).setCellValue("商品名称");
		sheet.getRow(8).getCell(1).setCellValue("数量");
		sheet.getRow(8).getCell(2).setCellValue("价格");
		sheet.getRow(8).getCell(3).setCellValue("金额");
		
		rowCount = 9;
		for (Orderdetail orderdetail : detail) {
			// 填充数据
			sheet.getRow(rowCount).getCell(0).setCellValue(orderdetail.getGoodsname());
			sheet.getRow(rowCount).getCell(1).setCellValue(orderdetail.getNum());
			sheet.getRow(rowCount).getCell(2).setCellValue(orderdetail.getPrice());
			sheet.getRow(rowCount).getCell(3).setCellValue(orderdetail.getMoney());
			
			rowCount++;
		}
		// 设置合计金额
		sheet.getRow(rowCount).getCell(0).setCellValue("合计");
		sheet.getRow(rowCount).getCell(3).setCellValue(orders.getTotalmoney());
		try {
			// 将流内容写入表格
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				wb.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
表格样式图示

在这里插入图片描述

导入

action值处理
public class SupplierAction extends BaseAction<Supplier> {

	private ISupplierBiz supplierBiz;
	
	public void setSupplierBiz(ISupplierBiz supplierBiz) {
		this.supplierBiz = supplierBiz;
		setBaseBiz(supplierBiz);
	}
	// 上传的文件
	private File file;
	// 文件名
	private String fileFileName;
	// 文件类型
	private String fileContentType;
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public void doImport() {
		// 判断文件类型
		if(!"application/vnd.ms-excel".equals(fileContentType)) {
			ajaxReturn(false, "必须上传excel文件");
			return;
		}
		try {
			supplierBiz.doImport(new FileInputStream(file));
			ajaxReturn(true, "上传文件成功");
		} catch (IOException e) {
			ajaxReturn(false, "上传文件失败");
			e.printStackTrace();
		} catch (ErpException e) {
			ajaxReturn(false, e.getMessage());
			e.printStackTrace();
		} 
	}
	// 将信息返回给前端
	public void ajaxReturn(boolean success,String message){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", success);
		map.put("message", message);
		write(JSON.toJSONString(map));		
	}
	public void write(String jsonString){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");	
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().print(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}
逻辑具体实现

可以实现,“有则改之无则加勉”。

public void doImport(InputStream is) throws IOException {
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(is);
			// 获取工作表
			HSSFSheet sheet = wb.getSheetAt(0);
			// 获取工作表名
			String sheetName = sheet.getSheetName();
			String type = "";
			if("供应商".equals(sheetName)) {
				type = Supplier.TYPE_SUPPLIER;
			} else if("客户".equals(sheetName)) {
				type = Supplier.TYPE_CUSTOMER;
			} else {
				throw new ErpException("工作表名称不正确");
			}
			// 获取工作表内容
			Supplier supplier = null;
			// i = 0,是标题行,不用读取
			for(int i = 1; i <= sheet.getLastRowNum(); i++) {
				supplier = new Supplier();
				// 名称
				supplier.setName(sheet.getRow(i).getCell(0).getStringCellValue());
				// 查询数据库该名称是否存在
				// [数据库中有表中数据,是持久态,如果有设值的代码执行,可以自动更新数据库]
				List<Supplier> list = supplierDao.getList(null, supplier, null);
				if(list != null & list.size() > 0) {
					supplier = list.get(0);
				}
				// 地址
				supplier.setAddress(sheet.getRow(i).getCell(1).getStringCellValue());
				// 联系人
				supplier.setContact(sheet.getRow(i).getCell(2).getStringCellValue());
				// 电话
				supplier.setTele(sheet.getRow(i).getCell(3).getStringCellValue());
				// 邮箱
				supplier.setEmail(sheet.getRow(i).getCell(4).getStringCellValue());
				
				// 新增数据到数据库[数据库中没有表中的数据]
				if(list.size() == 0) {
					supplier.setType(type);
					supplierDao.add(supplier);
				}
			}
		} finally {
			if(null != wb) {
				try {
					wb.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值