[GME-commit] GMESRC/Tools/GMETableEditor TableEditorDumper.h,NONE,1.1 TableEditorDumper.cpp,NONE,1.1 GridDlg.cpp,1.9,1.10

gme-commit at list.isis.vanderbilt.edu gme-commit at list.isis.vanderbilt.edu
Fri Apr 9 15:20:43 CDT 2004


Update of /var/lib/gme/GMESRC/Tools/GMETableEditor
In directory braindrain:/tmp/cvs-serv547/Tools/GMETableEditor

Modified Files:
	GridDlg.cpp 
Added Files:
	TableEditorDumper.h TableEditorDumper.cpp 
Log Message:
First version of Table Editor to Excel XML spreadsheet export

CVS User: brianw

--- NEW FILE: TableEditorDumper.h ---
// TableEditorXMLDumper.h: interface for the TableEditorXMLDumper class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_TABLEEDITORDUMPER_H__8E6DCC47_5A1A_46D0_9CD7_CA0ADE359A49__INCLUDED_)
#define AFX_TABLEEDITORDUMPER_H__8E6DCC47_5A1A_46D0_9CD7_CA0ADE359A49__INCLUDED_


#include <fstream.h>


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000



class TableEditorDumper  
{
public:
	TableEditorDumper();
	virtual ~TableEditorDumper();

// ------- Attributes
	
public:
	ofstream ofs;

public:
	void InitDump(CString filename, int numCols, int numRows);
	void DoneDump();
	void DumpCell(CString cell_type, CString cell_entry, BOOL multi, BOOL boolean);

// ------- Low level stuff
	void StartElem(const char *name);
	void StartElemAttr(const char *name);
	void Attr(const char *name, const char *value, BOOL last);
	void EndElem(const char *name);

};

#endif // !defined(AFX_TABLEEDITORDUMPER_H__8E6DCC47_5A1A_46D0_9CD7_CA0ADE359A49__INCLUDED_)

--- NEW FILE: TableEditorDumper.cpp ---
// TableEditorXMLDumper.cpp: implementation of the TableEditorXMLDumper class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TableEditorDumper.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TableEditorDumper::TableEditorDumper()
{
	
}

TableEditorDumper::~TableEditorDumper()
{
	
}

// --------------------------- TableEditorXMLDumper

void TableEditorDumper::InitDump(CString filename, int numCols, int numRows) 
{
	ASSERT( !ofs.is_open() );
	ofs.open(filename, ios::out | ios::trunc);

// Manually write out header necessary for Excel to open spreadsheet properly
	ofs << "<?xml version=\"1.0\"?>\n";
	ofs << "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n";
	ofs << " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n";
	ofs << " xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n";
	ofs << " xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">\n";
	StartElem("Styles");
	ofs << "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\n";
	EndElem("Style");
	ofs << "<Style ss:ID=\"s21\">\n";
	ofs << "<Alignment ss:Vertical=\"Bottom\" ss:WrapText=\"1\"/>\n";
	EndElem("Style");
	EndElem("Styles");	
	StartElemAttr("Worksheet");
	Attr("ss:Name", "temp", TRUE);	
	ofs << "\n";
	StartElemAttr("Table");

	char numBuf[256];
	itoa(numCols,numBuf,10);
	Attr("ss:ExpandedColumnCount",numBuf, FALSE);
	itoa(numRows,numBuf,10);
	Attr("ss:ExpandedRowCount",numBuf, FALSE);
	Attr("x:FullColumns","1",FALSE);
	Attr("x:FullRows","1",TRUE);
}

void TableEditorDumper::DoneDump()
{

	EndElem("Table"); // </Table>
	
	EndElem("Worksheet"); // </Worksheet>
	
	EndElem("Workbook"); // </Workbook>
	
	ofs.close();
}


void TableEditorDumper::DumpCell(CString cell_type, CString cell_entry, BOOL multi, BOOL boolean)
{
// First checks if cell entry is multiline. w/o specifying styleID, little boxes show up 
	if( multi ) 
	{
		StartElemAttr("Cell");
		Attr("ss:StyleID", "s21", TRUE);
		cell_entry.Replace("\n", "&#10;"); // to preserve multiline
	}
	else {
		StartElem("Cell");
	}

// Checks if cell entry is boolean. w/o converting to 1 / 0, gets Table strict parse error
	if( boolean )
	{
		cell_entry.Replace("true", "1");
		cell_entry.Replace("false", "0");
	}

	StartElemAttr("Data");
	Attr("ss:Type", cell_type, TRUE);
	ofs << cell_entry;
	EndElem("Data"); // </Data>
	EndElem("Cell"); // </Cell>
}


// ------- Low level stuff

inline void TableEditorDumper::StartElem(const char *name)
{
	ASSERT( name != NULL );
	ofs << '<' << name << '>';
}

