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

📄 excelexporter.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Created on 2005/1/11
 *
 * $Author$
 * $Date$
 * $Revision$ 
 */
package eti.bi.alphaminer.operation.result.export;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JTable;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Transform.Filter.MiningFileFilter;

import eti.bi.alphaminer.operation.result.datamodel.DataGridModel;
import eti.bi.alphaminer.operation.result.datamodel.SortingDataGridModel;
import eti.bi.common.data.MissingValue;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;

/**
 * @author kelvinj
 *  
 * Add function to export JTable or JTableLists to EXCEL file. 
 * By TWang Jan 21, 2005.
 */
public class ExcelExporter extends FileExporter{

	public final static String DEFAULT_SHEET_NAME = "Sheet 1";
	public final static int SOURCE_TYPE_MININGDATA = 0;
	public final static int SOURCE_TYPE_ARRAY = 1;
	public final static int SOURCE_TYPE_TABLE = 2;
	
	public final  static MiningFileFilter FILTER;
	public final  static String EXTENSION = "xls"; 
	public final  static String DESCRIPTION = "Excel Files"; 

	public static File DEFAULT_FILE = new File("book1.xls");
	//public static MiningFileFilter FILTER;
	
	private JTable m_JTable = null;
	private MiningStoredData m_MiningStoredData = null;
	private HSSFWorkbook m_Workbook = null;
	private HSSFSheet m_Sheet = null;
	private int m_SourceType = -1;
	
	// used to remember the row number when writting multiple JTables
	private int m_RowCounter = 0;
	
	static
	{
		FILTER = new MiningFileFilter();
		FILTER.addExtension(EXTENSION);
		FILTER.setDescription(DESCRIPTION);
	}

	
	/* ExcelExporter Constructer*/
	public ExcelExporter(MiningStoredData a_MiningStoredData, File a_File, boolean a_EnforceFileName)
	{
		m_SourceType = SOURCE_TYPE_MININGDATA;
		m_Workbook= new HSSFWorkbook(); 
		m_Sheet = m_Workbook.createSheet(DEFAULT_SHEET_NAME);
		m_MiningStoredData = a_MiningStoredData;
    	if (a_EnforceFileName && !FILTER.accept(a_File))
	  	{
	  		String oldPath = a_File.getAbsolutePath();
	  		String newPath = oldPath.concat(".");
	  		newPath = newPath.concat(EXTENSION);
	  		File newFile = new File(newPath);
	  		m_ExportFile = newFile;
	  	}else
	  	{
	  		m_ExportFile = a_File;
	  	}
	}
	
	
	/**
	 * Export a JTable to a File. JTable's getModel() DefaultTableModel 
	 * should be instance of eti.bi.alphaminer.ui.operatorresult.DataGridModel
	 * 
	 * By TWang. Jan 21, 2005.
	 * 
	 * @param a_JTable
	 * @param a_File
	 * @param a_EnforceFileName
	 */  
	public ExcelExporter(JTable a_JTable, File a_File, boolean a_EnforceFileName)
	{
		m_SourceType = SOURCE_TYPE_TABLE;
		m_Workbook= new HSSFWorkbook(); 
		m_Sheet = m_Workbook.createSheet(DEFAULT_SHEET_NAME);
		m_JTable = a_JTable;
		 
    	if (a_EnforceFileName && !FILTER.accept(a_File))
	  	{
	  		String oldPath = a_File.getAbsolutePath();
	  		String newPath = oldPath.concat(".");
	  		newPath = newPath.concat(EXTENSION);
	  		File newFile = new File(newPath);
	  		m_ExportFile = newFile;
	  	}else
	  	{
	  		m_ExportFile = a_File;
	  	}
	}
	
