当前位置:首页 > 实用技巧 >

java导入excel文件的代码(java读取excel文件模板)

来源:原点资讯(www.yd166.com)时间:2023-05-11 07:23:32作者:YD166手机阅读>>

1、导入

public STRING importExcel(@RequestParam("excelPath") MultipartFile file,@RequestParam("fileName") String fileName){ try { if(!file.isEmpty()) { BASE64Decoder decoder = new BASE64Decoder(); String filename = new String(decoder.decodeBuffer(fileName),"UTF-8"); int index = filename.lastIndexOf("."); filename = filename.substring(0, index) "_" userid filename.substring(index, filename.length()); File fileDir = FileUtils.getExeclDirFile(super.uploadPath); File newFile = new File(fileDir.getAbsolutePath() File.separator filename); file.transferTo(newFile); //各种验证 if (!fileName.matches("^. \\.(?i)(xls)$") && !fileName.matches("^. \\.(?i)(xlsx)$")) { throw new Exception("上传文件格式不正确"); } boolean isExcel2003 = true; if (fileName.matches("^. \\.(?i)(xlsx)$")) { isExcel2003 = false; } Workbook wb = null; inputStream is = new FileInputStream(file); try{ if (isExcel2003) { wb = new HSSFWorkbook(is); } else { wb = new XSSFWorkbook(is); } Sheet sheet = wb.getSheetAt(0); Row row = sheet.getRow(2);//第一行 XSSFCell cell = (XSSFCell) row.getCell(1); if(cell == null) { return FastJsonUtils.resultSuccess(400, "失败","格式验证"); } cell.setCellType(CellType.STRING); String year = cell.getStringCellValue(); if(StringUtils.isEmpty(year)){ return FastJsonUtils.resultSuccess(400, "失败","格式验证"); } //从第几列开始 if(sheet.getLastRowNum() >=4) { for (int r = 4; r <= sheet.getLastRowNum(); r ) { row = sheet.getRow(r); if (row == null){ continue; } //获取每列的数据 XSSFCell cellDealerCode = (XSSFCell) row.getCell(0); if(cellDealerCode == null) { continue; } cellDealerCode.setCellType(CellType.STRING); String str = cellDealerCode.getStringCellValue(); //TODO 处理业务逻辑 } } }finally { if(wb != null) {wb.close(); } if (is != null){is.close();} } } else { return FastJsonUtils.resultSuccess(400, "失败","文件为空!"); } } catch (Exception e) { return FastJsonUtils.resultSuccess(400, "失败","上传文件失败,请重试!"); } }

2、导出

public void downExcel(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException { String filename = "要显示的文件名.xlsx"; InputStream input = null; OutputStream outputStream = null; SXSSFWorkbook wb = null; XSSFWorkbook swb = null; try { ClassPathResource cpr = new ClassPathResource("/template/" filename); input = cpr.getInputStream(); swb = (XSSFWorkbook) WorkbookFactory.create(input); wb = new SXSSFWorkbook(swb,-1); Sheet sheet = wb.getSheetAt(0); // 工作表对象 Row nRow = null; // 行对象 Cell nCell = null; // 列对象 //设置格式样式 CellStyle style = ExportExcelUtil.setStyle(wb, null, true, true, CommonType.fontNAME, 10, false); //TODO 根据自己的需求赋值 nCell = swb.getSheetAt(0).getRow(2).getCell(1); nCell.setCellValue("第一个sheet页第三行的第二格赋值"); //处理各浏览器的兼容,防止乱码 String agent = request.getHeader("USER-AGENT"); if (null != agent && (-1 != agent.indexOf("MSIE") || -1 != agent.indexOf("Trident") || -1 != agent.indexOf("Edge"))) { codedfilename = java.net.URLEncoder.encode(filename, "UTF8"); } else { codedfilename = new String(filename.getBytes("UTF-8"), "iso-8859-1"); } response.setContentType("application/msexcel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=\"" codedfilename "\""); response.flushBuffer(); outputStream = response.getOutputStream(); wb.write(response.getOutputStream()); outputStream.flush(); } catch (Exception e) { e.printStackTrace(); }finally { try { if (swb != null) {swb.close();} if (wb != null) {wb.close();} if (outputStream != null) {outputStream.close();} if (input != null) {input.close();} } catch (Exception e) {} } }

3、单元格样式设置

public static CellStyle setStyle(Workbook wb,Object backgroundColor,boolean isAlignment,boolean isWrapText,String fontName,Object fontSize,boolean isBold) { CellStyle style = wb.createCellStyle(); if(backgroundColor != null) { style.setFillForegroundColor(Short.valueOf(backgroundColor.toString()));// 设置背景色 style.setFillPattern(FillPatternType.SOLID_FOREGROUND); } if(isAlignment) { style.setAlignment(HorizontalAlignment.CENTER); // 水平居中 style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中 } if(isWrapText) { style.setWrapText(true);//设置自动换行 } //在样式用应用设置的字体; Font font = wb.createFont(); if(!StringUtils.isEmpty(fontName)) { font.setFontName(fontName);//设置字体名字 } if(fontSize != null) { font.setFontHeightInPoints(Short.valueOf(fontSize.toString()));//设置字体大小 } if(isBold) { font.setBold(true); } style.setBorderBottom(BorderStyle.THIN);//下边框 style.setBorderLeft(BorderStyle.THIN);//左边框 style.setBorderTop(BorderStyle.THIN);//上边框 style.setBorderRight(BorderStyle.THIN);//右边框 style.setFont(font); return style; }

好好学习,天天搬砖,请点击关注学习更多java小知识

栏目热文

java连接excel数据源(java操作excel的库)

java连接excel数据源(java操作excel的库)

前言在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel、c...

2023-05-11 06:49:03查看全文 >>

java读取excel数据库(java获取数据库中的数据到excel中)

java读取excel数据库(java获取数据库中的数据到excel中)

前言:本篇博客读写Excel使用的是EasyExcel,EasyExcel是一个基于Java的简单、省内存的读写Exce...

2023-05-11 06:49:26查看全文 >>

java将数据导入excel(java将数据写入excel)

java将数据导入excel(java将数据写入excel)

程序员创业记跟大家分享一款excel组件,之所以分享这款,是因为它在处理excel时很方便,我将百万数据导出到excel...

2023-05-11 07:23:58查看全文 >>

java导入导出excel的工具包(java excel导入导出工具)

java导入导出excel的工具包(java excel导入导出工具)

前言在开发应用系统的时候,导出文件是必不可放的功能。以前用过poi、easyexcel等工具的导入导出功能,但总感觉太麻...

2023-05-11 06:44:54查看全文 >>

java完整excel导入mysql数据库(java读取excel文件并导入数据库)

java完整excel导入mysql数据库(java读取excel文件并导入数据库)

看过我之前分享的朋友,应该知道之前我用 ,需要导入大量的数据,也就研究了一下如何在后台批量导入大量的数据。因为源数据都是...

2023-05-11 07:10:10查看全文 >>

java中插入数据库表格(java添加数据到数据库的动作)

java中插入数据库表格(java添加数据到数据库的动作)

1、新建学生表这节课我们来把和数据库以及jdbc相关的内容完成,首先,进行数据库建表。数据库呢,我们采用mysql数据库...

2023-05-11 07:11:54查看全文 >>

java excel模板导出数据(java 动态模板excel数据导出)

java excel模板导出数据(java 动态模板excel数据导出)

目录业务场景原因分析解决思路优化后效果实现代码DEMO业务场景由于很多业务需要导出数据库里的数据,一般我们导出的数据都是...

2023-05-11 06:48:49查看全文 >>

java 导入excel控件(java将excel导入数据库)

java 导入excel控件(java将excel导入数据库)

一、概述在Excel中录入数据的同时可能会需要插入一些表单控件作为辅助功能。通过表单控件,用户可快速地将数据填写到模块文...

2023-05-11 07:13:34查看全文 >>

小学生品行鉴定评语(小学生在校表现简短评语)

小学生品行鉴定评语(小学生在校表现简短评语)

1.你性情温和、言语不多,但待人诚恳、礼貌。你思想健康,要求进步。你学习渴望进步,并有了很大的进步,但理科上还缺乏正确有...

2023-05-11 07:05:26查看全文 >>

六年级作文的写作技巧和方法(小学生六年级作文的写作技巧)

六年级作文的写作技巧和方法(小学生六年级作文的写作技巧)

一、写作方法与技巧 选择有代表性的景物一年四季都有它有代表性的景物,夏日的炎炎烈日,秋日的落叶纷纷,冬日的皑皑白雪,都是...

2023-05-11 07:15:08查看全文 >>

文档排行