⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readfmt.java

📁 关于生成EXCEL文件的封装类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.zero.GenPrint;

import java.io.IOException;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.XmlDocument;
import com.zero.exception.*;

/**
 * <p>Title: pubjar</p>
 * <p>Description: Read generate web print format file </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author zero
 * @version 1.0
 */

public class ReadFmt
{
	private PrintFmtBean pfb;
	private Map map;
	
	public ReadFmt() {}
	
	public PrintFmtBean getPrintFmt( String fmtname ) throws ZAppException
	{
		try
		{
			readXMLFile( fmtname );
		}
		catch ( Exception el )
		{
			el.printStackTrace();
			throw new ZAppException("读取打印格式文件错误;"+el.getMessage());
		}
		
		return pfb;
	}
	
	private void readXMLFile(String inFile) throws Exception
	{
		//为解析XML作准备,创建DocumentBuilderFactory实例,指定DocumentBuilder
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try
		{
			db = dbf.newDocumentBuilder();
		}
		catch (ParserConfigurationException pce)
		{
			pce.printStackTrace();
			throw new ZAppException("建DocumentBuilderFactory实例错误: " + pce); //出异常时输出异常信息,然后退出,下同
		}
		
		Document doc = null;
		try
		{
			doc = db.parse("fmt\\"+inFile);
		}
		catch (DOMException dom)
		{
			dom.printStackTrace();
			throw new ZAppException("DOMError: " + dom.getMessage());
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace();
			throw new ZAppException("IOException Error: " + ioe.getMessage());
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			throw new ZAppException("Exception Error: " + e.getMessage());
		}
		//下面是解析XML的全过程,先取根元素
		Element root = doc.getDocumentElement();
		//取"Addr"元素列表
		NodeList addrs = root.getElementsByTagName("print");

		get_fmtvalue( addrs );
	}
	
	private void get_fmtvalue( NodeList addrs ) throws ZAppException
	{
		String stmp = "";
		int inum;
		
		inum = addrs.getLength();
		pfb = new PrintFmtBean( inum );
		
		for (int i = 0; i < inum; i++)
		{
			//依次取每个"print"元素
			Element addr = (Element) addrs.item(i);
			map = new LinkedHashMap();

			get_table_parment( addr, i );
			get_foreword( addr );
			get_title( addr );
			get_head( addr );
			get_tr( addr );
			get_list( addr );
			get_body( addr );
			get_totle( addr );
			get_bottom( addr );
			
			
			if( !pfb.list.add( map ) )
				throw new ZAppException("增加格式元素错误。");
			
		}
		
		pfb.set();
	}
	
	private void get_table_parment( Element addr, int offset ) throws ZAppException
	{
		String str = null;
		NodeList width = addr.getElementsByTagName("width");
		if( width.getLength() > 0 )
		{
			Element e = (Element)width.item(0);
			Text t = (Text)e.getFirstChild();
			str = t.getNodeValue();
		}
		if( null == str || "".equals( str ) )
			str = "650";
		pfb.width[offset] = str;
		
		str = null;
		NodeList border = addr.getElementsByTagName("border");
		if( border.getLength() > 0 )
		{
			Element e = (Element)border.item(0);
			Text t = (Text)e.getFirstChild();
			str = t.getNodeValue();
		}
		if( null == str || "".equals( str ) )
			str = "0";
		pfb.border[offset] = str;
		
		str = null;		
		NodeList bordercolor = addr.getElementsByTagName("bordercolor");
		if( bordercolor.getLength() > 0 )
		{
			Element e = (Element)bordercolor.item(0);
			Text t = (Text)e.getFirstChild();
			str= t.getNodeValue();
		}
		if( null == str || "".equals( str ) )
			str = "#000000";
		pfb.bordercolor[offset] = str;
		
		str = null;		
		NodeList cellpadding = addr.getElementsByTagName("cellpadding");
		if( cellpadding.getLength() > 0 )
		{
			Element e = (Element)cellpadding.item(0);
			Text t = (Text)e.getFirstChild();
			str= t.getNodeValue();
		}
		if( null == str || "".equals( str ) )
			str = "0";
		pfb.cellpadding[offset] = str;
		
		str = null;		
		NodeList cellspacing = addr.getElementsByTagName("cellspacing");
		if( cellspacing.getLength() > 0 )
		{
			Element e = (Element)cellspacing.item(0);
			Text t = (Text)e.getFirstChild();
			str= t.getNodeValue();
		}
		if( null == str || "".equals( str ) )
			str = "0";
		pfb.cellspacing[offset] = str;
		
		str = null;		
		NodeList rules = addr.getElementsByTagName("rules");
		if( rules.getLength() > 0 )
		{
			Element e = (Element)rules.item(0);
			Text t = (Text)e.getFirstChild();
			str= t.getNodeValue();
		}
		if( null == str || "".equals( str ) )
			str = "";
		pfb.rules[offset] = str;
		
		str = null;		
		NodeList frame = addr.getElementsByTagName("frame");
		if( frame.getLength() > 0 )
		{
			Element e = (Element)frame.item(0);
			Text t = (Text)e.getFirstChild();
			str= t.getNodeValue();
		}
		if( null == str || "".equals( str ) )
			str = "";
		pfb.frame[offset] = str;
	}
	
