用poi时的数据类型问题 在获得excel数据的时候,数据类型是怎么区分的..... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我用的读excel的类方法 ,你参考以下把 * 根据formatExcel文件中的格式读取dataExcel中的数据 * * @param formatExcel excel模板的指针 * @param dataExcel excle格式存放的数据 * @return */ public Map readImportdata(HSSFWorkbook formatExcel, HSSFWorkbook dataExcel) { int sheetNumber = 0; int rowNumber = 0; int cellNumber = 0; int allSheet = 0; boolean needSkipFirstRow = true; List resultList = new ArrayList(); /** * resultMap 内含有如下内容: * flag String true:正常执行,可以取结果集 false:程序执行流程错误,但没有异常 * msg String 如果程序出错,此内容保存的是出错信息。 * resultList List 如果程序正常执行,这里保存的是结果集 */ Map resultMap = new HashMap(); String flag = "true"; StringBuffer msg = new StringBuffer(""); resultMap.put("flag", flag); resultMap.put("msg", msg.toString()); if (formatExcel == null) return resultMap; if (dataExcel == null) return resultMap; initialize(); getImportColumns(formatExcel); allSheet = dataExcel.getNumberOfSheets(); for (sheetNumber = 0; sheetNumber < allSheet; sheetNumber++) {//读取所有的sheet页 HSSFSheet dataSheet = dataExcel.getSheetAt(sheetNumber); int allrows = dataSheet.getLastRowNum(); Iterator rowIt = dataSheet.rowIterator(); //如果没有内容,返回,判断是否需要跳过第一行 if (rowIt.hasNext()) { if (needSkipFirstRow) { rowIt.next(); rowNumber++; } } //循环读数据 while (rowIt.hasNext()) { HSSFRow dataRow = (HSSFRow) rowIt.next();// HSSFRow dataRow = (HSSFRow)dataSheet.getRow(currentrow); int allcells = dataRow.getLastCellNum(); short currentCol = 0;// Iterator cellIt = dataRow.cellIterator(); int columnsize = importColumns.size(); cellNumber = 0; Map oneRow = new HashMap(); for (currentCol = 0; currentCol < allcells; currentCol++) {// while(cellIt.hasNext() && currentCol <columnsize){// HSSFCell dataCell = (HSSFCell)cellIt.next(); HSSFCell dataCell = dataRow.getCell(currentCol);// currentCol = dataCell.getCellNum(); cellNumber = currentCol; Object colType = importColumns.get(currentCol); if (colType instanceof Map) { //进行字段的校验和转换 Map tempcolType = (Map) colType; String cellContent = getStringFromCell(dataCell); String allownull = (String) tempcolType.get("allownull"); String dbname = (String) tempcolType.get("dbname"); String format = (String) tempcolType.get("format"); Integer maxlength = (Integer) tempcolType.get("maxlength"); String dictionary = (String) tempcolType.get("dictionary"); System.out.println(dbname + " = " + cellContent); if (dbname == null) dbname = ""; //进行非空检验 if (allownull == null) allownull = "true"; if (allownull.equals("false")) { if (cellContent == null || cellContent.trim().equals("")) { flag = "false"; msg.append( " 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元格[" + cellContent + "]不能为空。\n"); oneRow = null; break; } } //进行maxlength校验 if (maxlength == null) { } else { if (cellContent.trim().length() > maxlength.intValue()) { flag = "false"; msg.append( " 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元内容[" + cellContent + "]太长。\n"); oneRow = null; break; } } //进行format校验 if (format == null || format.trim().equals("")) { } else { if (!checkFormat(cellContent, format)) { flag = "false"; msg.append( " 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元内容[" + cellContent + "]格式不对。\n"); oneRow = null; break; } } //进行dictionary处理 if (dictionary == null || dictionary.trim().equals("")) { oneRow.put(dbname, cellContent); } else { Object newvalue = fromContentToCode(cellContent, dictionary); oneRow.put(dbname, newvalue); } } else { //对这个cell不做处理 }// currentCol ++; cellNumber++; } if (oneRow != null && oneRow.size() > 0) resultList.add(oneRow); rowNumber++; } } resultMap.put("flag", flag); resultMap.put("msg", msg.toString()); resultMap.put("resultList", resultList); return resultMap; } 今天看到一个方法,觉得写得不错,共享一下 初学JAVA编程,看哪本书好? synchronized(obj)的确切意思? java 同步问题 求教 myeclipse如何拖拽控件? 数据库连接配置问题 java 如何调用oracle的存储过程?在线等!!!! 一个java问题,有兴趣的来! 给个“hello world"的例子程序 java Oracle jjdbc 在eclipse中编写,无法成功连接Oracle 字符串格式的日期如何转换成Date的标准格式 Timer 问题 在线=
*
* @param formatExcel excel模板的指针
* @param dataExcel excle格式存放的数据
* @return
*/
public Map readImportdata(HSSFWorkbook formatExcel, HSSFWorkbook dataExcel) {
int sheetNumber = 0;
int rowNumber = 0;
int cellNumber = 0;
int allSheet = 0;
boolean needSkipFirstRow = true;
List resultList = new ArrayList(); /**
* resultMap 内含有如下内容:
* flag String true:正常执行,可以取结果集 false:程序执行流程错误,但没有异常
* msg String 如果程序出错,此内容保存的是出错信息。
* resultList List 如果程序正常执行,这里保存的是结果集
*/
Map resultMap = new HashMap();
String flag = "true";
StringBuffer msg = new StringBuffer("");
resultMap.put("flag", flag);
resultMap.put("msg", msg.toString());
if (formatExcel == null) return resultMap;
if (dataExcel == null) return resultMap; initialize();
getImportColumns(formatExcel);
allSheet = dataExcel.getNumberOfSheets(); for (sheetNumber = 0; sheetNumber < allSheet; sheetNumber++) {//读取所有的sheet页
HSSFSheet dataSheet = dataExcel.getSheetAt(sheetNumber);
int allrows = dataSheet.getLastRowNum();
Iterator rowIt = dataSheet.rowIterator(); //如果没有内容,返回,判断是否需要跳过第一行
if (rowIt.hasNext()) {
if (needSkipFirstRow) {
rowIt.next();
rowNumber++;
}
} //循环读数据
while (rowIt.hasNext()) {
HSSFRow dataRow = (HSSFRow) rowIt.next();
// HSSFRow dataRow = (HSSFRow)dataSheet.getRow(currentrow);
int allcells = dataRow.getLastCellNum();
short currentCol = 0;
// Iterator cellIt = dataRow.cellIterator();
int columnsize = importColumns.size();
cellNumber = 0;
Map oneRow = new HashMap();
for (currentCol = 0; currentCol < allcells; currentCol++) {
// while(cellIt.hasNext() && currentCol <columnsize){
// HSSFCell dataCell = (HSSFCell)cellIt.next();
HSSFCell dataCell = dataRow.getCell(currentCol);
// currentCol = dataCell.getCellNum();
cellNumber = currentCol;
Object colType = importColumns.get(currentCol);
if (colType instanceof Map) {
//进行字段的校验和转换
Map tempcolType = (Map) colType;
String cellContent = getStringFromCell(dataCell); String allownull = (String) tempcolType.get("allownull");
String dbname = (String) tempcolType.get("dbname");
String format = (String) tempcolType.get("format");
Integer maxlength = (Integer) tempcolType.get("maxlength");
String dictionary = (String) tempcolType.get("dictionary");
System.out.println(dbname + " = " + cellContent);
if (dbname == null) dbname = "";
//进行非空检验
if (allownull == null) allownull = "true";
if (allownull.equals("false")) {
if (cellContent == null || cellContent.trim().equals("")) {
flag = "false";
msg.append(
" 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元格[" +
cellContent +
"]不能为空。\n");
oneRow = null;
break;
}
}
//进行maxlength校验
if (maxlength == null) {
} else {
if (cellContent.trim().length() > maxlength.intValue()) {
flag = "false";
msg.append(
" 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元内容[" +
cellContent +
"]太长。\n");
oneRow = null;
break;
}
}
//进行format校验
if (format == null || format.trim().equals("")) {
} else {
if (!checkFormat(cellContent, format)) {
flag = "false";
msg.append(
" 在第" + (sheetNumber + 1) + "页第" + (rowNumber + 1) + "行第" + (cellNumber + 1) + "单元内容[" +
cellContent +
"]格式不对。\n");
oneRow = null;
break;
}
}
//进行dictionary处理
if (dictionary == null || dictionary.trim().equals("")) {
oneRow.put(dbname, cellContent);
} else {
Object newvalue = fromContentToCode(cellContent, dictionary);
oneRow.put(dbname, newvalue);
}
} else {
//对这个cell不做处理 }
// currentCol ++;
cellNumber++;
} if (oneRow != null && oneRow.size() > 0) resultList.add(oneRow);
rowNumber++; }
}
resultMap.put("flag", flag);
resultMap.put("msg", msg.toString());
resultMap.put("resultList", resultList);
return resultMap;
}