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());