Java全栈开发---Java ERP系统开发:商业ERP(十二)数据的导入导出(Excel)
发布日期:2021-06-29 15:03:08
浏览次数:2
分类:技术文章
本文共 13277 字,大约阅读时间需要 44 分钟。
数据的导入导出
一、供应商或客户数据导出
1、需求分析
点击导出按钮,将供应商或客户的信息导出为excel文档
2、POI介绍
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“可怜的模糊实现”。
用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)结构:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。 HWPF - 提供读写Microsoft Word DOC格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读Microsoft Visio格式档案的功能。 HPBF - 提供读Microsoft Publisher格式档案的功能。 HSMF - 提供读Microsoft Outlook格式档案的功能。3、添加POI依赖,在erp_partent下的pom.xml当中
4、在erp_biz下的ISupplierBiz当中
5、修改erp_entity下的Supplier
6、在erp_biz下的ISupplierBiz的实现类SupplierBiz
package com.itzheng.erp.biz.impl;import java.io.IOException;import java.io.OutputStream;import java.util.List;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 com.itzheng.erp.biz.ISupplierBiz;import com.itzheng.erp.dao.ISupplierDao;import com.itzheng.erp.entity.Supplier;/** * 供应商业务逻辑类 * @author Administrator * */public class SupplierBiz extends BaseBizimplements ISupplierBiz { private ISupplierDao supplierDao; public void setSupplierDao(ISupplierDao supplierDao) { this.supplierDao = supplierDao; setBaseDao(supplierDao); } /** * 导出数据 */ public void export(OutputStream os, Supplier t1) { //获取要导出的数据列表 List list = supplierDao.getList(t1, null, null); //创建一个工作簿 HSSFWorkbook wb = new HSSFWorkbook(); String sheetName = ""; if(Supplier.TYPE_CUSTOMER.equals(t1.getType())){ sheetName = "客户"; } if(Supplier.TYPE_SUPPLIER.equals(t1.getType())){ sheetName = "供应商"; } //创建一个工作表 HSSFSheet sheet = wb.createSheet(sheetName); //创建一行,行的索引是从0开始, 写标题 HSSFRow row = sheet.createRow(0); String[] header = { "名称","地址","联系人","电话","Email"}; int[] width = { 5000,8000,4000,8000,10000}; HSSFCell cell = null; for(int i = 0; i < header.length; i++){ cell = row.createCell(i); cell.setCellValue(header[i]); //设置列宽 sheet.setColumnWidth(i, width[i]); } //导出的内容 int rowCount = 1; for(Supplier supplier : list){ row = sheet.createRow(rowCount); row.createCell(0).setCellValue(supplier.getName());//名称 row.createCell(1).setCellValue(supplier.getAddress());//地址 row.createCell(2).setCellValue(supplier.getContact());//联系人 row.createCell(3).setCellValue(supplier.getTele());//电话 row.createCell(4).setCellValue(supplier.getEmail());//Email rowCount++; } try { wb.write(os); } catch (IOException e) { e.printStackTrace(); } finally{ try { wb.close(); } catch (IOException e) { e.printStackTrace(); } } }}
7、 在erp_web下的SupplierAction当中
/** * 导出数据 */ public void export(){ String filename = ""; if(Supplier.TYPE_SUPPLIER.equals(getT1().getType())){ filename = "供应商"; } if(Supplier.TYPE_CUSTOMER.equals(getT1().getType())){ filename = "客户"; } filename += ".xls"; //响应对象 HttpServletResponse response = ServletActionContext.getResponse(); try { //设置输出流,实现下载文件 response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"ISO-8859-1")); supplierBiz.export(response.getOutputStream(), getT1()); } catch (IOException e) { e.printStackTrace(); } }
8、设置列宽
9、前端的实现
1)创建download.js
在erp_web下的src当中的webapp/ui当中创建download.js
// Ajax 文件下载$.download = function(url, data){ // 获得url和data var inputs = ''; $.each(data, function(name, value) { inputs+=''; }); $('') .appendTo('body').submit().remove(); };
2)在erp_web下的src当中的webapp/ui当中supplier.html当中引入download.js
3)修改crud.js
二、导出订单
1、导出订单后端biz
(1)在erp_biz下的IOrdersBiz当中
(2)IOrdersBiz的实现类当中OrdersBiz
@Override public void export(OutputStream os, Long uuid) { // 创建一个工作簿 HSSFWorkbook wb = new HSSFWorkbook(); // 获取订单 Orders orders = ordersDao.get(uuid); Listdetaillist = orders.getOrderDetails(); // 创建工作表 String sheetName = ""; if (Orders.TYPE_IN.equals(orders.getType())) { sheetName = "采购单"; } if (Orders.TYPE_OUT.equals(orders.getType())) { sheetName = "销售单"; } HSSFSheet sheet = wb.createSheet(sheetName); // 创建一行,行的索引是从0开始 HSSFRow row = sheet.createRow(0); // 创建单元格,列的索引是从0 开始 HSSFCell cell = row.createCell(0); // 创建单元格样式 HSSFCellStyle style_content = wb.createCellStyle(); style_content.setBorderBottom(BorderStyle.THIN);// 下边框 style_content.setBorderTop(BorderStyle.THIN);// 上边框 style_content.setBorderLeft(BorderStyle.THIN);// 左边框 style_content.setBorderRight(BorderStyle.THIN);// 右边框 // 设置水平对其方式为居中 style_content.setAlignment(HorizontalAlignment.CENTER); // 设置垂直对其方式为居中 style_content.setVerticalAlignment(VerticalAlignment.CENTER); // 设置日期格式 HSSFCellStyle style_date = wb.createCellStyle(); // 把style_context里面样式复制到style_date style_date.cloneStyleFrom(style_content); DataFormat df = wb.createDataFormat(); style_date.setDataFormat(df.getFormat("yyyy-MM-dd HH:mm:ss")); // 设置标题的样式 HSSFCellStyle style_title = wb.createCellStyle(); style_title.setAlignment(HorizontalAlignment.CENTER); style_title.setVerticalAlignment(VerticalAlignment.CENTER); HSSFFont style_font = wb.createFont(); style_font.setFontName("黑体"); style_font.setFontHeightInPoints((short) 18); // 加粗 style_font.setBold(true); style_title.setFont(style_font); // 创建内容样式的字体 HSSFFont font_content = wb.createFont(); // 设置字体名称,相当于选中了那种字符 font_content.setFontName("宋体"); // 设置字体的大小 font_content.setFontHeightInPoints((short) 11); style_content.setFont(font_content); // 合并单元格 // 合并:标题 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3)); // 合并第二行 sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 3)); // 合并第7行 sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 3)); // 创建11行,4列 // 创建矩阵11行,4列 int rowCount = detaillist.size() + 9;// 创建单元格的数量是数据的长度+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); } } // 必须先有创建的行和单元格,才可以使用 // 创建标题单元格 HSSFCell titleCell = sheet.createRow(0).createCell(0); titleCell.setCellValue(sheetName); // 设置标题样式 titleCell.setCellStyle(style_title); sheet.getRow(2).getCell(0).setCellValue("供应商"); sheet.getRow(3).getCell(0).setCellValue("下单日期"); sheet.getRow(4).getCell(0).setCellValue("审核日期"); sheet.getRow(5).getCell(0).setCellValue("采购日期"); sheet.getRow(6).getCell(0).setCellValue("入库日期 "); sheet.getRow(3).getCell(2).setCellValue("经办人"); sheet.getRow(4).getCell(2).setCellValue("经办人"); sheet.getRow(5).getCell(2).setCellValue("经办人"); sheet.getRow(6).getCell(2).setCellValue("经办人"); 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("金额"); // 设置行高于列宽 // 标题行高 sheet.getRow(0).setHeight((short) 1000); // 内容体的行高 for (int i = 2; i <= rowCount; i++) { sheet.getRow(i).setHeight((short) 500); } // 设置列宽 for (int i = 0; i < 4; i++) { sheet.setColumnWidth(i, (short) 5000); } // 订单详情,设置日期与经办人 // 设置单元格样式 sheet.getRow(3).getCell(1).setCellStyle(style_date); sheet.getRow(4).getCell(1).setCellStyle(style_date); sheet.getRow(5).getCell(1).setCellStyle(style_date); sheet.getRow(6).getCell(1).setCellStyle(style_date); 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()); } // 缓存员工编号的名称,key=员工编号,value=员工名称 Map empNameMap = new HashMap (); // 设置经办人 sheet.getRow(3).getCell(3).setCellValue(getEmpName(orders.getCreater(), empNameMap, empDao)); sheet.getRow(4).getCell(3).setCellValue(getEmpName(orders.getStarter(), empNameMap, empDao)); sheet.getRow(5).getCell(3).setCellValue(getEmpName(orders.getStarter(), empNameMap, empDao)); sheet.getRow(6).getCell(3).setCellValue(getEmpName(orders.getEnder(), empNameMap, empDao)); // 缓存供应商编号与员工的名称,key=供应商的编号,value=供应商的名称 Map supplierNameMap = new HashMap (); // 设置供应商 sheet.getRow(2).getCell(1).setCellValue(getSupplierName(orders.getSupplieruuid(), supplierNameMap)); // 将数据库当中的数据填充(设置明细内容) int index = 0; Orderdetail od = null; for (int i = 9; i < rowCount; i++) { od = detaillist.get(index); row = sheet.getRow(i); row.getCell(0).setCellValue(od.getGoodsname()); row.getCell(1).setCellValue(od.getNum()); row.getCell(2).setCellValue(od.getPrice()); row.getCell(3).setCellValue(od.getMoney()); index++; } sheet.getRow(rowCount).getCell(0).setCellValue("合计"); // 设置合计 sheet.getRow(rowCount).getCell(3).setCellValue("" + orders.getTotalmoney()); // 写到输出流当中 try { wb.write(os); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { wb.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2、导出订单后端OrdersAction当中
3、导出订单前端实现
(1)在orders.html当中引入download.js
(2)修改orders.js
//添加审核按钮 var toolbar = new Array(); toolbar.push({ text:'导出', iconCls:'icon-excel', handler:doExport }) if(Request['oper'] == 'doCheck' ){ toolbar.push({ toolbar:[{ text:'审核', iconCls:'icon-search', handler:doCheck }] }); } //添加确认按钮 if(Request['oper'] == 'doStart' ){ toolbar.push({ toolbar:[{ text:'确认', iconCls:'icon-search', handler:doStart }] }); } $('#ordersDlg').dialog({ toolbar:toolbar });
function doExport(){ $.download("orders_export",{ "id":$('#uuid').html()});}
三、导入订单(应商或客户数据导入)
我们需要将批量的供应商(客户)信息导入到系统里,因此我们需要实现导入功能
如下图:点击导入按钮 弹出导入数据对话框: 选择excel文件后点击“导入”按钮,把数据导入到系统中,成功后刷新表格并关闭“导入数据”窗口1、修改SupplierDao
2、后端代码实现,在erp_biz下的ISupplier和Supplier
(1)ISupplier
(2)SupplierBiz当中
@Override public void doImport(InputStream is) throws IOException { HSSFWorkbook wb = null; try { wb = new HSSFWorkbook(is); HSSFSheet sheet = wb.getSheetAt(0); String type = ""; if("供应商".equals(sheet.getSheetName())) { type = Supplier.TYPE_SUPPLIER; }else if("客户".equals(sheet.getSheetName())) { type = Supplier.TYPE_CUSTOMER; }else { throw new ErpException("工作表名称不正确"); } //读取数据 int lastRow = sheet.getLastRowNum();//获取最后一行的行号 Supplier supplier = null; for (int i = 1; i <= lastRow; i++) { supplier = new Supplier(); supplier.setName(sheet.getRow(i).getCell(0).getStringCellValue());//设置供应商名称 //判断是否已经存在,通过名称来判断 Listlist = supplierDao.getList(null, supplier, null); if(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) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
3、SupplierAction当中
/* * 导入数据 */ public void doImport() { //文件类型判断 if(!"application/vnd.ms-excel".equals(fileContentType)){ write(ajaxReturn(false, "上传文件必须是excel文件")); return; } try { supplierBiz.doImport(new FileInputStream(file)); write(ajaxReturn(true, "上传文件成功")); } catch (ErpException e) { write(ajaxReturn(false, e.getMessage())); } catch (IOException e) { write(ajaxReturn(false, "上传文件失败")); e.printStackTrace(); } }
4、供应商或客户的导入——前端
(1)修改supplier.html
(2)修改crud.js
},'-',{ text: '导入', iconCls: 'icon-save', handler: function(){ $('#importDlg').dialog('open'); } }]
//判断是否有导入的功能 var importForm = document.getElementById("importForm"); if(importForm){ //添加的窗口 $('#importDlg').dialog({ title:'导入数据', width:330, height:160, modal:true, closed:true, buttons:[ { text: '导入', handler:function(){ $.ajax({ url: name + '_doImport', data:new FormData($('#importForm')[0]), type:'post', processData:false, contentType:false, dataType:'json', success:function(rtn){ $.messager.alert('提示',rtn.message,'info',function(){ if(rtn.success){ $('#importDlg').dialog('close'); $('#importForm').form('clear'); $('#grid').datagrid('reload'); } }); } }); } } ] }); }
(3)测试导入数据
http://localhost:8080/erp/supplier.html?type=2
转载地址:https://code100.blog.csdn.net/article/details/111593678 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月14日 16时47分16秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
SpringFox接口文档API DOC
2019-04-29
netty优化策略
2019-04-29
架构师知识体系全景图
2019-04-29
guava中EventBus(事件总线)源码分析与使用
2019-04-29
程序员成神之路文章目录
2019-04-29
SASS软件的成熟度模型总结
2019-04-29
一次搞定redis使用
2019-04-29
最全架构设计实践方法论: 微服务
2019-04-29
Linux下简单几步安装AI开发环境-ROS(超有意思)
2019-04-29
epoll详解
2019-04-29
linux入门--磁盘管理之分区、格式化与挂载
2019-04-29
开发必备:HTTP 及 TLS
2019-04-29
如何设计自己的第一个加密交易机器人?
2019-04-29
TKDE 2020 | 综述:基于知识图谱的推荐系统
2019-04-29
休息时间!哪些业余活动能提升开发人员的技能?
2019-04-29
把人脑“森林”上传到计算机:人类脑计划只是一个科幻野心?
2019-04-29
智力测试:如何超越动物之智?
2019-04-29
事关人类生存?为什么要探寻AI系统的可解释性?
2019-04-29
安全工程师必知:常见Java漏洞有哪些?
2019-04-29
数据科学中的计量经济学技术
2019-04-29