Displaying data in Excel 07

Only managed to display some.

Page 1 of 1

0 Replies - 705 Views - Last Post: 21 January 2010 - 12:58 AM Rate Topic: -----

#1 rshin2020   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 05-May 08

Displaying data in Excel 07

Post icon  Posted 21 January 2010 - 12:58 AM

Hi. I have a script as below. I need to display the data in excel. This is in C#.net. Right now, the data only displays up till B5 and C5. How do I make it display till B8 and C8. The values for symbol and unit are null.


using System; 
using System.Collections.Generic; 
using System.Text; 
using Excel = Microsoft.Office.Interop.Excel; 
  
namespace UncFunc 
{ 
	class Report 
	{ 
  
		public void Create(Quantity q) 
		{ 
			Excel.Application objApp; 
			Excel._Workbook objBook; 
			Excel.Workbooks objBooks; 
			Excel.Sheets objSheets; 
			Excel._Worksheet objSheet; 
			Excel.Range range; 
			Dictionary<int, Quantity> rootQty = q.RootQuantities; 
  
			try 
			{ 
				// Instantiate Excel and start a new workbook. 
				objApp = new Excel.Application(); 
				objBooks = objApp.Workbooks; 
				objBook = objBooks.Add(Type.Missing); 
				objSheets = objBook.Worksheets; 
				objSheet = (Excel._Worksheet)objSheets.get_Item(1); 
  
				//Get the range where the starting cell has the address 
				//m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols. 
				range = objSheet.get_Range("A1", Type.Missing); 
				range = range.get_Resize(6,4); 
  
				//Create an array. 
				string[,] saRet = new string[rootQty.Count+2, 4]; 
  
  
				saRet[0, 0] = "Root Quantities"; 
				saRet[1, 0] = "Symbol"; 
				saRet[1, 1] = "Value"; 
				saRet[1, 2] = "Std Unc"; 
				saRet[1, 3] = "Unit"; 
  
  
				int[] aryKeys= new int[rootQty.Keys.Count]; 
				Dictionary<int,Quantity>.KeyCollection oKeys = rootQty.Keys; 
				oKeys.CopyTo(aryKeys, 0); 
  
				//Fill the array. 
				for (long iRow = 0; iRow < rootQty.Count; iRow++) 
				{ 
  
					//for (long iCol = 0; iCol < 5; iCol++) 
					//{ 
						int iKey = aryKeys[iRow]; 
  
						Quantity qty = rootQty[iKey]; 
						String sSymbol = qty.Symbol; 
						String sValue = qty.Value.ToString(); 
						String sStdUnc = qty.StdUnc.ToString(); 
						String sUnit = qty.Unit; 
  
						saRet[iRow+2,0] = sSymbol; 
						saRet[iRow+2,1] = sValue; 
						saRet[iRow+2,2] = sStdUnc; 
						saRet[iRow+2,3]  =sUnit; 
  
  
						//Put the row and column address in the cell. 
						//saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString(); 
  
  
					//} 
				} 
  
  
  
				//Set the range value to the array. 
				range.set_Value(Type.Missing, saRet); 
  
				//Set Subscript 
				range = range.get_Resize(1, 1); 
				//objSheet.get_Range("A1", Type.Missing); 
				//range = range.get_Resize(1, 1); 
				//range.get_Characters(2, 2).Font.Subscript = true; 
  
  
				//Bold(Added) 
				//range = range.get_Resize(1, 1); 
				range.get_Characters(1, 1).Font.Bold = true; 
				range.Font.Bold = true; 
  
				//Return control of Excel to the user. 
				objApp.Visible = true; 
				objApp.UserControl = true; 
  
				//Save 
				objBook.SaveAs("Report Uncertainty.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,  
					Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, 
					Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
			} 
			catch (Exception theException) 
			{ 
				String errorMessage; 
				errorMessage = "Error: "; 
				errorMessage = String.Concat(errorMessage, theException.Message); 
				errorMessage = String.Concat(errorMessage, " Line: "); 
				errorMessage = String.Concat(errorMessage, theException.Source); 
  
				Console.WriteLine(errorMessage, "Error"); 
			} 
		} 




Hope someone can help. The data can only be displayed upto 3. I have 6. So where should I add the code as well as what to be added? Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1