目录结构:
Data.xls数据:
后台页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//System.out.println(this.getServletContext().getRealPath ("/"));
try {
Workbook wb = Workbook.getWorkbook(
new File( this .getServletContext().getRealPath ( "/" )+ "data.xls" ));
System.out.println( "2222222" );
List<Data> data = new ArrayList<Data>();
for ( int i= 1 ;i<wb.getSheet( 0 ).getRows();i++){
Cell[] cell = wb.getSheet( 0 ).getRow(i);
data.add( new Data(cell[ 0 ].getContents(),cell[ 1 ].getContents(),cell[ 2 ].getContents()));
System.out.println(cell[ 1 ].getContents());
}
wb.close();
request.setAttribute( "data" , data);
request.getRequestDispatcher( "../index.jsp" ).forward(request, response);
} catch (Exception e){
e.printStackTrace();
}
}
|
Data.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
public class Data {
private String id;
private String name;
private String password;
public Data() {
super ();
// TODO Auto-generated constructor stub
}
public Data(String id, String name, String password) {
super ();
this .id = id;
this .name = name;
this .password = password;
}
public String getId() {
return id;
}
|
前台页面:
Index.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<body>
<form action= "servlet/getExcelData" method= "post" >
<input type= "submit" id= "tijiao" value= "submit" >
</form>
<%
List<Data> data = new ArrayList<Data>();
data=(List<Data>)request.getAttribute( "data" );
if (data!= null &&data.size()> 0 )
for (Data d:data){
out.println(d.getId()+ "---" +d.getName()+ "---" +d.getPassword());
}
%>
</body>
|
运行结果:
以上这篇Java web的读取Excel简单实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。