1.1. Java操作Excel
1.1.1. 引入poi需要的jar包
<!-- poi支持的jar包 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
1.1.2. 创建Excel(完成99乘法表)
package cn.itsource.pss.common;
…
public class ExcelTest {
//创建一个Excel文件
//在文件里加一个99乘法法
//把这文件保存在项目中
@Test
public void testCreateExcel() throws Exception{
//1.创建一个Excel文件(内存中)
SXSSFWorkbook wb = new SXSSFWorkbook();
//2.创建一张表
Sheet sheet = wb.createSheet("99乘法表");
//3.创建行
for (int i = 1; i <= 9; i++) {
Row row = sheet.createRow(i-1);
//4.创建列(格子)
for (int j = 1; j <= i; j++) {
Cell cell = row.createCell(j-1);
//5.格子中加数据
cell.setCellValue(i+"*"+j+"="+(i*j));
}
}
//从内存中写出来
FileOutputStream out = new FileOutputStream("99.xlsx");
wb.write(out);
out.close();
}
}
1.1.3. 读取Excel
可以使用我们已经准备好的excel文件做测试
/**
* 读取我们使用相应的方案
* @throws Exception
*/
@Test
public void readExcel() throws Exception{
File file = new File("employee-3.xlsx");
FileInputStream fis = new FileInputStream(file);
//1.读取一个Excel文件(内存中)
Workbook wb = new XSSFWorkbook(fis);
//2.拿到第个sheet表
Sheet sheet = wb.getSheetAt(0);
//3.拿到wb中的行(不要拿头部)
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
//4.拿到每一列(格子)
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row.getCell(j);
System.out.print(cell.getStringCellValue()+" ");
}
System.out.println();
}
}
1.2. SpringMVC导出功能(了解认识)
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //mime类型
response.setHeader("Content-disposition", "attachment;filename="+filename);
response.setHeader("Pragma", "No-cache");//设置不要缓存
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
1.2.1. 直接导出Excel文件
- employee.jsp添加导出按钮
<a href="/employee/download " class="easyui-linkbutton" iconCls="icon-redo" plain="true">导出</a>
- EmployeeController完成导出功能
@RequestMapping("/download")
public void download(HttpServletResponse response) throws Exception{
//准备下载的文件名
String filename = "employee.xlsx";
response.setHeader("Content-disposition", filename);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //mime类型
response.setHeader("Content-disposition", "attachment;filename="+filename);
response.setHeader("Pragma", "No-cache");
//1.创建一个Excel文件(内存中)
SXSSFWorkbook wb = new SXSSFWorkbook();
//2.创建一张表
Sheet sheet = wb.createSheet("99乘法表");
//3.创建行
for (int i = 1; i <= 9; i++) {
Row row = sheet.createRow(i-1);
//4.创建列(格子)
for (int j = 1; j <= i; j++) {
Cell cell = row.createCell(j-1);
//5.格子中加数据
cell.setCellValue(i+"*"+j+"="+(i*j));
}
}
//从内存中写出来
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
}
1.2.2. 导出真实数据
导出数据时提交表单
- 完成导出功能的工具方法
package cn.itsource.pss.common;
…
//Excel的公共类
public class ExcelUtils {
/**
* 导出一个Excel文件
* @param filename 文件名称
* @param heads 文件头
* @param datas 文件内容
* @param response 响应
* @throws Exception
*/