473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Displaying the rest of data in Excel

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.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Excel = Microsoft.Office.Interop.Excel;
  5.  
  6. namespace UncFunc
  7. {
  8.     class Report
  9.     {
  10.         public void Create(Quantity q)
  11.         {
  12.             Excel.Application objApp;
  13.             Excel._Workbook objBook;
  14.             Excel.Workbooks objBooks;
  15.             Excel.Sheets objSheets;
  16.             Excel._Worksheet objSheet;
  17.             Excel.Range range;
  18.             Dictionary<int, Quantity> rootQty = q.RootQuantities;
  19.  
  20.             try
  21.             {
  22.                 // Instantiate Excel and start a new workbook.
  23.                 objApp = new Excel.Application();
  24.                 objBooks = objApp.Workbooks;
  25.                 objBook = objBooks.Add(Type.Missing);
  26.                 objSheets = objBook.Worksheets;
  27.                 objSheet = (Excel._Worksheet)objSheets.get_Item(1);
  28.  
  29.                 //Get the range where the starting cell has the address
  30.                 //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
  31.                 range = objSheet.get_Range("A1", Type.Missing);
  32.                 range = range.get_Resize(6,4);
  33.  
  34.                 //Create an array.
  35.                 string[,] saRet = new string[rootQty.Count+2, 4];
  36.                 saRet[0, 0] = "Root Quantities";
  37.                 saRet[1, 0] = "Symbol";
  38.                 saRet[1, 1] = "Value";
  39.                 saRet[1, 2] = "Std Unc";
  40.                 saRet[1, 3] = "Unit";
  41.  
  42.                 int[] aryKeys= new int[rootQty.Keys.Count];
  43.                 Dictionary<int,Quantity>.KeyCollection oKeys = rootQty.Keys;
  44.                 oKeys.CopyTo(aryKeys, 0);
  45.  
  46.                 //Fill the array.
  47.                 for (long iRow = 0; iRow < rootQty.Count; iRow++)
  48.                 {
  49.  
  50.                     //for (long iCol = 0; iCol < 5; iCol++)
  51.                     //{
  52.                         int iKey = aryKeys[iRow];
  53.                         Quantity qty = rootQty[iKey];
  54.                         String sSymbol = qty.Symbol;
  55.                         String sValue = qty.Value.ToString();
  56.                         String sStdUnc = qty.StdUnc.ToString();
  57.                         String sUnit = qty.Unit;
  58.  
  59.                         saRet[iRow+2,0] = sSymbol;
  60.                         saRet[iRow+2,1] = sValue;
  61.                         saRet[iRow+2,2] = sStdUnc;
  62.                         saRet[iRow+2,3]  =sUnit ;
  63.  
  64.                         //Put the row and column address in the cell.
  65.                         //saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();                        
  66.                     //}
  67.                 }
  68.  
  69.                 //Set the range value to the array.
  70.                 range.set_Value(Type.Missing, saRet);
  71.  
  72.                 //Set Subscript
  73.                 range = range.get_Resize(1, 1);
  74.                 //objSheet.get_Range("A1", Type.Missing);
  75.                 //range = range.get_Resize(1, 1);
  76.                 //range.get_Characters(2, 2).Font.Subscript = true;
  77.  
  78.                 //Bold(Added)
  79.                 //range = range.get_Resize(1, 1);
  80.                 range.get_Characters(1, 1).Font.Bold = true;
  81.                 range.Font.Bold = true;
  82.  
  83.                 //Return control of Excel to the user.
  84.                 objApp.Visible = true;
  85.                 objApp.UserControl = true;
  86.  
  87.                 //Save
  88.                 objBook.SaveAs("Report Uncertainty.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
  89.                     Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,
  90.                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  91.             }
  92.             catch (Exception theException)
  93.             {
  94.                 String errorMessage;
  95.                 errorMessage = "Error: ";
  96.                 errorMessage = String.Concat(errorMessage, theException.Message);
  97.                 errorMessage = String.Concat(errorMessage, " Line: ");
  98.                 errorMessage = String.Concat(errorMessage, theException.Source);
  99.  
  100.                 Console.WriteLine(errorMessage, "Error");
  101.             }
  102.         }
Hope someone can help, Thanks in advance. I know i have to change the coding after the for long code. I have tried but still outputs the indifferent.
Jan 21 '10 #1
8 3700
tlhintoq
3,525 Expert 2GB
Let me guess. You just copied this from someplace else and have no idea how it works.

I might be way off here because I don't do much with Office Interop, but line 54 looks pretty obvious since you only get through row 5 and the loop goes from 0 to 4 (that would be 5)
Jan 21 '10 #2
Nope, my boss had this example and asked me to go through it. I have added some codes inside to customize it. Its just that i couldnt figure it out where the mistake is.
"line 54 looks pretty obvious since you only get through row 5 and the loop goes from 0 to 4 (that would be 5) " meaningi have to change the loop to 4? But is that line even used? since i commanded it with //.
Jan 21 '10 #3
tlhintoq
3,525 Expert 2GB
"line 54 looks pretty obvious since you only get through row 5 and the loop goes from 0 to 4 (that would be 5) "
meaningi have to change the loop to 4? But is that line even used? since i commanded it with //.
You are very right. I was tired when I read that and missed that the loop was commented out.

What about line 33 where the range is selected? Like I said, I don't do much programmatically with Excel, and thought I had seen a fairly obvious place to increase the area of interest. I guess I was wrong. That's what trial and error is all about.
Jan 21 '10 #4
Its okay, trial and error i guess.... Line 33? I have tried customizing it too, but still no change. I have tried to customize many places as well, but still no change. :(
Jan 22 '10 #5
Hi. With reference to the above topic, how do I get to display another table of data in the same sheet? I managed to display the first table with data but now i need to display a second table. Where do I change or add the codings?
here's the latest:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Excel = Microsoft.Office.Interop.Excel;
  5.  
  6. namespace UncFunc
  7. {
  8.     class Report
  9.     {
  10.  
  11.         public void Create(Quantity q)
  12.         {
  13.             Excel.Application objApp;
  14.             Excel._Workbook objBook;
  15.             Excel.Workbooks objBooks;
  16.             Excel.Sheets objSheets;
  17.             Excel._Worksheet objSheet;
  18.             Excel.Range range;
  19.             Dictionary<int, Quantity> rootQty = q.RootQuantities;
  20.             //object misValue = System.Reflection.Missing.Value;
  21.  
  22.  
  23.             try
  24.             {
  25.                 // Instantiate Excel and start a new workbook.
  26.                 objApp = new Excel.Application();
  27.                 objBooks = objApp.Workbooks;
  28.                 objBook = objBooks.Add(Type.Missing);
  29.                 objSheets = objBook.Worksheets;
  30.                 objSheet = (Excel._Worksheet)objSheets.get_Item(1);
  31.  
  32.                 //Get the range where the starting cell has the address
  33.                 //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
  34.                 range = objSheet.get_Range("A1", Type.Missing);
  35.                 range = range.get_Resize(6,10);
  36.  
  37.                 //2nd table
  38.                 range = objSheet.get_Range("A2", Type.Missing);
  39.                 range = range.get_Resize(6, 10);
  40.  
  41.                 //Create an array.
  42.                 string[,] saRet = new string[rootQty.Count+2,10];
  43.  
  44.                 saRet[0, 4] = "Root Quantities";
  45.                 saRet[1, 0] = "String";
  46.                 saRet[1, 1] = "Symbol";
  47.                 saRet[1, 2] = "Limit Description";
  48.                 saRet[1, 3] = "Limit Type";
  49.                 saRet[1, 4] = "Value";
  50.                 saRet[1, 5] = "Limit";
  51.                 saRet[1, 6] = "DF";
  52.                 saRet[1, 7] = "Divisor";
  53.                 saRet[1, 8] = "Std Unc";
  54.                 saRet[1, 9] = "Unit";
  55.  
  56.  
  57.  
  58.  
  59.                 int[] aryKeys= new int[rootQty.Keys.Count];
  60.                 Dictionary<int,Quantity>.KeyCollection oKeys = rootQty.Keys;
  61.                 oKeys.CopyTo(aryKeys, 0);
  62.  
  63.                 //Fill the array.
  64.                 for (long iRow = 0; iRow < rootQty.Count; iRow++)
  65.                 {
  66.  
  67.                     //for (long iCol = 0; iCol < 4; iCol++)
  68.                     //{
  69.                         int iKey = aryKeys[iRow];
  70.  
  71.                         Quantity qty = rootQty[iKey];
  72.  
  73.                         String sSymbol = qty.Symbol;//column B
  74.                         String sLimitDescription = qty.LimitDescription;// column C
  75.                         //UncType = qty.LimitType.ToString();
  76.                         String sValue = qty.Value.ToString();//column D
  77.                         String sLimit = qty.Limit.ToString();//column E
  78.                         String sDF = qty.df.ToString();//column F
  79.                         String sDivisor = qty.Divisor.ToString();//column G
  80.                         String sStdUnc = qty.StdUnc.ToString();//column H
  81.                         String sUnit = qty.Unit;// column I
  82.  
  83.                         saRet[iRow+2, 1] = sSymbol;
  84.                         saRet[iRow + 2, 2] = sLimitDescription;
  85.                         //saRet[iRow + 2, 3] = qty.LimitType.ToString();
  86.                         saRet[iRow + 2, 4] = sValue.ToString();
  87.                         saRet[iRow + 2, 5] = sLimit.ToString();
  88.                         saRet[iRow + 2, 6] = sDF.ToString();
  89.                         saRet[iRow + 2, 7] = sDivisor.ToString();
  90.                         saRet[iRow + 2, 8] = sStdUnc.ToString();
  91.                         saRet[iRow + 2, 9] = sUnit;
  92.  
  93.  
  94.                     //Previous statements                
  95.                         //saRet[iRow+2,0] = sSymbol;
  96.                         //saRet[iRow+2,1] = sValue;
  97.                         //saRet[iRow+2,2] = sStdUnc;
  98.                         //saRet[iRow+2,3] = qty.Unit; //sUnit
  99.  
  100.                         //Put the row and column address in the cell.
  101.                         //saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();
  102.  
  103.  
  104.                   // }
  105.                 }
  106.  
  107.  
  108.                 //Set the range value to the array.
  109.                 range.set_Value(Type.Missing, saRet);
  110.  
  111.                 //Replace the _ with space
  112.                 string myValue = "X_2";
  113.                 string newValue = myValue.Replace("_", "");
  114.  
  115.  
  116.                 //Set Subscript
  117.                 range = range.get_Resize(6, 9);
  118.                 objSheet.get_Range("A1", Type.Missing);
  119.                 //range = range.get_Resize(0, 2);
  120.                 //range.get_Range(2, 0);
  121.  
  122.                 objSheet.get_Range("A1", "A6").EntireColumn.AutoFit();
  123.                 objSheet.get_Range("B3", "B6").get_Characters(2, 2).Font.Subscript = true;//column and character chosen respectively
  124.  
  125.                 //2nd table
  126.                 //objSheet.get_Range("A10", "A16").EntireColumn.AutoFit();
  127.                 //objSheet.get_Range("B12", "B15").get_Characters(2, 2).Font.Subscript = true;//column and character chosen respectively
  128.  
  129.                 //objSheet.get_Range("B4","B6").Font.Subscript = true;
  130.                 //objSheet.get_Range(1, 1).Font.Subscript = true;
  131.  
  132.  
  133.                 //Set Bold(Added)
  134.                 objSheet.get_Range("A1", "J1").Font.Bold = true;
  135.                 objSheet.get_Range("A2", "J2").Font.Bold = true;
  136.                 //range = range.get_Resize(1, 1);
  137.                 //range.get_Characters(1,1).Font.Bold = true;
  138.                 //range.Font.Bold = true;
  139.  
  140.  
  141.                 //Return control of Excel to the user.
  142.                 objApp.Visible = true;
  143.                 objApp.UserControl = true;
  144.  
  145.  
  146.                 // Displaying formula in spreadsheet
  147.                 //objSheet.get_Range("A8", "A8").FormulaR1C1 = "y = x1 * x2 - x3 / x2 * Quantity.Log(x4)";
  148.  
  149.                 objSheet.get_Range("A1", "J6").BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium,
  150.                             Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
  151.  
  152.  
  153.                //Save
  154.                 objBook.SaveAs("Report Uncertainty.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
  155.                     Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,
  156.                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  157.             }
  158.             catch (Exception theException)
  159.             {
  160.                 String errorMessage;
  161.                 errorMessage = "Error: ";
  162.                 errorMessage = String.Concat(errorMessage, theException.Message);
  163.                 errorMessage = String.Concat(errorMessage, " Line: ");
  164.                 errorMessage = String.Concat(errorMessage, theException.Source);
  165.  
  166.                 Console.WriteLine(errorMessage, "Error");
  167.             }
  168.         }
  169.  
  170.  
Feb 11 '10 #6
Any replies? What part do i have to change? From a logical aspect, i understand that i have to copy the array and paste it somewhere. I'm not sure where to paste it though. Please help. Thanks a lot in advance.
Feb 24 '10 #7
Bassem
344 100+
Hi,

Well, I don't have an answer to you, but I have a suggestion.

Three days ago, I didn't work with Excel through C#. When I decided I used XslCompiledTransform.
It is the simplest and fastest way to work with Excel like this:
1. Convert Excel file to XML.
2. Work with XML via Linq or DOM whatever you prefer.

It will not take more than few minutes to figure it and use it.

Perhaps my post is useless, but may be you give it a try and it will work for you. Working with XML is more simpler and used wider than Excel.

Hoped to help!

Thanks,
Bassem
Feb 24 '10 #8
That's a good suggestion. Will consider it. But as for now, I was asked to do it in excel. I am unsure of where to put the code. Thanks a lot, Bassem.
Feb 24 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
2
by: Joo Park | last post by:
Hello, i have an aspx page that displays data in tabular format. I'd like to change this page so that it outputs that same data into excel format. what needs to change in an aspx page to do...
3
by: Brooke | last post by:
I am new to ASP.NET, but have been programming for about 14 years (C# about 2 years). My manager asked me to develop a web application that would allow employees to view a spreadsheet that is used...
0
by: robin9876 | last post by:
In an .aspx (VB) page that displays that data in Excel which shows the data in excel within the browser. The following code is in the page to identify the file information. ...
1
by: Brave | last post by:
I have MS Access databases that are viewed via ASP pages made in Frontpage. I have two issues that have the same problem. 1: When someone views database results on one of my webpages, and...
7
by: alwayssmiling | last post by:
Hi frends, In my application, a class supports backend processing. In this class im creating a new excel sheet and im inserting some data into this excel sheet, and im trying to save the file...
5
by: =?Utf-8?B?U3R1YXJ0?= | last post by:
Hi There I have been having a play around with the following code to display a datagrid in Excel (all from Steve Orr's site): Private Sub btnTechServAccred_Click(ByVal sender As System.Object,...
1
by: NBKMNDN | last post by:
Hi frends, In my C# application im creating a new excel sheet and im inserting some data into this excel sheet, and im trying to save the file with the help of method SAVE AS. But it doesnot...
13
by: David W. Fenton | last post by:
I've been struggling the last two days with something I thought was very easy, which is to open a web page with a form on it and populate the form with data passed in a query string (either POST or...
1
by: harikakumar | last post by:
plz anyone knoe about displaying data in excel files using c# on command prompt thats using console application help me
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.