关于SpringMVC上传与导出excel问题
2015-06-26 16:19
363 查看
导入excel方法
1.首先在前端JSP中:
<input type="button" value="Excel导入" class="btn_bg"
onclick="openPage('<c:url value="/person.do?action=showImport"/>','Excel导入',600,400);" />
2.写import.jsp
<script type="text/javascript">
function checkInfo() {
if ($("#excelFile").val() == "") {
alert("请选择要上传的Excel文件!");
return;
}
var importName = $("#excelFile").val();
var fileSuffix = importName.substring(importName.lastIndexOf(".") + 1)
.toLowerCase();
if (fileSuffix != "xls" && fileSuffix!= "xlsx") {
alert("文件类型不正确,只能上传xls类型文件或者xlsx的文件");
return;
}
if (confirm("请确认Excel列名符合导入规则,确定导入?")) {
document.getElementById('saveBtn').disabled=true//不可用
document.treeForm.submit();
}
}
window.onload = function() {
var importTle = "${message}";
if (importTle != "") {
flushYmtParentPage();
}
}
function selectOrgId(id, name) {
$("#deptName").val(name);
$("#deptId").val(id);
closes();
}
function doclose() {
window.close();
}
</script>
</head>
<body>
<div class="main_nav">
<div class="lb_nav">
<div class="lb_nav_tu">
EXCEL批量增加
</div>
</div>
<form action="<c:url value='/person.do?action=doImport'/>"
method="post" id="treeForm" name="treeForm"
enctype="multipart/form-data">
<div class="lb_nav_bg1">
<div class="cx_nav">
<table cellpadding="0" cellspacing="0" class="cx_table">
<tr>
<td colspan="2" class="info1">
请选择需要导入的EXCEL文件
</td>
</tr>
<tr>
<th class="td_l" style="width: 30%">
选择文件:
</th>
<td class="td_r">
<input class="input_field_300" style="width:80%;" type="file" name="excelFile"
id="excelFile" title="选择要导入的excel文件"/>
</td>
</tr>
<c:if test="${message != null}">
<script>
flushParentPage();
</script>
<tr>
<th class="td_l">
导入结果:
</th>
<td class="td_r">
<textarea class="cl_textarea_small" readonly="readonly">${message }</textarea>
</td>
</tr>
</c:if>
<tr>
<td colspan="2" class="info2">
<p>注意:</p>
<p style="text-indent: 2em;">1:系统只导入Excel列头和这个列表表头一致的列,其余列将忽略。这里可用的列名如下:用户名、密码、姓名、性别、部门、电话、邮件
</p><p style="text-indent: 2em;">2:系统将从EXCEL文件的第二行开始提取数据</p>
</td>
</tr>
</table>
<div class="btn_nav_dk">
<input type="button" value="导 入" id="saveBtn" class="btn_bg"
onclick="checkInfo();" title="开始导入文件"/>
<input type="reset" value="重 置" class="btn_bg" title="重置导入信息"/>
<input type="button" value="关 闭" class="btn_bg" title="退出该页面"
onclick="doclose();" />
</div>
</div>
</div>
</form>
</div>
</body>
<script type="text/javascript">
/*树选择之后专用*/
function selectOrgId(id,name) {
if(id==1){
alert("不能选择默认的最高级别的组");
return;
}
$("#parentCodeName").val(name);
$("#villageId").val(id);
closes();
}
</script>
3.在controller中书写跳转网页
/**显示导入文件
*
*
*/
@RequestMapping(params="action=showImport")
public String showImport(Model model){
return "person/import";
}
4.执行导入
/**执行导入
*
*
*/
@RequestMapping(params = "action=doImport")
public String doUploadContactPerson(Model model, HttpServletRequest request,
@RequestParam(value = "personId", required = false) Long personId ) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile uploadFile = multipartRequest.getFile("excelFile");
ExcelUtil excelUtil = new ExcelUtil();
List<Person> personList = null;
try {
// personList = excelUtil.getList(Villager.class,
// uploadFile.getInputStream(),0,1,1);
personList = excelUtil.getList(Person.class, uploadFile
.getInputStream());
Object pertsonId;
//分配组
if(personId!=null && personList!=null && personList.size()>0){
for(Person v :personList ){
v.setPersonId(personId);
}
}
personService.insertBatch(personList);
model.addAttribute("message", "导入成功");
} catch (Exception e) {
logger.error("导入失败,出错原因:" + e);
model.addAttribute("message", "失败," + e.toString());
}
return "person/import";
}
5.在service中提供服务
/**
* 批量处理
*
* @param list
* @throws Exception
*/
public void insertBatch(List<Person> list) {
try {
int i = 1;
super.getSqlMapClient().startBatch();
for (Person o : list) {
super.insert(o);
i++;
if (i % 100 == 0)
super.getSqlMapClient().executeBatch();
}
super.getSqlMapClient().executeBatch();
} catch (Exception e) {
logger.error("批量出错" + this.getClass() + e);
}
以上为excel导入内容,,,
二::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
导出excel
1.前 端JSP书写
<input type="button" value="导出EXCEL" class="btn_bg" onclick="exportExcel();"/>
在js中
function exportExcel() {
document.queryForm.setAttribute("action", "<c:url value='/person.do?action=doExportDetail'/>");
document.queryForm.submit();
}
2.在controller中导入
private Export export;
/**导出excel
*
*
*/
@RequestMapping(params = "action=doExportDetail")
public void doExportDetailFee(@ModelAttribute("o")
Person o, HttpServletResponse response) {
List<List<String>> details = personService.queryDetailFeeList(o);
List<String> titles = new ArrayList<String>();
titles.add("用户名");
titles.add("密 码");
titles.add("姓 名");
titles.add("性 别");
titles.add("部 门");
titles.add("电 话");
titles.add("邮 件");
export.export("个人信息报表", titles, details, "个人信息报表.xls", response);
details = null;
titles = null;
Runtime.getRuntime().gc();
}
3.在service中
public List<List<String>> queryDetailFeeList(Person o) {
StringBuffer sb = new StringBuffer();
// sb.append(" select feeOne.fee,tz.zzName as orgName,tz.zzCode as orgCode,feeOne.feeTime from ");
// sb.append(" (select tf.orgId,tf.tfMoney as fee,tf.jfTime as feeTime from t_tfgl tf where 1=1 ");
// sb.append(SqlMakerUtil.popuBeginDateMysql("tf", "jfTime", o.getBeginTime()));
// sb.append(SqlMakerUtil.popuEndDateMysql("tf", "jfTime", o.getEndTime()));
// sb.append(" ) as feeOne ");
// sb.append(" left join t_tzz tz on feeOne.orgId = tz.id where 1=1 ");
// sb.append("SELECT tp.`userName`,tp.`passWord`,tp.`readName`,tp.`sex`,tp.`department`,");
// sb.append("tp.`telephone`,tp.`email`FROM t_person tp ");
// sb.append(" WHERE 1=1");
//以上为查询语句。。。。。。。。
sb.append("select tp.*from t_person tp where 1=1");
List<Person> personList = super.find(sb.toString());
List<List<String>> list = new ArrayList<List<String>>();
List<String> detailList = null;
for (Person vo : personList) {
detailList = new ArrayList<String>();
detailList.add(vo.getUserName());
detailList.add(vo.getPassWord());
detailList.add(vo.getReadName());
detailList.add(vo.getSex());
detailList.add(vo.getDepartment());
detailList.add(vo.getTelephone());
detailList.add(vo.getEmail());
list.add(detailList);
}
return list;
}
完成
1.首先在前端JSP中:
<input type="button" value="Excel导入" class="btn_bg"
onclick="openPage('<c:url value="/person.do?action=showImport"/>','Excel导入',600,400);" />
2.写import.jsp
<script type="text/javascript">
function checkInfo() {
if ($("#excelFile").val() == "") {
alert("请选择要上传的Excel文件!");
return;
}
var importName = $("#excelFile").val();
var fileSuffix = importName.substring(importName.lastIndexOf(".") + 1)
.toLowerCase();
if (fileSuffix != "xls" && fileSuffix!= "xlsx") {
alert("文件类型不正确,只能上传xls类型文件或者xlsx的文件");
return;
}
if (confirm("请确认Excel列名符合导入规则,确定导入?")) {
document.getElementById('saveBtn').disabled=true//不可用
document.treeForm.submit();
}
}
window.onload = function() {
var importTle = "${message}";
if (importTle != "") {
flushYmtParentPage();
}
}
function selectOrgId(id, name) {
$("#deptName").val(name);
$("#deptId").val(id);
closes();
}
function doclose() {
window.close();
}
</script>
</head>
<body>
<div class="main_nav">
<div class="lb_nav">
<div class="lb_nav_tu">
EXCEL批量增加
</div>
</div>
<form action="<c:url value='/person.do?action=doImport'/>"
method="post" id="treeForm" name="treeForm"
enctype="multipart/form-data">
<div class="lb_nav_bg1">
<div class="cx_nav">
<table cellpadding="0" cellspacing="0" class="cx_table">
<tr>
<td colspan="2" class="info1">
请选择需要导入的EXCEL文件
</td>
</tr>
<tr>
<th class="td_l" style="width: 30%">
选择文件:
</th>
<td class="td_r">
<input class="input_field_300" style="width:80%;" type="file" name="excelFile"
id="excelFile" title="选择要导入的excel文件"/>
</td>
</tr>
<c:if test="${message != null}">
<script>
flushParentPage();
</script>
<tr>
<th class="td_l">
导入结果:
</th>
<td class="td_r">
<textarea class="cl_textarea_small" readonly="readonly">${message }</textarea>
</td>
</tr>
</c:if>
<tr>
<td colspan="2" class="info2">
<p>注意:</p>
<p style="text-indent: 2em;">1:系统只导入Excel列头和这个列表表头一致的列,其余列将忽略。这里可用的列名如下:用户名、密码、姓名、性别、部门、电话、邮件
</p><p style="text-indent: 2em;">2:系统将从EXCEL文件的第二行开始提取数据</p>
</td>
</tr>
</table>
<div class="btn_nav_dk">
<input type="button" value="导 入" id="saveBtn" class="btn_bg"
onclick="checkInfo();" title="开始导入文件"/>
<input type="reset" value="重 置" class="btn_bg" title="重置导入信息"/>
<input type="button" value="关 闭" class="btn_bg" title="退出该页面"
onclick="doclose();" />
</div>
</div>
</div>
</form>
</div>
</body>
<script type="text/javascript">
/*树选择之后专用*/
function selectOrgId(id,name) {
if(id==1){
alert("不能选择默认的最高级别的组");
return;
}
$("#parentCodeName").val(name);
$("#villageId").val(id);
closes();
}
</script>
3.在controller中书写跳转网页
/**显示导入文件
*
*
*/
@RequestMapping(params="action=showImport")
public String showImport(Model model){
return "person/import";
}
4.执行导入
/**执行导入
*
*
*/
@RequestMapping(params = "action=doImport")
public String doUploadContactPerson(Model model, HttpServletRequest request,
@RequestParam(value = "personId", required = false) Long personId ) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile uploadFile = multipartRequest.getFile("excelFile");
ExcelUtil excelUtil = new ExcelUtil();
List<Person> personList = null;
try {
// personList = excelUtil.getList(Villager.class,
// uploadFile.getInputStream(),0,1,1);
personList = excelUtil.getList(Person.class, uploadFile
.getInputStream());
Object pertsonId;
//分配组
if(personId!=null && personList!=null && personList.size()>0){
for(Person v :personList ){
v.setPersonId(personId);
}
}
personService.insertBatch(personList);
model.addAttribute("message", "导入成功");
} catch (Exception e) {
logger.error("导入失败,出错原因:" + e);
model.addAttribute("message", "失败," + e.toString());
}
return "person/import";
}
5.在service中提供服务
/**
* 批量处理
*
* @param list
* @throws Exception
*/
public void insertBatch(List<Person> list) {
try {
int i = 1;
super.getSqlMapClient().startBatch();
for (Person o : list) {
super.insert(o);
i++;
if (i % 100 == 0)
super.getSqlMapClient().executeBatch();
}
super.getSqlMapClient().executeBatch();
} catch (Exception e) {
logger.error("批量出错" + this.getClass() + e);
}
以上为excel导入内容,,,
二::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
导出excel
1.前 端JSP书写
<input type="button" value="导出EXCEL" class="btn_bg" onclick="exportExcel();"/>
在js中
function exportExcel() {
document.queryForm.setAttribute("action", "<c:url value='/person.do?action=doExportDetail'/>");
document.queryForm.submit();
}
2.在controller中导入
private Export export;
/**导出excel
*
*
*/
@RequestMapping(params = "action=doExportDetail")
public void doExportDetailFee(@ModelAttribute("o")
Person o, HttpServletResponse response) {
List<List<String>> details = personService.queryDetailFeeList(o);
List<String> titles = new ArrayList<String>();
titles.add("用户名");
titles.add("密 码");
titles.add("姓 名");
titles.add("性 别");
titles.add("部 门");
titles.add("电 话");
titles.add("邮 件");
export.export("个人信息报表", titles, details, "个人信息报表.xls", response);
details = null;
titles = null;
Runtime.getRuntime().gc();
}
3.在service中
public List<List<String>> queryDetailFeeList(Person o) {
StringBuffer sb = new StringBuffer();
// sb.append(" select feeOne.fee,tz.zzName as orgName,tz.zzCode as orgCode,feeOne.feeTime from ");
// sb.append(" (select tf.orgId,tf.tfMoney as fee,tf.jfTime as feeTime from t_tfgl tf where 1=1 ");
// sb.append(SqlMakerUtil.popuBeginDateMysql("tf", "jfTime", o.getBeginTime()));
// sb.append(SqlMakerUtil.popuEndDateMysql("tf", "jfTime", o.getEndTime()));
// sb.append(" ) as feeOne ");
// sb.append(" left join t_tzz tz on feeOne.orgId = tz.id where 1=1 ");
// sb.append("SELECT tp.`userName`,tp.`passWord`,tp.`readName`,tp.`sex`,tp.`department`,");
// sb.append("tp.`telephone`,tp.`email`FROM t_person tp ");
// sb.append(" WHERE 1=1");
//以上为查询语句。。。。。。。。
sb.append("select tp.*from t_person tp where 1=1");
List<Person> personList = super.find(sb.toString());
List<List<String>> list = new ArrayList<List<String>>();
List<String> detailList = null;
for (Person vo : personList) {
detailList = new ArrayList<String>();
detailList.add(vo.getUserName());
detailList.add(vo.getPassWord());
detailList.add(vo.getReadName());
detailList.add(vo.getSex());
detailList.add(vo.getDepartment());
detailList.add(vo.getTelephone());
detailList.add(vo.getEmail());
list.add(detailList);
}
return list;
}
完成
相关文章推荐
- 使用Python生成Excel格式的图片
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- excel vba 限制工作表的滚动区域代码
- excel vba 高亮显示当前行代码
- SQL 导入导出Excel数据的语句
- 文本、Excel、Access数据导入SQL Server2000的方法
- C#导出数据到Excel文件的方法
- Vbscript生成Excel报表的常用操作总结
- C#基于NPOI生成具有精确列宽行高的Excel文件的方法
- 总提示[Microsoft][ODBC Excel Driver] 数值字段溢出官方解决方法
- C#将Sql数据保存到Excel文件中的方法
- VC6.0实现读取Excel数据的方法
- 把excel表格里的数据导入sql数据库的两种方法
- access dbase excel foxpro 数据库浏览器V3.1版 下载
- C#实现把txt文本数据快速读取到excel中
- C#自定义导出数据到Excel的类实例
- 使用PHPExcel操作Excel用法实例分析
- C#实现Excel导入sqlite的方法
- C#利用Openxml读取Excel数据实例
- C#使用Ado.net读取Excel表的方法