导入Excel:jsp中

 <form action="user_importTradingMoney" enctype="multipart/form-data" method="post">
<label>导入数据
<input type="file" name="tradingMoneyFile" value="选择文件">
<input type="submit" value="提交数据">
</label>
</form>

action中:

action最前面肯定要有下面这几句,而且要有其对应的set、get方法

private File tradingMoneyFile;// 实际上传文件
private String tradingMoneyFileContentType; // 文件的内容类型
private String tradingMoneyFileFileName; // 上传文件名

这是为了通过struts2自动获得上传的文件,当然struts.xml中肯定要有这几句:

 <constant name="struts.i18n.reload" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.devMode" value="true" />
<constant name=" struts.action.extension" value="action,," />
<constant name="struts.multipart.maxSize" value="102400000000000" />

文件大小什么的设定自己看着办。。。。

然后action中的importTradingMoney方法如下:

 //导入交易金额的Excel表
public String importTradingMoney(){
HttpServletRequest request = ServletActionContext.getRequest();
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
jxl.Workbook wb=null;
try {
//InputStream is=new FileInputStream(tradingMoneyFile);//读取存放数据的excel表格在电脑中的路径
InputStream is=new FileInputStream(tradingMoneyFile);
wb =Workbook.getWorkbook(is);//创建workbook对象,我的理解就是这个整个的excel
Sheet s=wb.getSheet(0);//创建sheet,其实就是excel中的每一页,其中的每一张表,他的下标是从0开始的//这里也就是取得第一张表
int rsRows=s.getRows();//得到所有的行
int rsColumns=s.getColumns();//得到所有的列
for(int i=1;i<rsRows;i++){
String userName=s.getCell(0, i).getContents();//(列,行)
int tradingMoney=Integer.parseInt(s.getCell(1, i).getContents().toString());
TradingMoney tradingMoneyClass=new TradingMoney();
tradingMoneyClass.setUserName(userName);
tradingMoneyClass.setTradingMoney(tradingMoney);
tradingMoneyClass.setYear(year);
tradingMoneyClass.setMonth(month);
userService.addTradingMoney(tradingMoneyClass);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<User> userList = null;
try {
userList = userService.selectUser(null, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("userList", userList);
return "selectUser";
}

至于excel的导出,我只是简单地把当前页面复制到了excel中:

 <%@page import="java.text.SimpleDateFormat"%>
<%
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
String filename = new String(("佣金明细-"+sf.format(new Date())).getBytes("utf8"),"ISO-8859-1");
response.setHeader("Content-disposition","attachment; filename="+filename+".xls");
%>

因为我要复制a.jsp页面的内容到excel中,而a.jsp中的内容又是便利list而出来,如果直接把上面这段代码加入到a.jsp中,那么a.jsp中的内容你就会看不到,这个页面一打开就会让你下载,所以就没有那种先预览再下载的效果了。。。。所以就在a.jsp中加了一个按钮

 <input type="button" value="结果导出为Excel" style="width:120px" class="button-action" onclick="toExcel(<s:property value='#request.owerUser.id' />,'${request.owerUser.userAccount }')"/>
 var userIdvar,userAccountvar;
function toExcel(userIdvar,userAccountvar){
//脚本运行后,将在新窗体newwindow中打开,宽为100,高为400,距屏顶0象素,屏左0象素,无工具条,无菜单条,无滚动条,不可调整大小,无地址栏,无状态栏。
window.open('reportUser_selectDetailReportUser?notice=1&userId='+userIdvar+'&userAccount='+userAccountvar,'newwindow','height=600,width=1200,top=80,left=80,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no');
};

就是重新发一遍请求到b.jsp中,其内容与a.jsp相同,所以这个b.jsp就不需要打开了。。。一切ok!

刚刚新增了不是简单地copy页面,而是从action导出excel表:

 //导出Excel表
public void exportForm(){
HttpServletRequest request = ServletActionContext.getRequest();
try {
request.setCharacterEncoding("utf8"); Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String sDate = sdf.format(now);
String fileName = new String(("用户-"+sDate).getBytes("utf8"),"ISO-8859-1");
String file = fileName+".xls"; HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition" ,"attachment;filename="+file+"");
OutputStream os = response.getOutputStream();
String[] title = {"姓名","账号","角色","电话","邮箱","发展人账号","备注"};
int i=0; WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("用户表", 0);
ws.setColumnView(0,15);
ws.setColumnView(1,15);
ws.setColumnView(2,20);
ws.setColumnView(3,15);
ws.setColumnView(4,18);
ws.setColumnView(5,15);
ws.setColumnView(6,35);
ws.setColumnView(7,15); for( i=0;i<title.length;i++){
WritableFont wf = new WritableFont(WritableFont.TIMES, 11, WritableFont.BOLD, false);
WritableCellFormat wcfF = new WritableCellFormat(wf);
Label labelCF = new Label(i, 0, title[i],wcfF);
ws.addCell(labelCF);
} i=1;
String userName = null;
String userAccount = null;
String userRole = null;
String userPhone = null;
String userEmail = null;
String higherAgentAccount = null;
String userNote = null; //根据selectedIds,生成一个List //List list = this.pageBean.getList(); List list = userService.selectUser(null, null);
Iterator it = list.iterator(); // 获得一个迭代子 while (it.hasNext()) {
User v = (User)it.next(); // 得到下一个元素 userName = v.getUserName();
Label labelC0 = new Label(0,i,userName);
ws.addCell(labelC0); userAccount = v.getUserAccount();
Label labelC1 = new Label(1,i,userAccount);
ws.addCell(labelC1); userRole = v.getRole().getRoleName();
Label labelC2 = new Label(2,i,userRole);
ws.addCell(labelC2); userPhone = v.getUserPhone();
Label labelC3 = new Label(3,i,userPhone);
ws.addCell(labelC3); userEmail = v.getUserEmail();
Label labelC4 = new Label(4,i,userEmail);
ws.addCell(labelC4); higherAgentAccount = v.getHigherAgentAccount();
Label labelC5 = new Label(5,i,higherAgentAccount);
ws.addCell(labelC5); userNote = v.getUserNote();
Label labelC6 = new Label(6,i,userNote);
ws.addCell(labelC6); i++;
}
//写入Exel工作表
os.flush();
wwb.write();
//关闭Excel工作薄对象
wwb.close();
os.close();
} catch (Exception e) { e.printStackTrace();
}
}
//如果想打开并写入一个已经存在的excel,只能通过副本操作
//Excel获得文件
Workbook wb2=Workbook.getWorkbook(excelFile);
//打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook wwb2= Workbook.createWorkbook(excelFile,wb2);
WritableSheet ws = wwb2.getSheet(0);

  

jsp中导入导出excel,ssh框架的更多相关文章

  1. 从SQL Server中导入/导出Excel的基本方法(转)

    从sql server中导入/导出 excel 的基本方法 /*=========== 导入/导出 excel 的基本方法 ===========*/ 从excel文档中,导入数据到sql数据库中,很 ...

  2. php中导入导出excel的原理

    在php中我们要经常导入导出excel文件,方便后台管理.那么php导入和导出excel的原理到底是什么呢?excel分为两大版本excel2007(后缀.xlsx).excel2003(后缀.xls ...

  3. Java中导入导出Excel -- POI技术

    一.介绍: 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实 ...

  4. .NET中导入导出Excel总结

    前一段时间,做了Excle的导入和导出,在此记录开发思路及技术要点,以便在今后开发中参考.                                                        ...

  5. .NET Core 中导入导出Excel

    操作Excel是一个比较常见的业务场景,本篇将使用EPPlus简单演示一个导入导出的示例. EPPlus开源地址:https://github.com/EPPlusSoftware/EPPlus 在项 ...

  6. ASP.NET Core 导入导出Excel xlsx 文件

    ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Li ...

  7. ASP.NET Core导入导出Excel文件

    ASP.NET Core导入导出Excel文件 希望在ASP.NET Core中导入导出Excel文件,在网上搜了一遍,基本都是使用EPPlus插件,EPPlus挺好用,但商用需要授权,各位码友若有好 ...

  8. C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序

    C#中缓存的使用   缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可:  <%@ Outp ...

  9. Vue框架下实现导入导出Excel、导出PDF

    项目需求:开发一套基于Vue框架的工程档案管理系统,用于工程项目资料的填写.编辑和归档,经调研需支持如下功能: Excel报表的导入.导出 PDF文件的导出 打印表格 经过技术选型,项目组一致决定通过 ...

随机推荐

  1. 基于AE的SimpleGIS框架的搭建

    ArcGIS是Esri公司集40余年地理信息系统(GIS)咨询和研发经验,奉献给用户的一套完整的GIS平台产品,具有强大的地图制作.空间数据管理.空间分析.空间信息整合.发布与共享的能力.本人主要就A ...

  2. 美洽SDK

    简介 GitHub地址:https://github.com/Meiqia/MeiqiaSDK-Android 开发文档:http://meiqia.com/docs/meiqia-android-s ...

  3. nginx添加缓存

    nginx的具体逻辑是什么样的? 分布式session spring session redis过滤器 有4种方案: 一直访问一台 //如果这台机器垮掉了,怎么办? session同步 序列化传输 / ...

  4. 一条insert语句批量插入多条记录

    一条insert语句批量插入多条记录 常见的insert语句,向数据库中,一条语句只能插入一条数据: insert into persons (id_p, lastname , firstName,  ...

  5. Java sql helper[转]

    原文:http://www.cnblogs.com/beijiguangyong/archive/2011/12/10/2302737.html package sql; import java.sq ...

  6. osgi与webservice

    osgi简介: http://osgia.com/ http://blog.csdn.net/xiaokui008/article/details/9662933 http://wdhdd889.it ...

  7. DHCP Relay 简介

    DHCP Relay(DHCPR)DHCP中继 也叫做DHCP中继代理 如果DHCP客户机与DHCP服务器在同一个物理网段,则客户机可以正确地获得动态分配的ip地址.如果不在同一个物理网段,则需要DH ...

  8. MySQL 慢查询配置

    MYSQL慢查询 1. 慢查询有什么用? 它能记录下所有执行超过long_query_time时间的SQL语句, 帮你找到执行慢的SQL, 方便我们对这些SQL进行优化. 2. 如何开启慢查询? 首先 ...

  9. 武汉科技大学ACM :1005: C语言程序设计教程(第三版)课后习题6.6

    Problem Description 打印出所有"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该本身. 例如:153是一个水仙花数,因为1 ...

  10. C++朝花夕拾【更新】

    C++拾遗 更新一些平时遇到的小细节: 1.关于类的无参构造函数和带有全部默认参考值的构造函数的区别 书上说的是带有全部默认值的构造函数就是无参构造函数,私以为不以为然,来看下边这个例子: #incl ...