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

📄 excelexporter.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		{
			throw new SysException("[ExcelExporter].[exportFromData].MiningSoredData is null");
		}

	    try {
	    	prepareWorkbook();
	    	
		    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 prepareWorkbook() throws SysException
	{
		switch(m_SourceType)
		{ 
			case SOURCE_TYPE_MININGDATA:
				prepareWorkbookFromData();
				break;
			case SOURCE_TYPE_ARRAY:
				prepareWorkbookFromData();
				break;
			default:
				break;
		}
	
	}
	
	/**
	 *  Write the JTable values into HSSFSheet
	 *  By Twang. Jan. 21, 2005.
	 */ 
	private void prepareWorkbookFromTable(JTable a_JTable, String a_TableName)
			throws SysException {
		m_JTable = a_JTable;
		if (!(m_JTable.getModel() instanceof DataGridModel)
				&& !(m_JTable.getModel() instanceof SortingDataGridModel)) {
			throw new SysException(
					"[ExcelExporter].[exportFromData].JTable's DataGridModel must be of DataGridModel/SortingDataGridModel.");
		}

		DataGridModel dataGridModel = (DataGridModel) m_JTable.getModel();

		int coloumNum = dataGridModel.getColumnCount();
		if (coloumNum == 0) {
			// Let it create an empty work sheet
			return;
		}
		HSSFRow aMetaDataRow = null;

		// create the table name if it is not empty
		if (a_TableName != "") {
			aMetaDataRow = m_Sheet.createRow((short) m_RowCounter++);
			aMetaDataRow.createCell((short) 0).setCellValue(a_TableName);
		}

		// Create the first row and export meta data
		aMetaDataRow = m_Sheet.createRow((short) m_RowCounter++);
		int[] attrType = new int[coloumNum];

		for (int i = 0; i < coloumNum; i++) {
			String attrName = dataGridModel.getColumnName(i);
			HSSFCell cell = aMetaDataRow.createCell((short) i);
			cell.setCellValue(attrName);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);

			if ((dataGridModel.getColumnClass(i).equals(Short.class))
					|| (dataGridModel.getColumnClass(i).equals(Integer.class))
					|| (dataGridModel.getColumnClass(i).equals(Long.class))
					|| (dataGridModel.getColumnClass(i).equals(Double.class))
					|| (dataGridModel.getColumnClass(i).equals(Float.class))) {
				attrType[i] = HSSFCell.CELL_TYPE_NUMERIC;

			} else if (dataGridModel.getColumnClass(i).equals(String.class)) {
				attrType[i] = HSSFCell.CELL_TYPE_STRING;
			} else if (dataGridModel.getColumnClass(i).equals(Boolean.class)) {
				attrType[i] = HSSFCell.CELL_TYPE_STRING;
			}
		}

		int recordsNum = dataGridModel.getRowCount();

		for (int i = 0; i < recordsNum; i++) {
			HSSFRow aDataRow = m_Sheet.createRow((short) (m_RowCounter++));
			for (int j = 0; j < coloumNum; j++) {
				HSSFCell cell = aDataRow.createCell((short) j);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellType(attrType[j]); 
			    if (MissingValue.isMissingValue(dataGridModel.getValueAt(i, j))) {
			    	cell.setCellValue(""); 
			    } else if (attrType[j] == HSSFCell.CELL_TYPE_NUMERIC) {
					double value = 0;
					Object oriVal = dataGridModel.getValueAt(i, j);

					if (dataGridModel.getColumnClass(j).equals(Short.class)
							&& oriVal != null) {
						value = ((Short) oriVal).doubleValue();
					} else if (dataGridModel.getColumnClass(j).equals(
							Long.class)
							&& oriVal != null) {
						value = ((Long) oriVal).doubleValue();

					} else if (dataGridModel.getColumnClass(j).equals(
							Integer.class)
							&& oriVal != null) {
						value = ((Integer) oriVal).doubleValue();

					} else if (dataGridModel.getColumnClass(j).equals(
							Double.class)
							&& oriVal != null) {
						value = ((Double) oriVal).doubleValue();

					} else if (dataGridModel.getColumnClass(j).equals(
							Float.class)
							&& oriVal != null) {
						value = ((Float) oriVal).doubleValue();

					}
					cell.setCellValue(value);

				} else {
					String value = ""; //the default value is NOTHING.
					Object oriVal = dataGridModel.getValueAt(i, j);
					if (dataGridModel.getColumnClass(j).equals(String.class)
							&& oriVal != null) {
						value = oriVal.toString();
					} else if (dataGridModel.getColumnClass(j).equals(
							String.class)
							&& oriVal != null) {
						value = ((Boolean) oriVal).toString();
					}
					cell.setCellType(attrType[j]);
					cell.setCellValue(value);
				}
			}
		}
	}
	
	private void prepareWorkbookFromData() throws SysException
	{
		if (m_MiningStoredData==null)
		{
			throw new SysException("[ExcelExporter].[prepareWorkbookFromData].MiningStoredData is null");
		}
		
		MiningDataSpecification aMetaData = null;
		try
		{
			aMetaData = m_MiningStoredData.getMetaData();
		}catch (MiningException e)
		{
			throw new SysException("[ExcelExporter].[prepareWorkbookFromData].Invalid MetaData");
		}
		
		if (aMetaData==null)
		{
			throw new SysException("[ExcelExporter].[prepareWorkbookFromData].MetaData is null");
		}

		MiningAttribute[] miningAttributes = aMetaData.getAttributesArray();
		int m_Column = aMetaData.getAttributesNumber();
		if (m_Column==0)
		{
			// Let it create an empty work sheet
			return;
		}
		
		// Create the first row and export meta data
	    HSSFRow aMetaDataRow = m_Sheet.createRow((short)0);
	    int[] attrType = new int[m_Column];
	    int[] dataType = new int[m_Column];
		for (int i=0; i<m_Column; i++)
		{
			String attrName = miningAttributes[i].getName();
			HSSFCell cell = aMetaDataRow.createCell((short)i);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(attrName);
			
			if (miningAttributes[i] instanceof NumericAttribute)
			{
				attrType[i] = HSSFCell.CELL_TYPE_NUMERIC;
				dataType[i] = ((NumericAttribute) miningAttributes[i]).getDataType();
			} else if (miningAttributes[i] instanceof CategoricalAttribute)
			{
				attrType[i] = HSSFCell.CELL_TYPE_STRING;
				dataType[i] = ((CategoricalAttribute) miningAttributes[i]).getDataType();
			} else
			{
				attrType[i] = HSSFCell.CELL_TYPE_STRING;
				dataType[i] = CategoricalAttribute.STRING;
			}
		}

		// Prepare data contents
		ArrayList list = m_MiningStoredData.getMiningVectors();
		Object[][] content = new Object[list.size()][m_Column];			
		MiningVector vec = null;
		for (int i=0; i<list.size(); i++)
		{
			vec = (MiningVector) list.get(i);
			content[i] = vec.toVector().toArray();

			HSSFRow aDataRow = m_Sheet.createRow((short)(i+1));
			for (int j=0;j<content[i].length;j++)
			{
				HSSFCell cell = aDataRow.createCell((short)j);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			    cell.setCellType(attrType[j]);
			    if (MissingValue.isMissingValue(content[i][j]))
			    {
			    	cell.setCellValue(""); 
			    }else if (attrType[j] == HSSFCell.CELL_TYPE_NUMERIC)
				{
					double value = 0;
					switch (dataType[j])
					{
						case NumericAttribute.DOUBLE:
							value = ((Double)content[i][j]).doubleValue();
							break;
						case NumericAttribute.FLOAT:
							value = ((Float)content[i][j]).doubleValue();
							break;
						case NumericAttribute.INTEGER:
							value = ((Integer)content[i][j]).doubleValue();
							break;
						case NumericAttribute.BOOLEAN:
							
							break;
						case NumericAttribute.DATETIME_PRUDSYS:
							
							break;
					}
				    cell.setCellValue(value);
				}else
				{
					String value = "";
					switch (dataType[j])
					{
						case CategoricalAttribute.STRING:
							value = content[i][j].toString();
							break;
						case CategoricalAttribute.BOOLEAN:
							value = ((Boolean)content[i][j]).toString();
							break;
					}
				    cell.setCellType(attrType[j]);
					cell.setCellValue(value);
				}
			}
		}
	}

	private void exportFromArray()
	{
		
	}
}

⌨️ 快捷键说明

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