标签:成功 lang add 文件的 res leo ble catch process
如有需要可以加我Q群【308742428】大家一起讨论技术,提供技术支持。
后面会不定时为大家更新文章,敬请期待。
前端上传excel文件到后台,后台接收后保存数据到数据库。
这里需要说明的一点是前端如果是用form提交那么需要在form里添加属性
method=‘post‘ enctype="multipart/form-data"
如果是用ajax提交的那么需要在请求方法添加添加
processData: false, contentType : false, // 不要设置Content-Type请求头
后台接收的action:
这里是把request请求转化为MultipartFile,表示当前请求是一个文件上传
//批量添加 @ResponseBody @RequestMapping(value = "/addAllPersonnel",method = RequestMethod.POST) @CrossOrigin public Map<String,Object> addpersonnel(HttpServletRequest request) { Map<String,Object> map = new HashMap<>(); try { String userToken = request.getParameter("token"); Administrator user = (Administrator) CacheUtils.get(userToken); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("excel_file"); // 1.取得上传目录父路径 String baseDir =request.getServletContext().getRealPath("/")+ "tmp/"; // 2.重命名上传文件 String fileName = "excel_" + new Date().getTime() + file.getOriginalFilename().replaceAll("^[\\s\\S]+(\\.\\w+)", "$1"); // 3.建立文件路径 File _baseDir = new File(baseDir); if (!_baseDir.exists() && !_baseDir.mkdir()){ } // 4.转储为文件 File localFile = new File(baseDir + fileName); file.transferTo(localFile); personnelService.addAllPersonnel(localFile); map.put("code",200); map.put("msg","添加成功"); return map; }catch (Exception e){ map.put("data",""); map.put("code",400); map.put("msg","添加失败"); return map; } }
service实现类里对应的方法:
Personnel是一个实体类,里面的字段根据需求自己写
//批量添加 public void addAllPersonnel(File file) { try { // 1.将file文件内容转为list集合 List<List<Object>> list = ExcelUtils.importExcel2Collection(file, 0); // 2.循环生成对象,并存入数据库 Personnel personnel = new Personnel(); int size = list.size(); SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (int i=1;i<size;i++){ List<Object> tmp = list.get(i); String serialnumber = (String) tmp.get(0); //学号重复的不添加 int query = personnelDao.query(serialnumber); if (query!=0){ continue; } personnel.setSerialnumber(serialnumber); personnel.setUsername((String) tmp.get(1)); personnel.setType("学生"); personnel.setGrade((String) tmp.get(2)); personnel.setBirthday((String) tmp.get(3)); personnel.setSex("男".equals((String) tmp.get(4))?1:2); personnel.setInputtingperson((String) tmp.get(5)); personnel.setPhone((String) tmp.get(6)); personnel.setParrent((String) tmp.get(7)); personnel.setParrentPhone((String) tmp.get(8)); personnel.setAge((String) tmp.get(9)); personnel.setGroupManager((String) tmp.get(10)); personnel.setSchool((String) tmp.get(11)); personnel.setHint((String) tmp.get(12)); personnel.setWeight((String) tmp.get(13)); personnel.setBlood((String) tmp.get(14)); personnel.setAddress((String) tmp.get(15)); personnel.setHobby((String) tmp.get(16)); personnel.setJdTime(simpleDateFormat.format(new java.util.Date())); //保存到数据库 personnelDao.addpersonnel(personnel); } }catch (Exception e){ System.out.println(e); } }
下载excel,这里我是从数据库查出需要都的数据,然后遍历list添加到excel里面。
前端请求地址就是action的地址,主要action我们这里定义的是void没有返回类型。
/** * 下载 * @return */ @ResponseBody @RequestMapping("/download") @CrossOrigin public void download(Paging paging, String query, HttpServletRequest request, HttpServletResponse response) { Map<String,Object> map = new HashMap<>(); try { List<Personnel> likepersonnel = personnelService.likepersonnel(paging); if (likepersonnel.size()==0){ map.put("data",""); map.put("code",404); }else{ // 文件名 String filename = "数据.xls"; ExcelData data = new ExcelData(); data.setName("数据"); List<String> titles = new ArrayList(); titles.add("学号"); titles.add("姓名"); titles.add("班级"); titles.add("出生年月"); titles.add("性别"); titles.add("建档人"); titles.add("手机号"); titles.add("家长"); titles.add("家长手机"); titles.add("年龄"); titles.add("班主任"); titles.add("学校"); titles.add("身高"); titles.add("体重"); titles.add("血型"); titles.add("家庭地址"); titles.add("爱好"); data.setTitles(titles); List<List<Object>> rows = new ArrayList(); for (int i=0;i<likepersonnel.size();i++){ List<Object> row1 = new ArrayList(); row1.add(likepersonnel.get(i).getSerialnumber()); row1.add(likepersonnel.get(i).getUsername()); row1.add(likepersonnel.get(i).getGrade()); row1.add(likepersonnel.get(i).getBirthday()); int sex = likepersonnel.get(i).getSex(); String se="男"; if (sex!=1){ se="女"; } row1.add(se); row1.add(likepersonnel.get(i).getInputtingperson()); row1.add(likepersonnel.get(i).getPhone()); row1.add(likepersonnel.get(i).getParrent()); row1.add(likepersonnel.get(i).getParrentPhone()); row1.add(likepersonnel.get(i).getAge()); row1.add(likepersonnel.get(i).getGroupManager()); row1.add(likepersonnel.get(i).getSchool()); row1.add(likepersonnel.get(i).getHint()); row1.add(likepersonnel.get(i).getWeight()); row1.add(likepersonnel.get(i).getBlood()); row1.add(likepersonnel.get(i).getAddress()); row1.add(likepersonnel.get(i).getHobby()); rows.add(row1); data.setRows(rows); } ExcelUtils.exportExcel(response,filename,data); } }catch (Exception e){ map.put("data",""); map.put("code",400); } }
这里面我们用到了一个ExcelData实体类,是用来封装excel的。
import java.io.Serializable; import java.util.List; public class ExcelData implements Serializable { private static final long serialVersionUID = 4454016249210520899L; /** * 表头 */ private List<String> titles; /** * 数据 */ private List<List<Object>> rows; /** * 页签名称 */ private String name; public List<String> getTitles() { return titles; } public void setTitles(List<String> titles) { this.titles = titles; } public List<List<Object>> getRows() { return rows; } public void setRows(List<List<Object>> rows) { this.rows = rows; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
还有ExcelUtils工具类,里面包含创建excel设置一些属性样式的方法都注释,自己看。
import com.ebatis.impl.Init; import com.ebatis.pojo.ActionContext; import com.ebatis.pojo.SheetInfo; import com.zkkz.entily.Personnel; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFRichTextString; 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.hssf.util.HSSFColor; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.ss.formula.functions.T; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.PaneInformation; import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.*; //excel工具类 public class ExcelUtils { public static List<Personnel> readFile(String file){ return null; } /** * 下载文件 * @param response * @param fileName * @param data * @throws Exception */ 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()); } /** * 创建 表格 * @param data * @param out * @throws Exception */ 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); } finally { wb.close(); } } /** * 将数据写入表格 * @param wb * @param sheet * @param data */ 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); } /** * 写入表头 * @param wb * @param sheet * @param titles * @return */ private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List<String> titles) { int rowIndex = 0; int colIndex = 0; Font titleFont = wb.createFont();//获取字体 titleFont.setFontName("simsun");//设置字体名称(宋体) titleFont.setBold(true);//设置字体加粗 titleFont.setColor(IndexedColors.BLACK.index);//设置字体颜色 黑色 XSSFCellStyle titleStyle = wb.createCellStyle();//获取单元格样式 titleStyle.setAlignment(HorizontalAlignment.CENTER);//设置单元格的水平对齐类型(这里是水平居中) titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置单元格的垂直对齐类型(这里是居中) titleStyle.setFillForegroundColor(createXssfColor("#FFFFFF"));//设置单元格前景色(白色) titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//指定图案和纯色单元格填充的单元格填充信息(实心前景) titleStyle.setFont(titleFont);//设置字体样式 setBorder(titleStyle, BorderStyle.THIN, createXssfColor("#000000"));//设置边框样式(细线、黑色) 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; } /** * 将数据写入 * @param wb * @param sheet * @param rows * @param rowIndex * @return */ private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List<List<Object>> rows, int rowIndex) { int colIndex = 0; Font dataFont = wb.createFont();//获取字体 dataFont.setFontName("simsun");//设置字体名称(宋体) dataFont.setColor(IndexedColors.BLACK.index);//设置字体颜色 黑色 XSSFCellStyle dataStyle = wb.createCellStyle();//获取单元格样式 dataStyle.setAlignment(HorizontalAlignment.CENTER);//设置单元格的水平对齐类型(这里是水平居中) dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置单元格的垂直对齐类型(这里是居中) dataStyle.setFont(dataFont);//设置字体样式 setBorder(dataStyle, BorderStyle.THIN, createXssfColor("#000000"));//设置边框样式(细线、黑色) for (List<Object> rowData : rows) {//循环写入数据 Row dataRow = sheet.createRow(rowIndex); colIndex = 0; for (Object cellData : rowData) { Cell cell = dataRow.createCell(colIndex); if (cellData != null) { cell.setCellValue(cellData.toString()); } else { cell.setCellValue(""); } cell.setCellStyle(dataStyle); colIndex++; } rowIndex++; } return rowIndex; } /** * 自动调整大小 * @param sheet * @param columnNumber */ private static void autoSizeColumns(Sheet sheet, int columnNumber) { for (int i = 0; i < columnNumber; i++) { int orgWidth = sheet.getColumnWidth(i); sheet.autoSizeColumn(i, true); int newWidth = (int) (sheet.getColumnWidth(i) + 100); if (newWidth > orgWidth) { sheet.setColumnWidth(i, newWidth); } else { sheet.setColumnWidth(i, orgWidth); } } } /** * 设置表格样式 * @param style * @param border * @param color */ private static void setBorder(XSSFCellStyle style, BorderStyle border, XSSFColor color) { style.setBorderTop(border); style.setBorderLeft(border); style.setBorderRight(border); style.setBorderBottom(border); style.setBorderColor(XSSFCellBorder.BorderSide.TOP, color); style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, color); style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, color); style.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, color); } /** * 将rgb颜色码 转换为 XSSFColor * @param color * @return */ private static XSSFColor createXssfColor(String color) { int[] rgbColor = hexToRgb(color); XSSFColor xssfColor = new XSSFColor(new java.awt.Color(rgbColor[0], rgbColor[1], rgbColor[2])); return xssfColor; } /** * 将颜色码 转换为 r g b * @param hex * @return */ public static int[] hexToRgb(String hex) { String colorStr = hex; if (hex.startsWith("#")) { colorStr = hex.substring(1); } if (StringUtils.length(colorStr) == 8) { colorStr = hex.substring(2); } int r= Integer.valueOf( colorStr.substring( 0, 2 ), 16 ); int g= Integer.valueOf( colorStr.substring( 2, 4 ), 16 ); int b= Integer.valueOf( colorStr.substring( 4, 6 ), 16 ); return new int[] { r, g, b }; } /** * Description: 将对象集合写入到excel中 eq:对象的属性应和excel的header对应 <BR> * * @author dsn * @date 2017年9月15日 下午3:55:57 * @return * @version 1.0 */ public static boolean exportExcel(List<Personnel> personnels, String fileName, HttpServletResponse response) throws FileNotFoundException, UnsupportedEncodingException { // 告诉浏览器用什么软件可以打开此文件 response.setHeader("content-Type", "application/vnd.ms-excel"); // 下载文件的默认名称 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8")); HSSFWorkbook mWorkbook = new HSSFWorkbook(); HSSFSheet mSheet = mWorkbook.createSheet("数据"); // 创建Excel标题行,第一行。 HSSFRow headRow = mSheet.createRow(0); headRow.createCell(0).setCellValue("学号"); headRow.createCell(1).setCellValue("姓名"); headRow.createCell(2).setCellValue("班级"); headRow.createCell(3).setCellValue("出生年月"); headRow.createCell(4).setCellValue("性别"); headRow.createCell(5).setCellValue("建档人"); headRow.createCell(6).setCellValue("手机号"); headRow.createCell(7).setCellValue("家长"); headRow.createCell(8).setCellValue("家长手机"); headRow.createCell(9).setCellValue("年龄"); headRow.createCell(10).setCellValue("班主任"); headRow.createCell(11).setCellValue("学校"); headRow.createCell(12).setCellValue("身高"); headRow.createCell(13).setCellValue("体重"); headRow.createCell(14).setCellValue("血型"); headRow.createCell(15).setCellValue("家庭地址"); headRow.createCell(16).setCellValue("爱好"); // 往Excel表中写入3行测试数据。 for (int i=0;i<personnels.size();i++){ createCell(personnels.get(i), mSheet); } // File xlsFile = new File(path); // mWorkbook.write(xlsFile);// 或者以流的形式写入文件 mWorkbook.write(new FileOutputStream(xlsFile)); try { mWorkbook.write(response.getOutputStream()); // mWorkbook.write(xlsFile); mWorkbook.close(); return false; } catch (IOException e) { e.printStackTrace(); } return false; } // 创建Excel的一行数据。 private static void createCell(Personnel personnel,HSSFSheet sheet) { HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1); dataRow.createCell(0).setCellValue(personnel.getSerialnumber()); dataRow.createCell(1).setCellValue(personnel.getUsername()); dataRow.createCell(2).setCellValue(personnel.getGrade()); dataRow.createCell(3).setCellValue(personnel.getBirthday()); int sex = personnel.getSex(); String se="男"; if (sex!=1){ se="女"; } dataRow.createCell(4).setCellValue(se); dataRow.createCell(5).setCellValue(personnel.getInputtingperson()); dataRow.createCell(6).setCellValue(personnel.getPhone()); dataRow.createCell(7).setCellValue(personnel.getParrent()); dataRow.createCell(8).setCellValue(personnel.getParrentPhone()); dataRow.createCell(9).setCellValue(personnel.getAge()); dataRow.createCell(10).setCellValue(personnel.getGroupManager()); dataRow.createCell(11).setCellValue(personnel.getSchool()); dataRow.createCell(12).setCellValue(personnel.getHint()); dataRow.createCell(13).setCellValue(personnel.getWeight()); dataRow.createCell(14).setCellValue(personnel.getBlood()); dataRow.createCell(15).setCellValue(personnel.getAddress()); dataRow.createCell(16).setCellValue(personnel.getHobby()); } /** * Description:将excel读入到对象集合中 <BR> * * @author dsn * @date 2017年9月15日 下午3:56:53 * @param file * @param sheetIndex * @return * @version 1.0 */ public static List<List<Object>> importExcel2Collection(File file, int sheetIndex) { List<List<Object>> list = new LinkedList<List<Object>>(); Object value = null; int counter = 0; Workbook wb = null; try { wb = new HSSFWorkbook(new FileInputStream(file)); } catch (OfficeXmlFileException e) { try { wb = new XSSFWorkbook(new FileInputStream(file)); } catch (IOException e1) { System.out.println("excel读入到对象异常,不支持的excel类型!,"+e); return null; } } catch (Exception e) { System.out.println("excel读入到对象异常,不支持的excel类型!,"+e); return null; } Sheet sheet = wb.getSheetAt(sheetIndex); if (sheet == null) return null; Row row; Cell cell; for (int i = sheet.getFirstRowNum(); counter < sheet.getPhysicalNumberOfRows(); i++) { row = sheet.getRow(i); if (row == null) continue; else counter++; List<Object> linked = new LinkedList<Object>(); for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { cell = row.getCell(j); if (cell == null) { value = ""; linked.add(value); continue; } DecimalFormat df = new DecimalFormat("0");// 格式化 number String SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化日期字符串 DecimalFormat nf = new DecimalFormat("0");// 格式化数字 switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if ("General".equals(cell.getCellStyle().getDataFormatString())) { value = nf.format(cell.getNumericCellValue()); } else { value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())); } break; case XSSFCell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case XSSFCell.CELL_TYPE_BLANK: value = ""; break; default: value = cell.toString(); } linked.add(value); } list.add(linked); } return list; } }
调用下载方法会用XSSFWorkbook类的write方法把文件流传到前端。
wb.write(out);
out就是response.getOutputStream() 。
excel的导入导出功能已经完成,工具类可以拿过去使用,注意添加jar依赖。
<!--文件读取--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
注意两个包的版本要一致。
标签:成功 lang add 文件的 res leo ble catch process
原文地址:https://www.cnblogs.com/dsn727455218/p/12793969.html