- 浏览: 340276 次
- 性别:
- 来自: 沈阳
文章分类
最新评论
-
haiw:
谢谢分享
Oracle 的递归查询(树型查询) -
nomandia:
除非是通过open打开的窗口,否则没法close的
JS 关闭当前页面 -
c30989239:
注意 SimpleDateFormat 是非线程安全的
Java 获取网络时间并在jsp中显示 -
归来朝歌:
不错,以后可能用得上
Java 操作Excel -
luhantu:
不错!学习了
Java 操作Excel
除Struts2必须的jar包外,还需要jar包:poi-3.7-beta-20100620.jar
首先是jsp代码实现上传:
<body>
<s:form id="form1" name="form1" action="roleFileUpload.action" method="post" enctype="multipart/form-data" theme="simple">
<table width="80%" border="1" align="center" class="DB_table">
<tr>
<td colspan="99" align="left">文件上传</td>
</tr>
<tr>
<td colspan="99" id="more">
<s:file name="uploadFile" label="文件位置" size="80"/>
</td>
</tr>
<tr>
<td colspan="99" align="right">
<s:submit value="上传"></s:submit>
<s:reset value="重置"></s:reset>
</td>
</tr>
</table>
[/align]
</s:form>
</body>
其次是Struts.xml配置文件:
<action name="roleFileUpload" class="roleFileAction" method="loadRoleFile">
<result name="success">/page/role/roleallinput.jsp</result>
<result name="input">/page/role/roleallinput.jsp</result>
</action>
然后是Action类实现:
public class RoleFileUploadAction extends ActionSupport{
private File uploadFile;
private String uploadFileFileName;
private FileLoadDao fileLoadDao;
private RoleDao roleDao;
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public FileLoadDao getFileLoadDao() {
return fileLoadDao;
}
public void setFileLoadDao(FileLoadDao fileLoadDao) {
this.fileLoadDao = fileLoadDao;
}
public RoleDao getRoleDao() {
return roleDao;
}
public void setRoleDao(RoleDao roleDao) {
this.roleDao = roleDao;
}
@Override
public void validate() {
super.validate();
}
public String loadRoleFile(){
String directory = "/upload";
String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
//生成上传的文件对象
File target = new File(targetDirectory,uploadFileFileName);
//如果文件已经存在,则删除原有文件
if(target.exists()){
target.delete();
}
//复制file对象,实现上传
try {
FileUtils.copyFile(uploadFile, target);
} catch (IOException e) {
e.printStackTrace();
}
loadRoleInfo();
return SUCCESS;
}
/**
* 把Excele表读出的数据,组装成一个List,统一导入数据库
* @param uploadFileFileName
*/
public void loadRoleInfo(){
String directory = "/upload";
String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
File target = new File(targetDirectory,uploadFileFileName);
List roleList = new ArrayList();
try{
FileInputStream fi = new FileInputStream(target);
Workbook wb = new HSSFWorkbook(fi);
Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum()+1;
for(int i=1;i<rowNum;i++){
PtRoleInfo ptRoleInfo = new PtRoleInfo();
Row row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for(int j=0;j<cellNum;j++){
Cell cell = row.getCell(j);
String cellValue = null;
switch(cell.getCellType()){ //判断excel单元格内容的格式,并对其进行转换,以便插入数据库
case 0 : cellValue = String.valueOf((int)cell.getNumericCellValue()); break;
case 1 : cellValue = cell.getStringCellValue(); break;
case 2 : cellValue = String.valueOf(cell.getDateCellValue()); break;
case 3 : cellValue = ""; break;
case 4 : cellValue = String.valueOf(cell.getBooleanCellValue()); break;
case 5 : cellValue = String.valueOf(cell.getErrorCellValue()); break;
}
switch(j){//通过列数来判断对应插如的字段
case 0 : ptRoleInfo.setRoleId(cellValue);break;
case 1 : ptRoleInfo.setRoleName(cellValue);break;
case 2 : ptRoleInfo.setDeil(cellValue);break;
}
}
roleList.add(ptRoleInfo);
}
for(PtRoleInfo pt:roleList){
fileLoadDao.roleInfotoDB(pt);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
最后在roleInfoToDB方法中需要使用‘事务Transaction’来进行批量插入,确保插入失败的能完全回滚。这样Excel文件的读取以及数据库的写入就实现了
首先是jsp代码实现上传:
<body>
<s:form id="form1" name="form1" action="roleFileUpload.action" method="post" enctype="multipart/form-data" theme="simple">
<table width="80%" border="1" align="center" class="DB_table">
<tr>
<td colspan="99" align="left">文件上传</td>
</tr>
<tr>
<td colspan="99" id="more">
<s:file name="uploadFile" label="文件位置" size="80"/>
</td>
</tr>
<tr>
<td colspan="99" align="right">
<s:submit value="上传"></s:submit>
<s:reset value="重置"></s:reset>
</td>
</tr>
</table>
[/align]
</s:form>
</body>
其次是Struts.xml配置文件:
<action name="roleFileUpload" class="roleFileAction" method="loadRoleFile">
<result name="success">/page/role/roleallinput.jsp</result>
<result name="input">/page/role/roleallinput.jsp</result>
</action>
然后是Action类实现:
public class RoleFileUploadAction extends ActionSupport{
private File uploadFile;
private String uploadFileFileName;
private FileLoadDao fileLoadDao;
private RoleDao roleDao;
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public FileLoadDao getFileLoadDao() {
return fileLoadDao;
}
public void setFileLoadDao(FileLoadDao fileLoadDao) {
this.fileLoadDao = fileLoadDao;
}
public RoleDao getRoleDao() {
return roleDao;
}
public void setRoleDao(RoleDao roleDao) {
this.roleDao = roleDao;
}
@Override
public void validate() {
super.validate();
}
public String loadRoleFile(){
String directory = "/upload";
String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
//生成上传的文件对象
File target = new File(targetDirectory,uploadFileFileName);
//如果文件已经存在,则删除原有文件
if(target.exists()){
target.delete();
}
//复制file对象,实现上传
try {
FileUtils.copyFile(uploadFile, target);
} catch (IOException e) {
e.printStackTrace();
}
loadRoleInfo();
return SUCCESS;
}
/**
* 把Excele表读出的数据,组装成一个List,统一导入数据库
* @param uploadFileFileName
*/
public void loadRoleInfo(){
String directory = "/upload";
String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
File target = new File(targetDirectory,uploadFileFileName);
List roleList = new ArrayList();
try{
FileInputStream fi = new FileInputStream(target);
Workbook wb = new HSSFWorkbook(fi);
Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum()+1;
for(int i=1;i<rowNum;i++){
PtRoleInfo ptRoleInfo = new PtRoleInfo();
Row row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for(int j=0;j<cellNum;j++){
Cell cell = row.getCell(j);
String cellValue = null;
switch(cell.getCellType()){ //判断excel单元格内容的格式,并对其进行转换,以便插入数据库
case 0 : cellValue = String.valueOf((int)cell.getNumericCellValue()); break;
case 1 : cellValue = cell.getStringCellValue(); break;
case 2 : cellValue = String.valueOf(cell.getDateCellValue()); break;
case 3 : cellValue = ""; break;
case 4 : cellValue = String.valueOf(cell.getBooleanCellValue()); break;
case 5 : cellValue = String.valueOf(cell.getErrorCellValue()); break;
}
switch(j){//通过列数来判断对应插如的字段
case 0 : ptRoleInfo.setRoleId(cellValue);break;
case 1 : ptRoleInfo.setRoleName(cellValue);break;
case 2 : ptRoleInfo.setDeil(cellValue);break;
}
}
roleList.add(ptRoleInfo);
}
for(PtRoleInfo pt:roleList){
fileLoadDao.roleInfotoDB(pt);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
最后在roleInfoToDB方法中需要使用‘事务Transaction’来进行批量插入,确保插入失败的能完全回滚。这样Excel文件的读取以及数据库的写入就实现了
发表评论
-
springmvc整合cxf webservice
2016-03-15 16:54 1248springmvc中整合cxf webservice。 ... -
JSTL fn函数中字符串拼接
2015-11-30 11:35 5168关于JSTL的标签,在网上查了很久,都是介绍fn ... -
Java 获取网络时间并在jsp中显示
2015-09-07 14:15 1833开发中经常会遇到需要将服务器时间或者网络时间显示在 ... -
记录--ReflectionUtil
2015-07-22 10:51 788import java.lang.reflect.Field; ... -
JSTL foreach及if when标签使用
2015-07-22 08:48 2047需要在jsp中加入以下标签库和函数库 <%@ ta ... -
Java 获取服务器IP和本地Ip
2015-07-21 21:39 8606在项目中经常会遇到需要获取服务器的IP和本地IP, ... -
Mybatis 模糊查询
2015-06-11 18:42 617参数中直接加入%% param.setUsernam ... -
MyBatis之增加删除修改
2015-06-11 16:32 1652insert、update、delete这三个元素分别用于执行 ... -
MyBatis 传入参数parameterType详解
2015-06-11 16:29 18728在MyBatis的select、insert、update ... -
Java 操作Excel
2015-06-10 20:54 2266前不久做过Excel的导入导出功能,其主要的难点是java如何 ... -
SpringMVC+mybatis 实现easyui中tree
2015-06-08 22:08 5269最近做项目用到了前端框架easyUI,以下是easyUI ... -
MD5加密工具类
2015-06-03 18:47 1313package base; /** * MD5加密算法 * ... -
spring+mybatis配置
2015-02-08 17:04 8521.用spring配置mybatis <?xml ver ... -
JAVA 处理JSON工具类
2014-12-31 13:49 1563以下代码为Java处理json数据的工具类,以备后用 pac ... -
Java解析及读取Json数据
2014-12-31 13:46 18331.JSON介绍 JSON比XML简单,主要体现在传输相 ... -
新版SSH整合(备用)
2014-09-01 19:40 1603在网上找的S4S2H4架构实现,现记录一下,以备后用。 附件中 ... -
jsp静态化和伪静态化
2014-06-12 16:21 798首先说说为什么要静态化。 对于现在的Web Applicat ... -
JSP自定义标签的创建和使用
2014-04-18 10:45 1228摘自http://jzinfo.iteye.com/blog/ ... -
ssh项目中strust2从2.0.11升级到2.3.15.1详细步骤
2014-03-28 15:33 2201将ssh项目中strust2从2.0.11升级到2.3.15. ... -
MyEclipse下创建的项目 导入eclipse(转载)
2014-03-19 17:15 773FROM:http://www.cnblogs.com/zho ...
相关推荐
运用struts2将数据库中的数据导入和导出,同时会用到poi的jar包
struts1 poi Excel批量导入支持xls和xlsx-源码java 有需要的能帮助到
Struts2+Excel导入功能 完整美运行有JAR包
程序使用Struts2(SSH即Struts2+Hibernate+Spring)+POI+MySQL实现Excel动态数据导入和导出,程序源码文件附带运行说明
poi struts2 Excel导入数据库 用jdbc连的sql server2000 非常简单 一看就能明白怎么个回事 就一张表 3个字段 适合第一次用poi的朋友
java struts 把数据导入 excel java struts 把数据导入 excel java struts 把数据导入 excel
本文档详细介绍了struts2整合apache的jxl导入导出excel表格的方法。包括代码,可以直接使用。
首先说明这不是个项目不能直接运行,需要自己集成到自己项目中,文件包括 struts2 Excel的导入和导出。
Struts2 从Oracle导出数据到Excel,建表语句,存储过程语句,都有
struts2+poi实现excel文件的导出,虽然有很多jxl库实现的方案,而本方案是使用struts2 mvc, apache poi库实现excel的导入导出,文档很详细
struts导入excel 并通过jxl对excel解析,以及模板下载
struts+hibernate实现excel导入数据库 注意:Excel导入是指读服务器端的数据,也就是说,如果想用客户端的Excel是需要把Excel文件先上传到服务器上的,然后再导入,我也是前几天才发现的。
项目内有个excel 拿出来 在index页面点击选择导入直接运行
Struts2导入实例下载,内含Excel数据
// 在Excel 工作簿中建一工作表 HSSFSheet sheet = workbook.createSheet("Sheet1"); // 设置单元格格式(文本) HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat...
1.本动态导入导出Excel工程导入Eclipse即可用行,支持所有版本的Excel导入导出。...2.程序使用Struts2+POI+SQLSever(SSH即Struts2+Hibernate+Spring)实现Excel动态数据导入和导出,压缩包内含程序源码文件。
poi+struts的excel导入action,加入包,配置文件就可以用
struts 文件上传 Struts实现文件上传
OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据库文件) 学习笔记(含源代码) 借鉴 风中叶 老师的视频,写的文章,代码比较详实。 说了很多我的看法,和思考,做了充分的日志
struts2 把数据导入到EXCEL表。简单实例