	/**
	 * Export a JTable List to a File. JTable's getModel() DefaultTableModel 
	 * should be instance of eti.bi.alphaminer.ui.operatorresult.DataGridModel
	 * 
	 * By TWang. Jan 28, 2005.
	 *  
	 * @param a_File
	 * @param a_EnforceFileName
	 */  
	public ExcelExporter(File a_File, boolean a_EnforceFileName)
	{
		m_SourceType = SOURCE_TYPE_TABLE;
		m_Workbook= new HSSFWorkbook(); 
		m_Sheet = m_Workbook.createSheet(DEFAULT_SHEET_NAME); 
		 
    	if (a_EnforceFileName && !FILTER.accept(a_File))
	  	{
	  		String oldPath = a_File.getAbsolutePath();
	  		String newPath = oldPath.concat(".");
	  		newPath = newPath.concat(EXTENSION);
	  		File newFile = new File(newPath);
	  		m_ExportFile = newFile;
	  	}else
	  	{
	  		m_ExportFile = a_File;
	  	}
	}
	
	
	
	/* (non-Javadoc)
	 * @see eti.bi.alphaminer.common.util.FileExporter#export()
	 */
	public void export() throws SysException, AppException {
		if (m_ExportFile != null && m_ExportFile.exists())
		{
			if (!m_ExportFile.delete())
			{
				throw new AppException("File '"+m_ExportFile.getAbsolutePath()+"' is already open.");
			}
		}
		switch(m_SourceType)
		{ 
			case SOURCE_TYPE_MININGDATA:
				exportFromData();
				break;
			case SOURCE_TYPE_ARRAY:
				exportFromArray();
				break;
			case SOURCE_TYPE_TABLE:
				exportFromTable();
				break; 
			default:
				break;
		}
	}
	
	/**
	 * Export a set of JTables
	 * TWang. Jan 28 2005. 
	 */
	public void exportMultiTables(ArrayList a_JTableLists, ArrayList a_JTableNameLists) throws SysException, AppException {
		if (m_ExportFile != null && m_ExportFile.exists()){
			if (!m_ExportFile.delete()) {
				throw new AppException("File '"+m_ExportFile.getAbsolutePath()+"' is already open.");
			}
		}
		if (m_SourceType != SOURCE_TYPE_TABLE || a_JTableLists == null || a_JTableNameLists == null) {
			throw new AppException("JTableLists/JTableNameLists is NULL or the input is not of correct type");
		}		
		if ( a_JTableNameLists.size()!= a_JTableLists.size()) {
			throw new AppException("The size of JTableNameLists doenot equal to the sizeof JTableLists");
		}
		
		m_RowCounter = 0;
		for (int i=0; i<a_JTableLists.size(); i++){
			Object object = a_JTableLists.get(i);
			if ( !(object instanceof JTable) ) {
				throw new AppException("The input is not of type JTable");
			}
			m_JTable = (JTable) a_JTableLists.get(i);
			prepareWorkbookFromTable(m_JTable, (String)a_JTableNameLists.get(i));
			m_RowCounter += 2; //create next JTable after two lines.
		}
			try { 
				FileOutputStream fileOut = new FileOutputStream(m_ExportFile);
				m_Workbook.write(fileOut);
				fileOut.close();
			}catch (FileNotFoundException e) {
				new AppException("Could not find file " + m_ExportFile.getAbsolutePath());
			} catch (IOException e1) {
				new AppException("Unable to access file " + m_ExportFile.getAbsolutePath());
			} 
	}
	
	/**
	 * Export only one JTable.
	 * @throws SysException
	 * @throws AppException
	 */
	private void exportFromTable() throws SysException, AppException
	{
		if (m_JTable == null)
		{
			throw new SysException("[ExcelExporter].[exportFromData].JTable is null");
		}

	    try {
	    	prepareWorkbookFromTable(m_JTable, "");
	    	
		    FileOutputStream fileOut = new FileOutputStream(m_ExportFile);
			m_Workbook.write(fileOut);
			fileOut.close();
		} catch (FileNotFoundException e) {
			new AppException("Could not find file " + m_ExportFile.getAbsolutePath());
		} catch (IOException e1) {
			new AppException("Unable to access file " + m_ExportFile.getAbsolutePath());
		}
	}
	
	private void exportFromData() throws SysException, AppException
	{
		if (m_MiningStoredData==null)

⌨️ 快捷键说明

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