ADF 的Excel导入方法总结 2
来源:互联网 发布:功夫梦 知乎 编辑:程序博客网 时间:2024/04/28 09:52
FileInputStream excelFile = new FileInputStream(excelFilePath); //excelPath为服务器目标路径 POIFSFileSystem excelObj = new POIFSFileSystem(excelFile); //通过POIFSFileSystem读取Excel档
接上文,现在要取出上文数据库表1中的列名信息
importColInfoVVO.setRangeSize(-1); importColInfoVVO.setRangeStart(0); importColInfoVVO.setMaxFetchSize(-1); importColInfoVVO.setNamedWhereClauseParam("vtranscode", this.getVtrancode()); importColInfoVVO.executeQuery(); RowSetIterator rst = importColInfoVVO.createRowSetIterator("excelColumnInfo"); Row r = null; Key key = null; Row curRow = null; int i = 0; while (rst.hasNext()) { r = rst.next(); key = r.getKey(); curRow = importColInfoVVO.getRow(key); if (curRow != null) { importExcelDatas.add(assembleColumnInfo(curRow, i++)); } } rst.closeRowSetIterator();
importColInfoVVO即上文表1的VO,查询出属于当前用例事务码的行数据。
然后对查询出来的结果进行迭代,
比如现在读取第一行数据,事务码统一为一个,字段名是A,字段类型为varchar,标签名为ATest
把这些信息进行重新整理存放到一个importExcelDatas的ArrayList的数据类型中。整理方法为
assembleColumnInfo()
{
Object vfldnameObj = curRow.getAttribute("Vfldname");
Object vttnameObj = curRow.getAttribute("Vttname");
Object vfldtypeObj = curRow.getAttribute("Vfldtype");
ImportColumnAttribute icAttr = new ImportColumnAttribute();
icAttr.setColType(vfldtypeObj == null ? "char" : String.valueOf(vfldtypeObj));
icAttr.setName(vfldnameObj == null ? "Col"+i : QMUtils.convertColumeNameToVOAttributes(String.valueOf(vfldnameObj)));
icAttr.setColumnLabel(vttnameObj == null ? "Col"+i : String.valueOf(vttnameObj));
return icAttr;
}
并把这个数据类型添加进importExcelDatas中。迭代的结果为 importExcelDatas包括Excel表中的行数个数组,数组中的每个元素都包含三项,列名,列标签,列数据类型。
之后开始解析EXCEL中的数据,分excel03和excel07两种解析方式。
通过 POIFSFileSystem类读取excel
FileInputStream excelFile = new FileInputStream(excelFilePath); //excelPath为服务器目标路径 POIFSFileSystem excelObj = new POIFSFileSystem(excelFile); //通过POIFSFileSystem读取Excel档
HSSFWorkbook wb = new HSSFWorkbook(excelObj); int pages = wb.getNumberOfSheets(); //可见的Sheet页个数 int key = 0; for (int i = 0; i < pages; i++) { HSSFSheet sheet = wb.getSheetAt(i); if (sheet.getLastRowNum() == 0) { continue; //Sheet页空或只有1行数据跳出当前循环 } else { Hashtable hst = parseExcelSheet2003Title(sheet,startNum); //0是Sheet的标题行 hsm.put(key, hst); key++; } }
其中parseExcelSheet2003Title为解析excel标题的信息
int rows = sheet.getLastRowNum(); if (rows < startRowNum) { startRowNum = rows; //默认 } HSSFRow row = null; //行数据 HSSFCell cell = null; //列数据 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化日期字符串 Hashtable hst = new Hashtable(); //i=0是标题行 if (startRowNum > 0) { row = sheet.getRow(startRowNum - 1); } if (row == null) { return hst; } int cols = row.getLastCellNum(); for (int j = 0; j < cols; j++) { cell = row.getCell(j); if (cell == null || cell.equals("")) { cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING); cell.setCellValue(""); } int type = cell.getCellType(); //判断单元格数据类型 if (type == HSSFCell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { hst.put(j, sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()))); } else { hst.put(j, cell.getNumericCellValue()); } } else if (type == HSSFCell.CELL_TYPE_STRING) { hst.put(j, String.valueOf(cell.getStringCellValue())); } else if (type == XSSFCell.CELL_TYPE_BOOLEAN) { hst.put(j, cell.getBooleanCellValue()); } else if (type == HSSFCell.CELL_TYPE_FORMULA) { hst.put(j, String.valueOf(cell.getCellFormula())); } else if (type == HSSFCell.CELL_TYPE_ERROR) { hst.put(j, String.valueOf(cell.getErrorCellValue())); } else { hst.put(j,"#VALUE");//未知数据类型 //throw new QMLMPException("捕获异常:第["+i+"]行["+j+"]列单元格数据定义有误"); } } return hst;
解析07的列字段的时候只是列数据类型稍有不同 其他大体相同。
之后如果要求严禁性的话,则还要把EXCEL的列头再和数据结构中的信息做一次比较 比如trim()等等
然后就可以导入到数据库表3中
Row newRow = null; Object exlobj = null; for (int i = 0; i < exlObj.size(); i++) { newRow = excelImportAPPVO.createRow(); newRow.setNewRowState(Row.STATUS_INITIALIZED); for (int j = 0; j < importExcelDatas.size(); j++) { exlobj = exlObj.get(i).get(j); newRow.setAttribute(QMUtils.convertColumeNameToVOAttributes(importExcelDatas.get(j).getName()), exlobj); } newRow.setAttribute("Id", getId(db)); newRow.setAttribute("Vtranscode", this.getVtrancode()); newRow.setAttribute("Noperator", Long.parseLong(info.getUserId())); newRow.setAttribute("Doperatedate", new Date()); newRow.setAttribute("Dtstamp", new Date());
- ADF 的Excel导入方法总结 2
- ADF 的Excel导入方法总结 1
- ADF 中使用Sequence的方法总结
- ADF 中使用Sequence的方法总结
- C#导入Excel|读取Excel方法总结
- adf常用方法总结
- adf常用方法总结
- excel数据导入到oracle数据库的方法总结
- 从SQL导入到EXCEL方法总结
- C#Excel数据导入导出方法总结
- Excel数据导入导DataTable 方法总结
- dataset导入excel的方法
- 导入导出Excel的方法
- 导入EXCEL表的方法
- .Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法)
- .Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法)
- excel的导出导入的总结
- 高效的excel导入数据库的方法
- 免费空间
- 日语动词
- 工作三年做个mark
- c# WinForm中查找控件,动态赋值
- java.lang.UnsupportedClassVersionError: Bad version number in .class file
- ADF 的Excel导入方法总结 2
- 为程序员量身定制的12个目标
- Oracle 表中order by rowid是不是按照记录插入时间排序?
- mysql数据库中命令行下常用命令的操作(增、删、改、查)和数据类型
- 【struts2】五大核心jar包 和 作用
- Android获取ROOT权限方法汇总
- FilterInputStream-001
- cygwin改目录
- 欠自己的【2011总结】~