	private void get_foreword( Element addr ) throws ZAppException
	{
		String stmp = "";
		
		map.put( "foreword_value", new Vector() );
		map.put( "foreword_align", new Vector() );
		map.put( "foreword_size", new Vector() );
		map.put( "foreword_font", new Vector() );
		map.put( "foreword_local_x", new Vector() );
		map.put( "foreword_local_y", new Vector() );
		map.put( "foreword_height", new Vector() );
		map.put( "foreword_datefmt", new Vector() );
		
		NodeList foreword = addr.getElementsByTagName("foreword");
		for ( int j=0; j<foreword.getLength(); j++ )
		{
			Element e = (Element) foreword.item(j);
			Text t = (Text)e.getFirstChild();
			
			try
			{
				stmp = t.getNodeValue();
			}
			catch( NullPointerException ne )
			{
				stmp = e.getAttribute( "value" );
			}
			catch( Exception ex )
			{
				stmp = "&nbsp;";
			}
			
			if( stmp != null || !"".equals( stmp ) )
			{
				((Vector)map.get( "foreword_value" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "foreword_value" ) ).add( "&nbsp;" );
			}
			stmp = e.getAttribute( "align" );
			if( stmp != null )
			{
				((Vector)map.get( "foreword_align" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "foreword_align" ) ).add( "" );
			}
			stmp = e.getAttribute( "size" );
			if( stmp != null || !"".equals( stmp ) )
			{
				((Vector)map.get( "foreword_size" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "foreword_size" ) ).add( "" );
			}
			stmp = e.getAttribute( "font" );
			if( stmp != null )
			{
				((Vector)map.get( "foreword_font" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "foreword_font" ) ).add( "" );
			}
			stmp = e.getAttribute( "local_x" );
			if( stmp != null )
			{
				((Vector)map.get( "foreword_local_x" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "foreword_local_x" ) ).add( "" );
			}
			stmp = e.getAttribute( "local_y" );
			if( stmp != null )
			{
				((Vector)map.get( "foreword_local_y" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "foreword_local_y" ) ).add( "" );
			}
			stmp = e.getAttribute( "height" );
			if( null != stmp )
			{
				((Vector)map.get( "foreword_height" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "foreword_height" ) ).add( "" );
			}
			stmp = e.getAttribute( "datefmt" );
			if( null != stmp || !"".equals( stmp ) )
			{
				((Vector)map.get( "foreword_datefmt" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "foreword_datefmt" ) ).add( "yyyy-MM-dd" );
			}
		}
	}
	
