Excel and ' Invalid Operation Problems'

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
So umm. Hello.
Wyck - MCC originally did this code ive removed some bits.
Ive tryed a few hours to find a solution . <br/>

I cant so maybe you guys know a way to do it?:D

Problem : at Sequence contains no elements
Console.Write("{0} {1},pair.Key, pair.Value.Average());
Basic program description : It is going to read data from a file ( with a number of different variables , D(date).T,R,P and H ( in which all have values next to them).
The program will add each of the variables to a list(in the case of this code) and the output will be to average the values. Excluding the date from all outputs all together.
<File format>
<span lang="FR" style="font-size:12pt; font-family:Courier New 5:P:987:D:2012:11:26:D:2012:11:27:P:980:
<span lang="FR" style="font-size:12pt; font-family:Courier New R:71:T:50:D:2012:11:28:H:78:P:996:D:2012
<span lang="FR" style="font-size:12pt; font-family:Courier New
<span style="font-size:medium And umm, i need it too export the data + averages to excel sheet. One user gave me this link to use:
http://www.codeproject.com/Articles/204562/Small-Research-on-Exporting-Data-to-Excel -- ( still a little unsure where to put either codes/noobme

However, I have this code I would like to use if possible.

<span style="font-size:11pt; font-family:Courier New <span style="color:blue var excelApp = <span style="color:blue new Excel.<span style="color:#2b91af Application();
<span style="font-size:11pt; font-family:Courier New excelApp.Visible = <span style="color:blue true;

<span style="font-size:11pt; font-family:Courier New excelApp.Workbooks.Add();

<p style=" <span style="font-size:11pt; font-family:Courier New Excel.<span style="color:#2b91af _Worksheet workSheet = (Excel.<span style="color:#2b91af Worksheet)excelApp.ActiveSheet;
<span style="font-size:11pt; font-family:Courier New workSheet.Cells[1, <span style="color:#a31515 "A"] = <span style="color:#a31515 "ID Number";
<span style="font-size:11pt; font-family:Courier New workSheet.Cells[1, <span style="color:#a31515 "B"] = <span style="color:#a31515 "Current Balance";

<span style="font-size:11pt; font-family:Courier New <span style="color:blue for (<span style="color:blue int row = 1; row <= 10; row++)
<span style="font-size:11pt; font-family:Courier New {
<span style="font-size:11pt; font-family:Courier New workSheet.Cells[row + 1, <span style="color:#a31515 "A"] = 34*row;
<span style="font-size:11pt; font-family:Courier New workSheet.Cells[row + 1, <span style="color:#a31515 "B"] = <span style="color:#a31515 "Text
here";
<span style="font-size:11pt; font-family:Courier New workSheet.Columns[1].AutoFit();
<span style="font-size:11pt; font-family:Courier New workSheet.Columns[2].AutoFit();
<span style="font-size:11pt; font-family:Courier New ((Excel.<span style="color:#2b91af Range)workSheet.Columns[1]).AutoFit();
<span style="font-size:11pt; font-family:Courier New ((Excel.<span style="color:#2b91af Range)workSheet.Columns[2]).AutoFit();
<span style="font-size:11pt; font-family:Courier New }

<span style="font-size:11pt; font-family:Courier New using Excel = Microsoft.Office.Interop.Excel;

<span lang="FR" style="font-size:12pt; font-family:Courier New
<span lang="FR" style="font-size:12pt; font-family:Courier New < The Code --------------- >

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




View the full article
 
Back
Top