POI操作Excel
poi簡介
Apache POI是Apache軟件基金會的開放源碼函式庫,POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫的功能。
1、HSSF:HSSF 是Horrible SpreadSheet Format的縮寫,通過HSSF,你可以用純Java代碼來讀取、寫入、修改Excel文件。
2、POI EXCEL文檔結(jié)構(gòu)類
HSSFWorkbook excel文檔對象
HSSFSheet excel的sheet
HSSFRow excel的行
HSSFCell excel的單元格
HSSFFont excel字體
HSSFName 名稱
HSSFDataFormat 日期格式
HSSFHeader sheet頭
HSSFFooter sheet尾
HSSFCellStyle cell樣式
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 錯誤信息表
3、導(dǎo)出Excel常用的方法:
HSSFWorkbook wb = new HSSFWorkbook(); //創(chuàng)建Excel工作簿對象
HSSFSheet sheet = wb.createSheet("new sheet"); //創(chuàng)建Excel工作表對象
HSSFRow row = sheet.createRow((short)0); //創(chuàng)建Excel工作表的行
cellStyle = wb.createCellStyle(); //創(chuàng)建單元格樣式
row.createCell((short)0).setCellStyle(cellStyle); //創(chuàng)建Excel工作表指定行的單元格
row.createCell((short)0).setCellValue(1); //設(shè)置Excel工作表的值
例如:
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.binshi.common.utils.common.R;
import com.binshi.common.utils.page.PageUtils;
import com.binshi.common.utils.page.Query;
import com.binshi.common.utils.tools.StringUtil;
import com.binshi.store.modules.car.dao.StorePersonEarningsDao;
import com.binshi.store.modules.car.entity.*;
import com.binshi.store.modules.car.service.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
public R export(Long storeId,HttpServletResponse response) throws IOException {
//獲取表頭數(shù)據(jù)
String[] header = {"序號","姓名","電話","車牌","車輛詳情"};
//獲取數(shù)據(jù)內(nèi)容
List<StoreUserEntity> storeUserEntityList = this.selectList(new EntityWrapper<StoreUserEntity>().eq("store_id",storeId));
//聲明一個工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
//設(shè)置表頭樣式
XSSFCellStyle headStyle = setHeadStyle(workbook);
// 給單元格內(nèi)容設(shè)置另一個樣式
XSSFCellStyle cellStyle = setCellStyle(workbook);
//生成一個表格,設(shè)置表格名稱為"門店會員表"
XSSFSheet sheet = workbook.createSheet("門店會員表");
//設(shè)置表格列寬度為10個字節(jié)
sheet.setDefaultColumnWidth(45);
//創(chuàng)建第一行表頭
XSSFRow headrow = sheet.createRow(0);
sheet.setVerticallyCenter(true);
//遍歷添加表頭
for (int i = 0; i < header.length; i++) {
//創(chuàng)建一個單元格
XSSFCell cell = headrow.createCell(i);
//創(chuàng)建一個內(nèi)容對象
XSSFRichTextString text = new XSSFRichTextString(header[i]);
//將內(nèi)容對象的文字內(nèi)容寫入到單元格中
cell.setCellStyle(headStyle);
cell.setCellValue(text);
}
//模擬遍歷數(shù)據(jù)內(nèi)容,把內(nèi)容加入表格
for (int i = 0; i < storeUserEntityList.size(); i++) {
XSSFRow row1 = sheet.createRow(i+1);
for(int j=0;j<header.length;j++){
XSSFCell cell = row1.createCell(j);
XSSFRichTextString text = new XSSFRichTextString();
if(j==0){
text = new XSSFRichTextString(i+1+"");
}else if(j==1){
if(storeUserEntityList.get(i).getName()!=null){
text = new XSSFRichTextString(storeUserEntityList.get(i).getName());
}
}else if(j==2){
if(storeUserEntityList.get(i).getPhone()!=null){
text = new XSSFRichTextString(storeUserEntityList.get(i).getPhone());
}
}else if(j==3){
if(storeUserEntityList.get(i).getCarCodeTotal()!=null){
text = new XSSFRichTextString(storeUserEntityList.get(i).getCarCodeTotal());
}
}else if(j==4){
if(storeUserEntityList.get(i).getCarName()!=null){
text = new XSSFRichTextString(storeUserEntityList.get(i).getCarName());
}
}
cell.setCellStyle(cellStyle);
cell.setCellValue(text);
}
}
// 下載導(dǎo)出
String filename = "報銷申請表.xlsx";
// 設(shè)置頭信息
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
//一定要設(shè)置成xlsx格式
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename , "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
filename = URLEncoder.encode(filename, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-disposition", "attachment;filename="+filename+";"+"filename*=utf-8''"+filename);
//創(chuàng)建一個輸出流
ServletOutputStream outputStream = response.getOutputStream();
//寫入數(shù)據(jù)
workbook.write(outputStream);
return R.ok("導(dǎo)出成功");
}
導(dǎo)出后的excel表格如下圖所示:

自此,使用POI將數(shù)據(jù)導(dǎo)出到excel表格完成。
ps:關(guān)于導(dǎo)出數(shù)據(jù)會遇到的問題:poi和poi-ooxml版本不一致可能會出現(xiàn)錯誤。
把版本該成一致就好啦
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10</version>
</dependency>