javascript和JQuery焦点图和代码特效大全
当前最流行的开源CMS网站系统大全
当前位置:主页 > 编程开发 > JAVA技术 >

JAVA如何实现解析xls文件

来源:IT技术网编辑:香独秀发布于:2013-07-03人围观Java文件xls解析

JAVA如何实现解析xls文件,具体看如下java代码:

/**
 * 解析DBF文件

	 * @param in
	 * @return List> columns通过AMF编码之后的字符串
	 * @throws Exception
	 */
	public static String parseDbf2Map(InputStream in)throws Exception {

		DBFReader dbfreader = new DBFReader(in);
        dbfreader.setCharactersetName("utf8");

        List> columns = new ArrayList>();
        Map head = new HashMap();
        //解析DBF表头的信息
        Map dataType = new HashedMap();
        for(int i = 0; i < dbfreader.getFieldCount(); i++) {
        	DBFField field = dbfreader.getField(i);
        	byte type = field.getDataType();
        	int decimal = 0;
        	int length = 0;
        	String formatString = "";
        	if(DBFField.FIELD_TYPE_N == type || DBFField.FIELD_TYPE_F == type){
        		 decimal = field.getDecimalCount();
            	 length = field.getFieldLength();
            	 for (int j = 0; j < (length - decimal); j++) {
            		 formatString +="#";
                 }
            	 if(decimal != 0){
            		 formatString = formatString + "0.";
            	 }
            	 for (int jj = 0; jj < decimal; jj++) {
            		 formatString +="0";
                }
            	 dataType.put(""+i, formatString);
        	}
        	if(DBFField.FIELD_TYPE_D == type){
        		formatString = "yyyy/MM/dd";
        		dataType.put(""+i, formatString);
        	}

        	String name = field.getName();

        	head.put(""+i, name);
        }
        columns.add(head);
        Object[] rowObj = null;
        int k = 1;
        while ((rowObj = dbfreader.nextRecord()) != null) {
        	Map data = new HashMap();
            for (int i = 0; i < rowObj.length; i++) {
            	Object temp = rowObj[i];
            	String dataTypeFormat = (String)dataType.get(""+i);
            	if(dataTypeFormat != null){
            		if(dataTypeFormat.indexOf("yyyy") !=-1){
            			SimpleDateFormat format = new SimpleDateFormat(dataTypeFormat);
            			temp = format.format((Date)temp);
            		}else {
            			DecimalFormat format = new DecimalFormat(dataTypeFormat);
                		temp = format.format(temp);
					}
            	}
                data.put(dbfreader.getField(i).getName(), temp.toString());
            }
            columns.add(data);
            k++;
        }

    	//编码:
        AmfSerializer amfSerializer = new AmfSerializer();
        String str = amfSerializer.toAmf(columns);
		return str;
	}

 

Java,文件,xls,相关的文章
有时间的话来看看IT界的突发事件