读取Excel文档的内容工具类
来源:互联网 发布:c#foreach遍历数组 编辑:程序博客网 时间:2024/05/29 08:45
import java.io.ByteArrayInputStream;import java.io.IOException;import java.math.BigDecimal;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDateUtil;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class Excel2ObjectMappingUtil {public static Map<String,List<CZZbMapping>> para(Context context,String fileName,byte[] data) throws Exception{if(fileName.endsWith(".xls")|| fileName.endsWith(".xlsx")) {try {Map<String,List<CZZbMapping>> map=new HashMap<String, List<CZZbMapping>>();ByteArrayInputStream datas=new ByteArrayInputStream(data);boolean isExcel2007 = getExcelVersion(fileName);Workbook wb = (!isExcel2007)?new XSSFWorkbook(datas):new HSSFWorkbook(datas); int count = wb.getNumberOfSheets();//sheet个数 for(int i = 0; i < count; i++){ List<CZZbMapping> list=new ArrayList<CZZbMapping>(); Sheet sheet = wb.getSheetAt(i); int rowCount = sheet.getLastRowNum()+1;//行数 //计算列数int colCount = getColCount(sheet, rowCount);if(rowCount < 2 || colCount <5) {continue;} String[][] sheetData = oneSheetProcess(sheet, rowCount, colCount); for(int j=1 ;j < rowCount ;j++) { CZZbMapping zm=new CZZbMapping(); zm.setRecid(GUID.randomID());//主键 String key = sheetData[j][0]; System.out.println(key); String value = sheetData[j][0]; System.out.println("第"+j+"行第1列"+value); zm.setJqrcode(sheetData[j][0]);//jqrcode zm.setCzcode(sheetData[j][1]);//czcode zm.setMapid(GUID.tryValueOf(sheetData[j][2]));//mappingid zm.setReportname(sheetData[j][3]);//报表名称 zm.setTabName(sheetData[j][4]);//项目标识名称 list.add(zm); } String sheetName = sheet.getSheetName();//报表名称 map.put(sheetName, list); } return map;} catch (IOException e) {e.printStackTrace();}}else{System.out.println("文件名不对");}return null;}/** * 将sheet中的数据按二阶数组获取 * @param sheet * @param rowCount * @param colCount * @return */private static String[][] oneSheetProcess(Sheet sheet, int rowCount, int colCount) {String showTexts[][];showTexts = new String[rowCount][colCount];for(int r = 0;r<rowCount;r++){for(int c=0;c<colCount;c++){showTexts[r][c] = doGetShowText(sheet, r, c);}}return showTexts;}/** * 获取单元格显示的值 * @param sheet * @param row * @param col * @return */public static String doGetShowText(Sheet sheet, int row, int col) {if(row>=sheet.getFirstRowNum() && row<=sheet.getLastRowNum()){Row hssfRow = sheet.getRow(row);if(hssfRow!=null && col>=hssfRow.getFirstCellNum() && col<=hssfRow.getLastCellNum()){Cell cell = getSheetCell(sheet,row,col);return getStringCellValue(cell);}}return "";}/** * 获取sheet的单元格 * @param sheet * @param row * @param col * @return */private static Cell getSheetCell(Sheet sheet, int row, int col) {int theRow = row;int theCol = col;int num = sheet.getNumMergedRegions();for(int i=0;i<num;i++){CellRangeAddress region = sheet.getMergedRegion(i);if(col>=region.getFirstColumn() && col<=region.getLastColumn() &&row>=region.getFirstRow() && row<=region.getLastRow()){theRow = region.getFirstRow();theCol = region.getFirstColumn();break;}}Row sheetRow = sheet.getRow(theRow);Cell cell = sheetRow.getCell(theCol);return cell;}/** * 获取单元格字符值 * @param cell * @return */private static String getStringCellValue(final Cell cell) {if(cell==null)return "";switch(cell.getCellType()){case HSSFCell.CELL_TYPE_STRING:return cell.getStringCellValue();case HSSFCell.CELL_TYPE_NUMERIC:if (HSSFDateUtil.isCellDateFormatted(cell)) { Date d = cell.getDateCellValue(); DateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); return formater.format(d); } return handleNumericStr(cell.getNumericCellValue());case HSSFCell.CELL_TYPE_BLANK:return "";}return "";}/** * 将数字转换成字符 * @param numericCellValue * @return */private static String handleNumericStr(double numericCellValue) {BigDecimal bd = new BigDecimal(numericCellValue);return bd.toString();}/** * 获取列数 * @param sheet * @param rowCount * @return */private static int getColCount(Sheet sheet, int rowCount) {int colCount = 0;for(int r=0;r< rowCount;r++){Row hssfRow = sheet.getRow(r);if(hssfRow!=null){if(colCount<hssfRow.getLastCellNum()){colCount = hssfRow.getLastCellNum();}}}return colCount;}public static boolean getExcelVersion(String fileName) {if(fileName.matches("^.+\\.(?i)(xlsx)$"))return true;return false;}}
阅读全文
0 0
- 读取Excel文档的内容工具类
- 封装poi读取excel的超强工具类,支持一行代码获取excel内容
- 读取Excel的内容
- 使用NOPI读取Word、Excel文档内容
- PHP读取Excel内容的类
- 另一个PHP读取Excel内容的类
- 读取Excel工具类
- 读取excel 工具类
- 读取excel表格的内容
- c# 读取 Excel的内容
- python读取excel的内容
- vb读取excel的内容
- poi读取excel的工具类
- 读取加密的excel文档
- Java 使用最新POI Lib 将Excel转换成Txt读取Excel内容工具类
- 把excel文档中的内容读取到dataset中
- java读取word,excel和pdf文档内容
- 读取Excel工具类ExcelUtils
- SearchView的简单使用
- 阿里云双11云产品访谈 之 互联网中间件
- PHP_notes_01
- cocoa pods 的安装
- redis基本命令
- 读取Excel文档的内容工具类
- SharePoint Online 创建用户和组
- PHP_notes_02
- Power-BI 空调行业BI解决方案-品牌竞争分析
- iOS进度条 渐进色 刻度尺的小demo
- 通过VMware克隆centos7虚拟机之后,修改网络配置问题。
- 正则表达式之前后查找(Java版) 实例
- 关于电脑安装双系统的一些问题的思考和解决
- solr批量添加索引数据报错