使用POI 3.10.1读取 Excel 2007格式
来源:互联网 发布:excel sql语句 编辑:程序博客网 时间:2024/06/01 20:55
相关jar包,自行去poi官网下载,其中包含了对于date类型的处理,其中该列必须在excel中设置为日期类型。
解析date 工具类:
package com.wonders.task.excel.util;import org.apache.poi.ss.usermodel.DateUtil;import java.util.Calendar;/** * Created with IntelliJ IDEA. * User: zhoushun * Date: 2014/12/7 * Time: 0:36 * To change this template use File | Settings | File Templates. */public class XSSFDateUtil extends DateUtil { protected static int absoluteDay(Calendar cal, boolean use1904windowing) { return DateUtil.absoluteDay(cal, use1904windowing); }}
package com.wonders.task.excel.util;import com.wonders.task.excel.model.SendBo;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;import java.io.FileInputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created with IntelliJ IDEA. * User: zhoushun * Date: 2014/12/6 * Time: 20:25 * To change this template use File | Settings | File Templates. */public class PoiUtil { private static String excel2007 = "D:\\all.xlsx"; private static void setCode(SendBo bo){ Pattern pattern = Pattern.compile("(\\S+)\\((\\d+)\\)(\\d+)号"); String sendId = bo.getSendId(); if(sendId == null || sendId.length() == 0){ bo.setCode1("");bo.setCode2("");bo.setCode3(""); }else{ Matcher matcher = pattern.matcher(sendId); if(matcher.find()){ bo.setCode1(matcher.group(1)); bo.setCode2(matcher.group(2)); bo.setCode3(matcher.group(3)); } } } public static List<SendBo> readExcel2007(){ List<SendBo> list = new ArrayList<SendBo>(); try{ File excelFile = new File(excel2007); FileInputStream is = new FileInputStream(excelFile);// 获取文件输入流 XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 创建Excel2003文件对象 XSSFSheet sheet = workbook2007.getSheetAt(0);// 取出第一个工作表,索引是0 // 开始循环遍历行,表头不处理,从1开始 for (int i = 1; i <= sheet.getLastRowNum(); i++) { XSSFRow row = sheet.getRow(i);// 获取行对象 if (row == null) {// 如果为空,不处理 continue; } SendBo bo = new SendBo(); // 循环遍历单元格 for (int j = 0; j < row.getLastCellNum(); j++) { XSSFCell cell = row.getCell(j);// 获取单元格对象 switch(j){ case 0 : bo.setSendDept(getStringCellValue(cell).trim()); break; case 1 : bo.setSendId(getStringCellValue(cell).trim()); break; case 2 : bo.setSendDate(getStringCellValue(cell).trim()); setCode(bo); break; case 3 : bo.setTitle(getStringCellValue(cell).trim()); break; case 4 : bo.setSendMain(getStringCellValue(cell).trim()); break; case 5 : break; case 6 : break; default: break; } } list.add(bo); } }catch(Exception e){} return list; } /** * 获取单元格数据内容为字符串类型的数据 * * @param cell Excel单元格 * @return String 单元格数据内容 */ private static String getStringCellValue(XSSFCell cell) { String strCell = ""; switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: strCell = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if (XSSFDateUtil.isCellDateFormatted(cell)) { // 如果是date类型则 ,获取该cell的date值 strCell = new SimpleDateFormat("yyyy-MM-dd").format(XSSFDateUtil.getJavaDate(cell.getNumericCellValue())); } else { // 纯数字 strCell = String.valueOf(cell.getNumericCellValue()); } break; case XSSFCell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case XSSFCell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } if (strCell.equals("") || strCell == null) { return ""; } if (cell == null) { return ""; } return strCell; } public static void main(String[] args){ String s = "沪地铁(2014)22号"; Pattern pattern = Pattern.compile("(\\S+)\\((\\d+)\\)(\\d+)号"); Matcher matcher = pattern.matcher(s); if(matcher.find()){ int gc = matcher.groupCount(); for(int i = 0; i <= gc; i++) System.out.println("group " + i + " :" + matcher.group(i)); } //readExcel2007(); }}
0 0
- 使用POI 3.10.1读取 Excel 2007格式
- java使用poi读取xlsx格式的Excel总结
- java使用poi.3.10读取excel 2010
- 使用POI读取EXCEL文件
- 使用POI读取EXCEL文件
- 使用 apache poi 读取 Excel
- Springmvc 使用 poi 读取excel
- poi读取excel的使用
- 使用POI读取excel文件
- java使用poi读取excel
- poi读取excel表格格式问题解决方案
- java poi读取excel日期格式数据
- java poi读取excel日期格式数据
- poi 3.10 读取excel(2003/2007)
- java使用poi读取xls格式和xlsx格式的excel文件
- 使用POI操作Excel,读取、写入Excel
- java使用poi读取存储excel表格,包括xls和xlsx格式
- 使用POI设置EXCEL单元格格式
- 栈,队列算法题
- 树莓派制作,敲击式Flappy-Bird,远程PPT
- 校招感悟
- 社説 20150106 安倍政権の課題 「強い経済」を諸改革の基盤に
- 正则练习
- 使用POI 3.10.1读取 Excel 2007格式
- 正式踏上前端之路
- 实现Burt Beckwith式GORM的性能-没有集合
- 企业应用通用架构图
- 活用设计模式:如何避免一连串的if else
- 让dedecms站点内容自动更新到微博
- github初尝试
- Libgdx 1.5.2发布
- 供应链金融:互联网金融又见新玩法