jxl操作excel包括对象Workbook,Sheet ,Cell。
一个excel就对应一个Workbook对象,
一个Workbook可以有多个Sheet对象
一个Sheet对象可以有多个Cell对象
读取excel操作
通过Workbook,Sheet ,Cell这三个对象我们就可以实现Excel文件的读取工作。
步骤:
1、 选取Excel文件得到工作薄
2、 选择工作表
3、 选择Cell
4、 读取信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="uploadFile" method="post" enctype="multipart/form-data">
<input type="file" name="upfile"/>
<input type="submit" value="上传"/>
</form>
<a href="classes.jsp">导出</a>
</body>
</html>
导入数据代码
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.Part;
import java.io.IOException;
@WebServlet("/uploadFile")
@MultipartConfig
public class UploadFileServlet extends HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, IOException {
request.setCharacterEncoding("utf-8");
//1.获取文件部分part
Part part = request.getPart("upfile");
try {
//2.选取Excel文件得到工作薄Workbook
Workbook workbook = Workbook.getWorkbook(part.getInputStream());
//3.读取工作表
//通过Workbook的getSheet方法选择第一个工作表(从0开始),也可以通过工作的名称来得到Sheet
Sheet sheet = workbook.getSheet(0);
System.out.println(sheet.getColumns());
System.out.println(sheet.getRows());
for (int i = 0; i < sheet.getRows(); i++) {
for (int j = 0; j < sheet.getColumns(); j++) {
//4.读取单元格
//通过Sheet的getCell方法选择位置为C1的单元格(两个参数都从0开始)
Cell cell = sheet.getCell(j, i);
//5.读取单元格的值
String result = cell.getContents();
System.out.println(result + " ");
}
System.out.println();
}
workbook.close();
} catch (BiffException e) {
e.printStackTrace();
}
}
protected void doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, IOException {
}
}
Cell提供了一个getType方法(cell.getType() )
能够返回单元格的类型信息,同时JXL提供了一个CellType类用来预设Excel中的类型信息,而且JXL提供了一些Cell类的子类用来分别用来表示各种类型的单元格,如LabelCell,NumberCell,DateCell分别表示字符、数值、日期类型的单元格
CellType. LABEL CellType. DATE CellType.NUMBER
以释放资源:workbook.close()
当你完成对Excel电子表格数据的处理后,一定要使用close()方法来关闭先前创建的对象,以释放读取数据表的过程中所占用的内存空间,在读取大量数据时显得尤为重要
JSP 导出EXCEL文件
把JSP里面的contentType设置为:
contentType="application/vnd.ms-excel; charset=UTF-8"
打开页面的时候就可以直接保存为一个EXCEL文件
导出数据代码示例
<%@ page contentType="application/vnd.ms-excel; charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table class="table table-condensed table-striped">
<tr>
<th>编号</th>
<th>班级名称</th>
<th>班级人数</th>
<th>开班日期</th>
<th>结束日期</th>
</tr>
<tr>
<td>1001</td>
<td>java一班</td>
<td>30</td>
<td>2014-12-12</td>
<td>2015-12-12</td>
</tr>
<tr>
<td>1001</td>
<td>java一班</td>
<td>30</td>
<td>2014-12-12</td>
<td>2015-12-12</td>
</tr>
<tr>
<td>1001</td>
<td>java一班</td>
<td>30</td>
<td>2014-12-12</td>
<td>2015-12-12</td>
</tr>
</table>
</body>
</html>