action.java部分代码:
@Namespace(value="/excel")
@Scope("prototype")
@Action("uploadAction")
@InterceptorRefs(value = {@InterceptorRef("fileUploadStack")})
=============================================
List<List<String[]>> listlist = new ArrayList<List<String[]>>();
List<String[]> list = new ArrayList<String[]>();
public List<List<String[]>> getListList(){
return listlist;
}
public void setListList(List<List<String[]>> listlist){
this.listlist=listlist;
}
//下面的readExcel()就是把一张excel的数据最终存到了listlist中;
public static List<List<String[]>> readExcel(File excelFile,int rowNum) throws BiffException,
IOException {
// 创建一个list 用来存储读取的内容
Workbook rwb = null;
Cell cell = null;
// 创建输入流
InputStream stream = new FileInputStream(excelFile);
// 获取Excel文件对象
rwb = Workbook.getWorkbook(stream);
// 获取文件的指定工作表默认的第一个
Sheet sheet = rwb.getSheet(0);
// 行数(表头的目录不需要,从1开始)
for (int i = rowNum-1; i < sheet.getRows(); i++) {
// 创建一个数组用来存储每一列的值
String[] str = new String[sheet.getColumns()];
// 列数
for (int j = 0; j < sheet.getColumns(); j++) {
// 获取第i行,第j列的值
cell = sheet.getCell(j, i);
str[j] = cell.getContents();
}
// 把刚获取的列存入list
list.add(str);
}
listlist.add(list);
return listlist;
}
现在就是要把listlist的数据显示到jsp页面,该怎么写jsp呢?需要配置些什么吗?我试了网上很多方法都没搞定,
jsp的部分代码如下:
<%@ taglib prefix="s" uri="/struts-tags"%> (引用这句之前需要做什么吗)
<table fit="true" width="500" height="1024" border="1" cellpadding="1" cellspacing="1" >
<caption>
excel遍历!
</caption>
<s:form action="uploadAction" namespace="/excel">
<s:iterator id="id" value="#request.listlist" >
<td >
<s:property value="getListList()"/>
</td>
</s:iterator>
</s:form>
</table>
新手上路,技术不好,希望大家多多关照!!
------解决方案--------------------
把 String[]数组转换成list 然后用下面的格式读取出来。
<logic:iterate id="a" name="listlist">
<logic:iterate id="excelData" name="a">
<bean:write name="excelData" property="value0"/>
</logic:iterate>
</logic:iterate>
------解决方案--------------------
excelData就是实体类,value0就是参数,如果单单就一个值可以<bean:write name="excelData" />这样写。