Click here to Skip to main content
15,891,033 members
Home / Discussions / C#
   

C#

 
QuestionAccess is denied Pin
jon-8010-Feb-12 22:29
professionaljon-8010-Feb-12 22:29 
AnswerRe: Access is denied Pin
fjdiewornncalwe11-Feb-12 5:40
professionalfjdiewornncalwe11-Feb-12 5:40 
QuestionWriting to an excel file Pin
JFRobertson10-Feb-12 20:12
JFRobertson10-Feb-12 20:12 
AnswerRe: Writing to an excel file Pin
Richard MacCutchan10-Feb-12 23:48
mveRichard MacCutchan10-Feb-12 23:48 
GeneralRe: Writing to an excel file Pin
JFRobertson11-Feb-12 8:08
JFRobertson11-Feb-12 8:08 
GeneralRe: Writing to an excel file Pin
Richard MacCutchan11-Feb-12 23:32
mveRichard MacCutchan11-Feb-12 23:32 
GeneralRe: Writing to an excel file Pin
Richard MacCutchan12-Feb-12 1:13
mveRichard MacCutchan12-Feb-12 1:13 
GeneralRe: Writing to an excel file Pin
JFRobertson12-Feb-12 8:49
JFRobertson12-Feb-12 8:49 
Actually it was working. I thought I had verified that there were no hidden sheets, but I forgot to select them all before I clicked unhide. I am having an additional problem now. The code is working, but I have started to encapsulate it so I can make it better as well as start the process of finding which sheet rather than hardcoding, but it is crashing with a can not bind to a null object whether I call save or not. The big problem is that it is actually working even though it is giving the can't bind to a null object at runtime. Thank you for the help. I really hate that I spent so many years avoiding learning com interop, because now that I need it on a deadline it is killing me.

This is how I have modified the code. The calls are pretty much still the same just encapsulated but I am still hardcoding them.
C#
Excel.Application excelDocument = ExcelOperations.GetApplication();
            Excel.Workbook xlWorkbook = ExcelOperations.GetWorkbook(excelDocument);
            Excel.Worksheet xlWorksheet = ExcelOperations.GetWorksheet("", xlWorkbook);
            Excel.Range xlRange = ExcelOperations.GetRange(xlWorksheet);
            try
            {
                //This is the call to parse the tab document.  The query variable will hold a copy of everything in the text doc while it is
                //in scope.  The query is stored as a generic list to help with iteration without casting issues.
                var query = QueryTabDocument(columnsCount, filePath);
                List<ScoreTabFile> events = query.ToList();

                ////This code sets up and opens the excel score sheet so that it can be written too and read from. This 
                ////using statement is required for this code to work.  using Excel = Microsoft.Office.Interop.Excel;


                int rowCount = xlRange.Rows.Count;
                int colCount = xlRange.Columns.Count;

                //Calculate and record the air losses.
                int totalAircraftLostAA = 0;
                int totalAircraftLostSA = 0;
                ScoreCalculations.CalculateEnemyAirLosses(events, ref totalAircraftLostAA, ref totalAircraftLostSA);
                int column = 3;
                for (int row = 1; row <= rowCount; row++)
                {
                    if (string.Equals(xlRange.Cells[row, 1].Value2.ToString(), "Aircraft Kills (A-A)"))
                    {
                        Console.WriteLine(xlRange.Cells[row, 1].Value2.ToString());
                        Console.WriteLine("write attempted");
                        xlRange.Cells[row, column].Value2 = totalAircraftLostAA;
                    }
                }


                //Calculate and record ally airlosses
                ScoreCalculations.CalculateAllyAirLosses(events);

                //Calculate and record expended Ordnance
                ScoreCalculations.CalculateExpendedOrdnance(events);

                //Calculate and record ground targets hit
                ScoreCalculations.CalculateNamedGroundTargets(events);

                //xlWorkbook.Save();


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
            finally
            {
                //Clean up the com objects
                GC.Collect();
                GC.WaitForPendingFinalizers();

                Marshal.FinalReleaseComObject(xlRange);
                Marshal.FinalReleaseComObject(xlWorksheet);
                xlWorkbook.Close();
                Marshal.FinalReleaseComObject(xlWorkbook);
                excelDocument.Quit();
                Marshal.FinalReleaseComObject(excelDocument);
            }


modified 12-Feb-12 16:15pm.

QuestionRe: Writing to an excel file Pin
Richard MacCutchan12-Feb-12 9:07
mveRichard MacCutchan12-Feb-12 9:07 
GeneralRe: Writing to an excel file Pin
BobJanova12-Feb-12 22:44
BobJanova12-Feb-12 22:44 
GeneralRe: Writing to an excel file Pin
Richard MacCutchan12-Feb-12 23:02
mveRichard MacCutchan12-Feb-12 23:02 
QuestionWhy IsBetweenLabels property only works for AxysType Text? Pin
Thiago Marçal10-Feb-12 8:04
Thiago Marçal10-Feb-12 8:04 
QuestionText Editor Pin
jeramyRR10-Feb-12 6:41
jeramyRR10-Feb-12 6:41 
AnswerRe: Text Editor Pin
Eddy Vluggen10-Feb-12 6:52
professionalEddy Vluggen10-Feb-12 6:52 
GeneralRe: Text Editor Pin
jeramyRR10-Feb-12 7:15
jeramyRR10-Feb-12 7:15 
QuestionGenerating html tree from xml file using XSLT..? Pin
anikbutt2210-Feb-12 6:16
anikbutt2210-Feb-12 6:16 
AnswerRe: Generating html tree from xml file using XSLT..? Pin
jschell10-Feb-12 9:24
jschell10-Feb-12 9:24 
GeneralRe: Generating html tree from xml file using XSLT..? Pin
anikbutt2210-Feb-12 14:18
anikbutt2210-Feb-12 14:18 
GeneralRe: Generating html tree from xml file using XSLT..? Pin
jschell12-Feb-12 7:31
jschell12-Feb-12 7:31 
Questionc# code to convert txt to xls Pin
taurusone10-Feb-12 4:37
taurusone10-Feb-12 4:37 
AnswerRe: c# code to convert txt to xls Pin
Luc Pattyn10-Feb-12 4:40
sitebuilderLuc Pattyn10-Feb-12 4:40 
GeneralRe: c# code to convert txt to xls Pin
taurusone10-Feb-12 4:55
taurusone10-Feb-12 4:55 
AnswerRe: c# code to convert txt to xls Pin
Luc Pattyn10-Feb-12 5:21
sitebuilderLuc Pattyn10-Feb-12 5:21 
AnswerRe: c# code to convert txt to xls Pin
Eddy Vluggen10-Feb-12 5:19
professionalEddy Vluggen10-Feb-12 5:19 
GeneralRe: c# code to convert txt to xls Pin
taurusone10-Feb-12 6:11
taurusone10-Feb-12 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.