struts1.x的例子,struts2.x可以参考自己修改
1.action的写法
import java.io.*;
import java.sql.*;
import java.util.arraylist;

import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.apache.poi.hssf.usermodel.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.formfile;
import org.apache.commons.beanutils.beanutils;


public class action
{

/**//*
* 把数据库中的字段导入到excel ,并生成excel文档
**/
public actionforward getdownload(actionmapping actionmapping,
actionform actionform, httpservletrequest request,

httpservletresponse response) throws exception
{
form fm = (form) actionform;
// excel 文件存放在服务器的相对路径下
string outputfile = request.getrealpath("/tmp/excel.xls");

try
{
// 创建新的excel 工作簿
hssfworkbook workbook = new hssfworkbook();
// 在excel 工作簿中建一工作表
hssfsheet sheet = workbook.createsheet("sheet1");
// 设置单元格格式(文本)
hssfcellstyle cellstyle = workbook.createcellstyle();
cellstyle.setdataformat(hssfdataformat.getbuiltinformat("@"));
// 在索引0的位置创建行(第一行)
hssfrow row = sheet.createrow((short) 0);
hssfcell cell1 = row.createcell((short) 0);// 第一列
hssfcell cell2 = row.createcell((short) 1);
hssfcell cell3 = row.createcell((short) 2);
// 定义单元格为字符串类型
cell1.setcelltype(hssfcell.cell_type_string);
cell2.setcelltype(hssfcell.cell_type_string);
cell3.setcelltype(hssfcell.cell_type_string);
cell1.setencoding(hssfcell.encoding_utf_16);
cell2.setencoding(hssfcell.encoding_utf_16);
cell3.setencoding(hssfcell.encoding_utf_16);
// 在单元格中输入数据
cell1.setcellvalue("姓名");
cell2.setcellvalue("性别");
cell3.setcellvalue("年龄");
connection connection = session.connection();
string sql = "select t.name, t.sex, t.age from table t where t.sex = ?";

try
{
preparedstatement ps = connection.preparestatement(sql);
ps.setstring(1, fm.getsex());// 传入查询条件
resultset rs = ps.executequery();// 查询结果存入rs
connection.commit();// 执行sql

while (rs.next())
{
//设置j行从第二行开始
int j = 1;
row = sheet.createrow((short) j);
//设置i列从第二列开始

for (int i = 1; i <= 3; i)
{
hssfcell cell = row.createcell((short) (i-1));
// 设置单元格格式
cell.setcellstyle(cellstyle);
cell.setcelltype(hssfcell.cell_type_string);
cell.setencoding(hssfcell.encoding_utf_16);
cell.setcellvalue(rs.getstring(i));
}
j;
}
request.setattribute("message", "文件生成成功!");

} catch (sqlexception e)
{
request.setattribute("message", "创建文件失败!");
e.printstacktrace();
}
// 删除路径下同名的excel 文件
file path = new file(outputfile);
path.delete();
// 新建一输出文件流
fileoutputstream fout = new fileoutputstream(outputfile);
// 把相应的excel 工作簿存盘
workbook.write(fout);
// 操作结束,关闭文件
fout.flush();
fout.close();
//该处如果excel过大会影响效率,谁有好的想法可以提出来参考(不过从页面下载完后就会清空)
request.getsession().setattribute("download", outputfile);

} catch (exception ioexception)
{
request.setattribute("message", "创建文件失败!");
return actionmapping.findforward("outjsp");
}
return actionmapping.findforward("outjsp");
}

/**//*
* 从excel文件中读取数据,并导入到数据库中
**/
public actionforward getupload(actionmapping actionmapping,
actionform actionform, httpservletrequest request,

httpservletresponse response) throws exception
{
// 获取excel 文件
form fm = (form) actionform;
formfile formfile = fm.getuploadfile();
inputstream inputstream = formfile.getinputstream();
fm.clear();// 清空
session session = hibernatesession.currentsession();
arraylist list = new arraylist();
int input = 0; //导入记数
string name = null;
string sex = null;
string age = null;

try
{
//通过得到的文件输入流inputstream创建一个hssfwordbook对象
hssfworkbook hssfworkbook = new hssfworkbook(inputstream);
hssfsheet hssfsheet = hssfworkbook.getsheetat(0);//第一个工作表
hssfrow hssfrow = hssfsheet.getrow(0);//第一行
//遍历该表格中所有的工作表,i表示工作表的数量 getnumberofsheets表示工作表的总数

for (int i = 0; i < hssfworkbook.getnumberofsheets(); i)
{
hssfsheet = hssfworkbook.getsheetat(i);
//遍历该行所有的行,j表示行数 getphysicalnumberofrows行的总数

for (int j = 1; j < hssfsheet.getphysicalnumberofrows(); j)
{
hssfrow = hssfsheet.getrow(j);
//判断是否还存在需要导入的数据

if (hssfrow == null)
{
system.out.println("这里已没有数据,在第"i"列,第"j"行");
break;
}

/** *//**将excel中的第 j 行,第一列的值插入到实例中*/

if (hssfrow.getcell((short) 0) == null)
{
name = "";

} else if (hssfrow.getcell((short) 0).getcelltype() == 0)
{
name = new double(hssfrow.getcell((short) 0).getnumericcellvalue()).tostring();
}
//如果excel表格中的数据类型为字符串型

else
{
name = hssfrow.getcell((short) 0).getstringcellvalue().trim();
}

/** *//**将excel中的第 j 行,第二列的值插入到实例中*/
//姓名

if(hssfrow.getcell((short) 1) == null)
{
sex = "";

} else if(hssfrow.getcell((short) 1).getcelltype() == 0)
{
sex = new double(hssfrow.getcell((short) 1).getnumericcellvalue()).tostring();
}
//如果excel表格中的数据类型为字符串型

else
{
sex = hssfrow.getcell((short) 1).getstringcellvalue().trim();
}

/** *//**将excel中的第 j 行,第三列的值插入到实例中*/
//姓名

if(hssfrow.getcell((short) 1) == null)
{
age = "";

} else if(hssfrow.getcell((short) 1).getcelltype() == 0)
{
age = new double(hssfrow.getcell((short) 1).getnumericcellvalue()).tostring();
}
//如果excel表格中的数据类型为字符串型

else
{
age = hssfrow.getcell((short) 1).getstringcellvalue().trim();
}
name = name.trim();
sex = sex.touppercase();

if (name.equals(""))
{
error.setname(name);
error.setmessage("姓名不能为空");
list.add(error);
continue;

} else
{
fm.setname(name);
fm.setsex(sex);
fm.setage(age);
session.save(fm);
}
//导入成功加1
input;
}
}
session.saveobjs(list.toarray());

} catch ()
{
}
}
}

