Dashboard > Public Content > ... > ExcelCom > Reading Excel Ranges from a COM Addin
  Public Content Log In   View a printable version of the current page.  
  Reading Excel Ranges from a COM Addin
Added by James Richardson, last edited by James Richardson on Aug 03, 2006  (view change)
Labels: 
(None)

For some reason, the index starts at 1... don't know why that is, so to convert an excel range into an html table you can do this:

public string rangeToTable(Excel.Range range) {
	int rows = range.Rows.Count;
	int cols = range.Columns.Count;

	XmlDocument document = new XmlDocument();
	XmlNode root = document.CreateElement("table");
	document.AppendChild(root);
        for ( int row = 1 ; row <= rows ; row++ ) {
		XmlNode rowNode = document.CreateElement("tr");				
		root.AppendChild(rowNode);
		for ( int col = 1 ; col <= cols ; col++ ) {
			XmlNode columnNode = document.CreateElement("td");
			rowNode.AppendChild(columnNode);
			columnNode.InnerText = (string)((Excel.Range)range.Cells[row,col]).Text;
		}
	}
	return document.OuterXml;
}
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators