How do you create an excel file in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Connelly
    New Member
    • Jan 2011
    • 103

    How do you create an excel file in C#?

    Hello,
    I am currently having issues with the following code when trying to create a excel file using C#. This is the code that I have at the moment.

    Code:
        oXL = new Microsoft.Office.Interop.Excel.Application();
                oXL.Visible = false;
                oWK = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(System.Reflection.Missing.Value));
                oWK.SaveAs(path + "\\" + fileName);
                oWS = (Microsoft.Office.Interop.Excel._Worksheet)oWK.Worksheets.get_Item(1);
                oWS.Cells[1, 1] = "Student ID";
                oWS.Cells[1, 2] = "Student Name";
                oWK.Save();
    The line that I am having issues with is
    Code:
    oWS = (Microsoft.Office.Interop.Excel._Worksheet)oWK.Worksheets.get_Item(1);
    If I remove it, I am able to create and save teh workbook in the directory. However, I want to make some changes on a worksheet, and that is where the issue is. I have also tried using the line of code:
    Code:
    oWS = (Microsoft.Office.Interop.Excel._Worksheet)oWK.Worksheets[1];
    . This line hade no luck either. The issue I am having is the error regarding InvalidOperatio nException: Dynamic operations can only be performed in homogenous AppDomain.

    Any help would be greatly appreciated.
  • Ghostwolf
    New Member
    • Jan 2014
    • 5

    #2
    I just tested it, and your code for creating the workbook works just fine.

    So I did a search for "homogenous appdomain" and found http://social.msdn.microsoft.com/For...main?forum=pex The last post on the page describes his resolution.

    HTH

    Comment

    • leo23
      New Member
      • Apr 2014
      • 1

      #3
      Two ways to achieve this.
      1).If you have Microsoft Excel Object Library installed,then can check the source code below
      Code:
      using System;
      using System.Windows.Forms;
      using Excel = Microsoft.Office.Interop.Excel; 
      
      namespace WindowsApplication1
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  Excel.Application xlApp ;
                  Excel.Workbook xlWorkBook ;
                  Excel.Worksheet xlWorkSheet ;
                  object misValue = System.Reflection.Missing.Value;
      
                  xlApp = new Excel.ApplicationClass();
                  xlWorkBook = xlApp.Workbooks.Add(misValue);
      
                  xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                  xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
      
                  xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                  xlWorkBook.Close(true, misValue, misValue);
                  xlApp.Quit();
      
                  releaseObject(xlWorkSheet);
                  releaseObject(xlWorkBook);
                  releaseObject(xlApp);
      
                  MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
              }
      
              private void releaseObject(object obj)
              {
                  try
                  {
                      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                      obj = null;
                  }
                  catch (Exception ex)
                  {
                      obj = null;
                      MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
                  }
                  finally
                  {
                      GC.Collect();
                  }
              }
          }
      }

      REGARDS
      Last edited by Rabbit; Apr 15 '14, 03:05 PM. Reason: Commercial product removed

      Comment

      Working...