2.form的写法
import org.apache.struts.action.actionform;
import org.apache.struts.upload.formfile;


public class form extends actionform
{
// 上传的文件
private formfile _flddo;

public void setuploadfile(formfile formfile)
{
_flddo = formfile;
}

public formfile getuploadfile()
{
return _flddo;
}

public void clear()
{
_flddo = null;
}
}



3.上传页面upload.jsp



<%
@ page contenttype="text/html; charset=gbk" language="java"%>

<%
@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

<html>
<html:form action="/action.do?method=getupload" method="post" enctype="multipart/form-data">
<html:file property="uploadfile" size="80%" />
<input type="button" value="导 入" onclick="upload(this.form)" class="buttongray">
html:form>
html>


<script language="javascript">
function upload(obj)


{
if(confirm("您现在选择的是xxx,您确定要导入吗?"))

{
var uploadfile = document.all.uploadfile.value;
if((null == uploadfile) ||( "" == uploadfile))

{
alert("上传文件没有指定!");
return false;
}
obj.action = '<html:rewrite page="/action.do?method=getupload"/>';
obj.submit();
}
}
script>


4.下载页面download.jsp



<%
@ page contenttype="text/html; charset=gbk"%>

<%
@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>

<%
@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>


<%
//获取下载文件
string download = (string) request.getsession().getattribute("download");
//清空文件
request.getsession().removeattribute("download");
%>

<html>
下传文件 <a href="<%=download %>" name="下载">下载a>
html>
