4 Replies - 415 Views - Last Post: 14 April 2013 - 08:42 AM Rate Topic: -----

#1 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Writing An Excel Workbook

Posted 12 April 2013 - 04:19 AM

I have started writing a function to export some data to an Excel file using a few tutorials I found.

For some reason when I run the code it creates 100's of workbooks, charts etc as if it is looping.

Can anyone see where I am going wrong?

//Export change.
        public static void ExportChange(int JobID)
        {
            //Variables.
            Application Excel = new Application(); //New application for working with Excel.
            Workbook WkBook; //New workbook.
            Worksheet WkSheet = new Worksheet(); //New worksheet.
            Range Rng; //Range for adding data to sheet.
            string TempFileLoc = Properties.Settings.Default.DocumentationFilePath; //Location to create Excel file.

            //Initial settings.
            Excel.Visible = true; //Make application visible.

            //Create Excel objects.
            WkBook = Excel.Workbooks.Add(Properties.Resources.ExportTemplate); //Add new workbook to Excel application using template.
            WkSheet = WkBook.Worksheets[1]; //Set worksheet to first sheet in template.

            //Get data.
            GetChangeByID(JobID); //Load data for selected job.
            Change_LogDataSet.tblChangesRow TableRow = TableRow = ChangeDataModule.ChangesDataTable[0]; //Get row from data table.

            //Add data to sheet.
            Rng = WkSheet.get_Range("C4");
            Rng.Value2 = GenerateJobReference(JobID, TableRow.CreationDate);

            Rng = WkSheet.get_Range("C5");
            Rng.Value2 = TableRow.TPReference.ToString();

            Rng = WkSheet.get_Range("F4");
            Rng.Value2 = TableRow.CreationDate.ToShortDateString();

            Rng = WkSheet.get_Range("F5");
            Rng.Value2 = TableRow.Deadline.ToShortDateString();

            Rng = WkSheet.get_Range("I4");
            Rng.Value2 = TableRow.JobStatus.ToString();

            Rng = WkSheet.get_Range("I5");
            Rng.Value2 = (TableRow.Deadline < DateTime.Now) ? "Yes" : "No";
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Writing An Excel Workbook

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,361
  • Joined: 12-December 12

Re: Writing An Excel Workbook

Posted 12 April 2013 - 04:59 AM

There is no looping within your function so it appears that you must be calling the function multiple times. What triggers, or calls, this function?

If it is from a button-click then maybe your Enter key gets stuck :)

This post has been edited by andrewsw: 12 April 2013 - 05:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,993
  • Joined: 05-May 12

Re: Writing An Excel Workbook

Posted 12 April 2013 - 05:24 AM

Perhaps the function posted above is hooked up to a TextChanged or Selectionchanged event?
Was This Post Helpful? 0
  • +
  • -

#4 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: Writing An Excel Workbook

Posted 14 April 2013 - 08:34 AM

The code posted above is called from a button click event.

I tested on another machine and the same thing happens so doesn't appear to be a case of sticky keys.

I'm guessing it's something in the code as it creates things in excel that im not telling it to. All it's supposed to do is create a new workbook using a template from my project folder and add some data to it. Instead it creates a new workbook with loads of sheets and charts etc and just keeps going until it's created too many and crashes.

Matt
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,361
  • Joined: 12-December 12

Re: Writing An Excel Workbook

Posted 14 April 2013 - 08:42 AM

Perhaps your ExportTemplate has its own code that runs.. or even the Excel Application itself.

This post has been edited by andrewsw: 14 April 2013 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1