inline void TableEditorDumper::StartElemAttr(const char *name)
{
	ASSERT( name != NULL );
	ofs << '<' << name;
}

inline void TableEditorDumper::Attr(const char *name, const char *value, BOOL last)
{
	ASSERT( name != NULL );
	ASSERT( value != NULL );

  	ofs << ' ' << name << "=\"" << value << "\"";
	if(last) ofs << ">";
}

inline void TableEditorDumper::EndElem(const char *name)
{
	ofs << "</" << name << ">\n";
}
Index: GridDlg.cpp
===================================================================
RCS file: /var/lib/gme/GMESRC/Tools/GMETableEditor/GridDlg.cpp,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** GridDlg.cpp	8 Apr 2004 16:13:42 -0000	1.9
--- GridDlg.cpp	9 Apr 2004 19:20:40 -0000	1.10
***************
*** 12,15 ****
--- 12,16 ----
  #include "NewCellTypes/GridCellAttrName.h"
  #include "TableEditorParser.h"
+ #include "TableEditorDumper.h"
  
  #ifdef _DEBUG
***************
*** 933,976 ****
  void CGridDlg::OnBtnExport() 
  {
! 	AfxMessageBox("the .csv export will be soon replaced by excel .xml export/import functionality"); //changed to \"\\n\"");
! 	//Export to .csv file for use in MS Excel
! 	CFileDialog FileSelector(FALSE,"csv",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Comma Separated Values (*.csv)|*.csv||");
  	if(FileSelector.DoModal() == IDOK)
  	{
- 		CFile file;
- 		file.Open(FileSelector.GetPathName(), CFile::modeCreate|CFile::modeReadWrite);
- 		
  		int MaxRows = m_Grid.GetRowCount();
  		int MaxCols = m_Grid.GetColumnCount();
! 		for(int i=0; i<MaxRows; i++)
  		{
! 			for(int j=0; j<MaxCols; j++) // goes thru each column
  			{
! 				CString outText = m_Grid.GetItemText(i,j);
! 
! 				//Make multiline cell by adding " to beginning and end of column
! 				outText.Insert(outText.GetLength(), '"');
! 				outText.Insert(0, '"');
! 	
! 				//outText.Replace("\r\n", "");
! 				//outText.Replace("\n\r", "");
! 				outText.Replace("\r", "");
! 
! 				outText.Replace(","," COMMA ");
! 	
! 				char *buffer1;
! 				buffer1 = outText.GetBuffer(1);
! 
! 				char buffer2[1];
! 				buffer2[0] = ',';
! 
! 				file.Write(buffer1, outText.GetLength());
! 				file.Write(buffer2, 1);
! 			}				
  			
! 			char buffer3[1];
! 			buffer3[0] = '\n';
! 			file.Write(buffer3, 1);
  		}
  	}
  }
--- 934,988 ----
  void CGridDlg::OnBtnExport() 
  {
! 	//Export to .xml file for use in MS Excel
! 	CFileDialog FileSelector(FALSE,"xml",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Excel formatted XML (*.xml)|*.xml||");
  	if(FileSelector.DoModal() == IDOK)
  	{
  		int MaxRows = m_Grid.GetRowCount();
  		int MaxCols = m_Grid.GetColumnCount();
! 		CString cell_type;
! 
! 		TableEditorDumper dumper;  // creates dumper 
! 		dumper.InitDump(FileSelector.GetPathName(),MaxCols,MaxRows); // adds in proper excel header
! 		for( int i=0; i<MaxRows; i++ )
  		{
! 			dumper.StartElem("Row");
! 			for( int j=0; j<MaxCols; j++ )
  			{
! 				CString ClassName = m_Grid.GetCell(i,j)->GetRuntimeClass()->m_lpszClassName; 
! 				CString cell_entry = m_Grid.GetItemText(i,j);
! 				CString cell_type = "String"; //default to String.
! 				BOOL multi = FALSE;
! 				BOOL boolean = FALSE;
! 				
! 				if( ClassName == "CGridCellComboBool" ) 
! 				{
! 					cell_type = "Boolean";
! 					boolean = TRUE;
! 				}
! 				
! 				if( ClassName == "CGridCellNumeric" || ClassName == "CGridCellDouble" ) 
! 				{
! 					cell_type = "Number";
! 				}
! 				
! 				if( ClassName == "CGridCell" ) 
! 				{
! 					cell_type = "String";
! 				}
! 				
! 				if( ClassName == "CGridCellMultiLine" ) 
! 				{
! 					cell_type = "String";
! 					multi = TRUE;
! 				}
! 				
! 				dumper.DumpCell(cell_type, cell_entry, multi, boolean);
! 			}
  			
! 			dumper.EndElem("Row");
  		}
+ 		
+ 		dumper.DoneDump();
+ 
  	}
  }



More information about the GME-commit mailing list