	private void get_title( Element addr ) throws ZAppException
	{
		String stmp = "";
		
		map.put( "title_value", new Vector() );
		map.put( "title_align", new Vector() );
		map.put( "title_size", new Vector() );
		map.put( "title_font", new Vector() );
		map.put( "title_local_x", new Vector() );
		map.put( "title_local_y", new Vector() );
		map.put( "title_height", new Vector() );
		map.put( "title_datefmt", new Vector() );
		
		NodeList code = addr.getElementsByTagName("title");
		for ( int j=0; j<code.getLength(); j++ )
		{
			Element e = (Element) code.item(j);
			
			stmp = e.getAttribute( "value" );
			if( stmp != null || !"".equals( stmp ) )
			{
				((Vector)map.get( "title_value" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "title_value" ) ).add( "&nbsp;" );
			}
			stmp = e.getAttribute( "align" );
			if( stmp != null )
			{
				((Vector)map.get( "title_align" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "title_align" ) ).add( "" );
			}
			stmp = e.getAttribute( "size" );
			if( stmp != null || !"".equals( stmp ) )
			{
				((Vector)map.get( "title_size" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "title_size" ) ).add( "" );
			}
			stmp = e.getAttribute( "font" );
			if( stmp != null )
			{
				((Vector)map.get( "title_font" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "title_font" ) ).add( "" );
			}
			stmp = e.getAttribute( "local_x" );
			if( stmp != null )
			{
				((Vector)map.get( "title_local_x" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "title_local_x" ) ).add( "" );
			}
			stmp = e.getAttribute( "local_y" );
			if( stmp != null )
			{
				((Vector)map.get( "title_local_y" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "title_local_y" ) ).add( "" );
			}
			stmp = e.getAttribute( "height" );
			if( null != stmp )
			{
				((Vector)map.get( "title_height" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "title_height" ) ).add( "" );
			}
			stmp = e.getAttribute( "datefmt" );
			if( null != stmp || !"".equals( stmp ) )
			{
				((Vector)map.get( "title_datefmt" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "title_datefmt" ) ).add( "yyyy-MM-dd" );
			}
		}
	}
	
	private void get_head( Element addr )
	{
		String stmp = "";
		
		map.put( "head_value", new Vector() );
		map.put( "head_align", new Vector() );
		map.put( "head_size", new Vector() );
		map.put( "head_font", new Vector() );
		map.put( "head_local_x", new Vector() );
		map.put( "head_local_y", new Vector() );
		map.put( "head_height", new Vector() );
		map.put( "head_width", new Vector() );
		map.put( "head_colspan", new Vector() );
		map.put( "head_rowspan", new Vector() );
		map.put( "head_rownum", new Vector() );
		
		NodeList head = addr.getElementsByTagName("head");
		for ( int j=0; j<head.getLength(); j++ )
		{
			Element e = (Element) head.item(j);
			Text t = (Text) e.getFirstChild();
			
			stmp = t.getNodeValue();
			if( null != stmp || !"".equals( stmp ) )
			{
				((Vector)map.get( "head_value" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_value" ) ).add( "&nbsp;" );
			}
			stmp = e.getAttribute( "align" );
			if( null != stmp )
			{
				((Vector)map.get( "head_align" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_align" ) ).add( "" );
			}
			stmp = e.getAttribute( "size" );
			if( null != stmp )
			{
				((Vector)map.get( "head_size" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_size" ) ).add( "" );
			}
			stmp = e.getAttribute( "font" );
			if( null != stmp )
			{
				((Vector)map.get( "head_font" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_font" ) ).add( "" );
			}
			stmp = e.getAttribute( "local_x" );
			if( null != stmp )
			{
				((Vector)map.get( "head_local_x" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_local_x" ) ).add( "" );
			}
			stmp = e.getAttribute( "local_y" );
			if( null != stmp )
			{
				((Vector)map.get( "head_local_y" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_local_y" ) ).add( "" );
			}
			stmp = e.getAttribute( "height" );
			if( null != stmp )
			{
				((Vector)map.get( "head_height" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_height" ) ).add( "" );
			}
			stmp = e.getAttribute( "width" );
			if( null != stmp )
			{
				((Vector)map.get( "head_width" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_width" ) ).add( "" );
			}
			stmp = e.getAttribute( "colspan" );
			if( null != stmp )
			{
				((Vector)map.get( "head_colspan" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_colspan" ) ).add( "" );
			}
			stmp = e.getAttribute( "rowspan" );
			if( null != stmp )
			{
				((Vector)map.get( "head_rowspan" ) ).add( stmp );
			}
			else
			{
				((Vector)map.get( "head_rowspan" ) ).add( "" );
			}
			stmp = e.getAttribute( "rownum" );
			if( null != stmp )
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -