一、pom引入
org.apache.poi poiRELEASE org.apache.poi poi-ooxmlRELEASE org.springframework.boot spring-boot-starter-thymeleaf
二、实体类
package com.re.r.pojo; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import java.io.Serializable; @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class Teacher implements Serializable { private Integer id; private String teaNo; private String teaName; }
三、ExcelUntil类
package com.re.r.until; import com.re.r.pojo.Teacher; import org.apache.poi.hssf.usermodel.HSSFCell; 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.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.awt.Color; import java.net.URLEncoder; public class ExcelUtils2 { public static void exportExcel(HttpServletResponse response, String fileName, ExcelData data) throws Exception { // 告诉浏览器用什么软件可以打开此文件 response.setHeader("content-Type", "application/vnd.ms-excel"); // 下载文件的默认名称 response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8")); exportExcel(data, response.getOutputStream()); } public static void exportExcel(ExcelData data, OutputStream out) throws Exception { XSSFWorkbook wb = new XSSFWorkbook(); try { String sheetName = data.getName(); if (null == sheetName) { sheetName = "Sheet1"; } XSSFSheet sheet = wb.createSheet(sheetName); writeExcel(wb, sheet, data); wb.write(out); } catch(Exception e){ e.printStackTrace(); }finally{ //此处需要关闭 wb 变量 out.close(); } } private static void writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelData data) { int rowIndex = 0; rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles()); writeRowsToExcel(wb, sheet, data.getRows(), rowIndex); autoSizeColumns(sheet, data.getTitles().size() + 1); } private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, Listtitles) { int rowIndex = 0; int colIndex = 0; XSSFFont titleFont = wb.createFont(); titleFont.setFontName("simsun"); titleFont.setColor(IndexedColors.BLACK.index); XSSFCellStyle titleStyle = wb.createCellStyle(); // titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // titleStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); //titleStyle.setFillForegroundColor(new XSSFColor(new Color(182, 184, 192))); // titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); titleStyle.setFont(titleFont); //setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0))); Row titleRow = sheet.createRow(rowIndex); colIndex = 0; for (String field : titles) { Cell cell = titleRow.createCell(colIndex); cell.setCellValue(field); cell.setCellStyle(titleStyle); colIndex++; } rowIndex++; return rowIndex; } private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List > rows, int rowIndex) { int colIndex = 0; XSSFFont dataFont = wb.createFont(); dataFont.setFontName("simsun"); dataFont.setColor(IndexedColors.BLACK.index); XSSFCellStyle dataStyle = wb.createCellStyle(); //dataStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); //dataStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); dataStyle.setFont(dataFont); // setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0))); for (List
四、controller测试一下
templates建立上传前端文件
上传
package com.re.r.controller; import com.re.r.pojo.Teacher; import com.re.r.until.ExcelData; import com.re.r.until.ExcelUtils2; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import static org.apache.tomcat.util.bcel.classfile.ElementValue.STRING; @Controller public class UploadController { @PostMapping("/importSup") @ResponseBody public String importSup(@RequestParam(value = "file") MultipartFile file) throws IOException { //获取数据集合 Listsuppliers = ExcelUtils2.excel2Supplier(file); System.out.println(file.getOriginalFilename()); System.out.println(suppliers); return " *** 作成功"; //插入数据库 //supplierService.addSuppliers(suppliers); } @RequestMapping("/index1") public String indexHtml() { return "index"; } @RequestMapping(value = "/export", method = RequestMethod.GET) public void excel(HttpServletResponse response) throws Exception { ExcelData data = new ExcelData(); data.setName("hello"); List titles = new ArrayList(); titles.add("a1"); titles.add("a2"); titles.add("a3"); data.setTitles(titles); List > rows = new ArrayList(); List
上传(导入)测试结果
导出测试结果
简单excel导入导出的就搞定了 !!!!是不是很简单!!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)