第一步、導入依賴
<!--生成excel文件-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.9</version>
</dependency>
第二步,編寫實體類
public class Zhi {
private String url; //地址
private String Requestbody; //請求體
private String Responsebody; //響應體
@Override
public String toString() {
return "Zhi{" +
"url='" + url + '\'' +
", Requestbody='" + Requestbody + '\'' +
", Responsebody='" + Responsebody + '\'' +
'}';
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getRequestbody() {
return Requestbody;
}
public void setRequestbody(String requestbody) {
Requestbody = requestbody;
}
public String getResponsebody() {
return Responsebody;
}
public void setResponsebody(String responsebody) {
Responsebody = responsebody;
}
}
第三步、編寫表格生成工具
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelUtil {
/**
* 導出Excel
* @param sheetName sheet名稱
* @param title 標題
* @param values 內容
* @param wb HSSFWorkbook對象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){
// 第一步,創建一個HSSFWorkbook,對應一個Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一個sheet,對應Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制
HSSFRow row = sheet.createRow(0);
// 第四步,創建單元格,並設置值表頭 設置表頭居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式
//聲明列對象
HSSFCell cell = null;
//創建標題
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//創建內容
for(int i=0;i<values.length;i++){
row = sheet.createRow(i + 1);
for(int j=0;j<values[i].length;j++){
//將內容按順序賦給對應的列對象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}
第四步、編寫controller層
import cn.kgc.pojo.Account;
import cn.kgc.pojo.Zhi;
import cn.kgc.service.impl.ExcelUtil;
import cn.kgc.service.impl.SalaryServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.tagext.PageData;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Controller
@RequestMapping(value = "/report")
public class ReportFormController{
//@Resource(name = "reportService")
//private ReportManager reportService;
@Resource(name = "aoo")
SalaryServiceImpl salaryService;
/**
* 導出報表
* @return
*/
@RequestMapping(value = "/export")
@ResponseBody
public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
//獲取數據
List<Zhi> list = new ArrayList<Zhi>();
Zhi zhi=null;
/*生成隨機數*/
Random random = new Random();
for(int i=0;i<100;i++){
int a = (int)(random.nextInt(900))+100;
zhi=new Zhi();
zhi.setUrl("/NGCRMPF_GS_CGSHQGETFULLUSRINFO_POST");
zhi.setRequestbody("{\"params\":{\"object\":\"ServiceNo:13919892"+a+"\"}}");
zhi.setResponsebody("{\"rtnCode\":\"0\",\"rtnMsg\":\"成功!\",\"bean\":{},\"beans\":[],\"object\":\"0~success~0~75~0~正常~01~58元新飛享套餐~1~0931\"}");
list.add(zhi);
}
//excel標題
String[] title = {"請求地址","請求體","響應體"};
//excel文件名
String fileName = "學生信息表"+System.currentTimeMillis()+".xls";
//sheet名
String sheetName = "學生信息表";
String [][] content = new String[list.size()][];
for (int i = 0; i < list.size(); i++) {
content[i] = new String[title.length];
Zhi obj = list.get(i);
content[i][0] = obj.getUrl()+"";
content[i][1] = obj.getRequestbody()+"";
content[i][2] = obj.getResponsebody()+"";
}
//創建HSSFWorkbook
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
//響應到客戶端
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//發送響應流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

wdqqqqqqqqqqqqqqq
第五步 編寫jsp部分 調用一下即可
<a href="${pageContext.request.contextPath}/report/export">導出Excel表格</a>
