您的位置:首页 > 其它

027医疗项目-模块二:药品目录的导入导出-导入功能的Action的编写

2016-11-28 15:10 603 查看
前一篇文章我们写了Service层,这篇文章我们写一下Action层。

实现的功能:



1:我们先下载模板:然后按照模板里面的规则,插入数据。比如存在d盘。

2:然后浏览找到那个文件,上传上去。
然后把excel中数据都插入到数据库。

---------------------------------------------------------------------------------------------------

我们之前在写导入功能的时候,已经写过了YpxxAction。我们继续在这个Ation中写。

思路:

我们要在Action中完成这么几个方法:

1:导入页面的显示方法:我们总是要有一个方法是能显示出导入功能页面的。

2:导入提交的方法:

将文件上传到服务器(实现文件的上传)

调用HSSF事件驱动封装类(Raed那个工具类),导入药品。

接下来我们具体讲一下怎么代码怎么实现:

首先我们要实现文件上传的话就要去用Springmvc的上传功能,在springmvc.xml中去配置。

如下:



我们之前写好了Service 我们在Action中要用这个Service的话,要先把这个类注入到Spring 容器中。所以我们修改applicationContext-base-service.xml代码:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

<!-- 用户管理 -->

<bean id="userService" class="yycg.bae.service.impl.userServiceimpl"/>
<bean id="systemConfigService" class="yycg.bae.service.impl.SystemConfigServiceImpl"/>
<bean id="YpxxService" class="yycg.business.service.impl.YpxxServiceImpl"/>

<!-- 这个是在药品导入数据库的action中注入的 -->
<bean id="HxlsOptRowsInterface" class="yycg.business.service.impl.YpxxImportServiceImpl"/>
</beans>


然后我们看Action的代码:

package yycg.business.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import yycg.base.pojo.po.Dictinfo;
import yycg.base.process.context.Config;
import yycg.base.process.result.ResultUtil;
import yycg.base.process.result.SubmitResultInfo;
import yycg.base.service.SystemConfigService;
import yycg.business.pojo.vo.YpxxCustom;
import yycg.business.pojo.vo.YpxxQueryVo;
import yycg.business.service.YpxxService;
import yycg.util.ExcelExportSXXSSF;
import yycg.util.HxlsOptRowsInterface;
import yycg.util.HxlsRead;
import yycg.util.UUIDBuild;
import yycg.util.Ypxx;

@Controller
@RequestMapping("/ypml")
public class YpxxAction {

@Autowired
YpxxService ypxxService;
@Autowired
private SystemConfigService systemConfigService;

@Autowired
private HxlsOptRowsInterface ypxximportserviceimpl;

//导出页面的展示

@RequestMapping("/exportYpxx")
public String exPortYpxx(Model model) throws Exception
{
/**
* 这些数据查出来后填充到查询页面上。
*/
List<Dictinfo> yplblist=systemConfigService.findDictinfoByType("001");
model.addAttribute("yplblist", yplblist);

List<Dictinfo> jyztlist=systemConfigService.findDictinfoByType("003");
model.addAttribute("jyztlist", jyztlist);

return "/business/ypml/exportYpxx";
}

//导出提交
@RequestMapping("/exportYpxxSubmit")
public @ResponseBody SubmitResultInfo exPortYpxxsubmit(YpxxQueryVo ypxxQueryVo)throws Exception
{
//查询到数据
List<YpxxCustom> list=ypxxService.findYpxxList(ypxxQueryVo);

/**            导出文件存放物理路径
* @param fileWebPath
*            导出文件web下载路径
* @param filePrefix
*            导出文件名的前缀
* @param flushRows
*            存放在内存的数据量
* @param fieldNames
*            导出文件列标题
* @param fieldCodes
*               导出数据对象的字段名称
* @param flushRows*/
//导出文件存放的路径,并且是虚拟目录指向的路径
String filePath = "d:/upload/linshi/";
//导出文件的前缀
String filePrefix="ypxx";
//-1表示关闭自动刷新,手动控制写磁盘的时机,其它数据表示多少数据在内存保存,超过的则写入磁盘
int flushRows=100;

//指导导出数据的title
List<String> fieldNames=new ArrayList<String>();
fieldNames.add("流水号");
fieldNames.add("通用名");
fieldNames.add("剂型");
fieldNames.add("规格");
fieldNames.add("转换系数 ");
fieldNames.add("生产企业");
fieldNames.add("商品名称");
fieldNames.add("中标价");
fieldNames.add("交易状态");

//告诉导出类数据list中对象的属性,让ExcelExportSXXSSF通过反射获取对象的值
List<String> fieldCodes=new ArrayList<String>();
fieldCodes.add("bm");//药品流水号
fieldCodes.add("mc");//通用名
fieldCodes.add("jx");
fieldCodes.add("gg");
fieldCodes.add("zhxs");
fieldCodes.add("scqymc");
fieldCodes.add("spmc");
fieldCodes.add("zbjg");
fieldCodes.add("jyztmc");

//注意:fieldCodes和fieldNames个数必须相同且属性和title顺序一一对应,这样title和内容才一一对应

//开始导出,执行一些workbook及sheet等对象的初始创建,以及表格建在哪里
//"/upload/"本来是服务器的磁盘目录,但是我们是一台机器做开发,所以见建立一个虚拟目录。
ExcelExportSXXSSF excelExportSXXSSF = ExcelExportSXXSSF.start(filePath, "/upload/", filePrefix, fieldNames, fieldCodes, flushRows);

//准备导出的数据,将数据存入list,且list中对象的字段名称必须是刚才传入ExcelExportSXXSSF的名称

//执行导出,把数据导入到excel表
excelExportSXXSSF.writeDatasByObject(list);

/*
* new Object[]{list.size(),excelExportSXXSSF.exportFile()}参数:一共导出的数据数量,.exportFile()导出文件。
*/
return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 313, new Object[]{list.size(),excelExportSXXSSF.exportFile()}));

}
//载入导入页面
@RequestMapping("/importypxx")
public String importypxx()throws Exception
{

return "/business/ypml/importypxx";
}
//提交按钮(导入数据库)
@RequestMapping("/importypxxsubmit")
public @ResponseBody SubmitResultInfo importypxxsubmit(@RequestParam MultipartFile ypxximportfile) throws Exception
{

String fileName_OriginalString=ypxximportfile.getOriginalFilename();
String fileName_new=UUIDBuild.getUUID()+fileName_OriginalString.substring(fileName_OriginalString.indexOf("."));
File targetFile=new File("d:/upload/"+fileName_new);
//如果这个路径不存在就创建这个路径
if(!targetFile.exists())
{

targetFile.mkdirs();

}
//将文件导入到磁盘
ypxximportfile.transferTo(targetFile);

//得到物理的路径

String filepath=targetFile.getAbsolutePath();
//将导入的service对象设置到HxlsRead中
HxlsRead hxlsRead=new HxlsRead(filepath, 1, ypxximportserviceimpl);
hxlsRead.process();

return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 906, null));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