SpringBoot匯入匯出Excel
阿新 • • 發佈:2018-12-26
1 下載jar包
<!-- excel匯出工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
2 匯出Excel
package com.briup.apps.poll.web.controller;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.briup.apps.poll.bean.Answer;
import com.briup.apps.poll.service.IAnswerService;
import com.briup.apps.poll.util.MsgResponse;
@Controller
@RequestMapping("/excel")
public class ExcelController extends BaseController{
@Autowired
private IAnswerService answerService;
/***
* 下載Excel
* @throws IOException
*/
@GetMapping("download")
public void download() throws IOException{
HSSFWorkbook workbook = new HSSFWorkbook();
//建立一個Excel表單,引數為sheet的名字
HSSFSheet sheet = workbook.createSheet("課調答卷表");
//建立表頭
setTitle(workbook, sheet);
List<Answer> answers = answerService.findAll();
//新增資料行,並且設定單元格資料
int rowNum = 1;
for (Answer answer:answers) {
HSSFRow row = sheet.createRow(rowNum);
row.createCell(0).setCellValue(answer.getId());
row.createCell(1).setCellValue(answer.getSelections());
row.createCell(2).setCellValue(answer.getCheckes());
row.createCell(3).setCellValue(answer.getContent());
rowNum++;
}
String fileName = "survey-answer.xlsx";
//清空response
response.reset();
//設定response的Header
response.addHeader("Content-Disposition", "attachment;filename="+ fileName);
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
//將excel寫入到輸出流中
workbook.write(os);
os.flush();
os.close();
}
/***
* 設定表頭
* @param workbook
* @param sheet
*/
private void setTitle(HSSFWorkbook workbook, HSSFSheet sheet){
HSSFRow row = sheet.createRow(0);
//設定列寬,setColumnWidth的第二個引數要乘以256,這個引數的單位是1/256個字元寬度
sheet.setColumnWidth(0, 10*256);
sheet.setColumnWidth(1, 20*256);
sheet.setColumnWidth(2, 20*256);
sheet.setColumnWidth(3, 100*256);
//設定為居中加粗
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBold(true);
style.setFont(font);
HSSFCell cell;
cell = row.createCell(0);
cell.setCellValue("序號");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("單選");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("多選");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("簡答");
cell.setCellStyle(style);
}
}
匯出的結果
再將此Excel讀取,程式碼如下:
3 匯入Excel
@PostMapping("upload")
public MsgResponse upload(MultipartFile file) {
if (file==null) {
return error("file不能為空");
}
List<Answer> answers = new ArrayList<>();
try {
HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(file.getInputStream()));
//有多少個sheet
int sheets = workbook.getNumberOfSheets();
for (int i = 0; i < sheets; i++) {
HSSFSheet sheet = workbook.getSheetAt(i);
//獲取多少行
int rows = sheet.getPhysicalNumberOfRows();
Answer answer = null;
//遍歷每一行,注意:第 0 行為標題
for (int j = 1; j < rows; j++) {
answer = new Answer();
//獲得第 j 行
HSSFRow row = sheet.getRow(j);
answer.setSelections(row.getCell(1).getStringCellValue());//單選
answer.setCheckes(row.getCell(2).getStringCellValue());//多選
answer.setContent(row.getCell(3).getStringCellValue());//簡答
answers.add(answer);
}
}
} catch (IOException e) {
logger.error(e.getMessage(),e);
return error(e.getMessage());
}
return success(answers);
}