CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2012
    Posts
    2

    Excel and ' Invalid Operation Problems'

    Hello.

    I'm currently making a program, that will read data from a file, that contains a number different variables. These being, D - Date , T, R, P and H which are just values. The program will need to add up all the 'T' values, 'R' values , including the P and H values separately. With the same format and outputting the average of each value. ( I'm sure most of it has been done already via the code I currently have). Though, their are some problems that arise. (I'll explain, further down.

    Code:
    5:P:987:D:2012:11:26:D:2012:11:27:P:980:
    
    R:71:T:50:D:2012:11:28:H:78:P:996:D:2012
    The separator char being ':'.

    The Code
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    
    //Class Libary IO has been added to the list
    
    class Program
    {
    
        public static void Main()
        {
            //FileStream file = new FileStream(@"c:\CSharp\CW1_2012.txt", FileMode.Open);
            // StreamReader read = new StreamReader(file);
            // The file will be located and and opened for use further into the program.
    
            Dictionary<string, List<int>> variableList = new Dictionary<string, List<int>>();
            string whiteSpace = new string(File.ReadAllText(@"c:\CSharp\CW1_2012.txt").Where(dataFile => !char.IsWhiteSpace(dataFile)).ToArray());
            // This strips the file of white space.
    
            string[] fields = whiteSpace.Split(new char[] { ':' }, StringSplitOptions.None);
            // This splits the  file up into pieces using the semi colon as the point to seperate each char.
    
            List<int> numbers = new List<int>();
            // This creates a new list.
    
            foreach (string field in fields
            // This looks through all of the fields.
            {
                if (char.IsDigit(field.FirstOrDefault()))
                {
                    numbers.Add(Convert.ToInt32(field));
                }
                else
                {
                    List<int> currentlist = null;
    
                    if (!variableList.TryGetValue(field, out currentlist))
                    {
                        currentlist = new List<int>();
                        variableList.Add(field, currentlist);
                    }
    
                    {
                        currentlist.AddRange(numbers);
    
                        foreach (var pair in variableList)
                        {
                            Console.WriteLine("{0} {1}", pair.Key, pair.Value.Average());
                            Console.ReadLine();
                        }

    The problem arises at this section of the program. I get an error saying 'Sequence contains no elements'
    I'm unsure what to do from here. I have looked for other solutions and tryed to figure what is wrong but as you can see I havent found one just yet xD

    When I try to debugg it , it simply flashes the cmd box for a second so, now im not sure if the program is doing what it is meant to do.

    }
    }
    }
    }


    --- Second small problem.

    If i can get a fix for the current code, how would I get it to export the data + the produced averages into an excel file? I have this code I would like to use if possible.
    Code:
                var excelApp = new Excel.Application();
    
                excelApp.Visible = true;
    
    
                excelApp.Workbooks.Add();
    
    
    Excel._Worksheet workSheet =     (Excel.Worksheet)excelApp.ActiveSheet;
    
                workSheet.Cells[1, "A"] = "ID Number";
    
                workSheet.Cells[1, "B"] = "Current Balance";
    
    
                for (int row = 1; row <= 10; row++)
    
                {
    
                    workSheet.Cells[row + 1, "A"] = 34*row;
    
                    workSheet.Cells[row + 1, "B"] = "Text here";
    
                    workSheet.Columns[1].AutoFit();
    
                    workSheet.Columns[2].AutoFit();
    
                    ((Excel.Range)workSheet.Columns[1]).AutoFit();
    
                    ((Excel.Range)workSheet.Columns[2]).AutoFit();
    
                }
    
    using Excel = Microsoft.Office.Interop.Excel;
    Thank you for reply in advance

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Excel and ' Invalid Operation Problems'

    Hi and welcome to the forum. You can get your code to format nicely by surrounding it with [code] and [/code] tags (I have added this to your first post for you).
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2012
    Posts
    2

    Re: Excel and ' Invalid Operation Problems'

    Quote Originally Posted by BioPhysEngr View Post
    Hi and welcome to the forum. You can get your code to format nicely by surrounding it with [code] and [/code] tags (I have added this to your first post for you).
    Thank you.

    And didn't know about using the [code] on forums til now. :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured