473,378 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do you create an excel file in C#?

103 100+
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.

Expand|Select|Wrap|Line Numbers
  1.     oXL = new Microsoft.Office.Interop.Excel.Application();
  2.             oXL.Visible = false;
  3.             oWK = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(System.Reflection.Missing.Value));
  4.             oWK.SaveAs(path + "\\" + fileName);
  5.             oWS = (Microsoft.Office.Interop.Excel._Worksheet)oWK.Worksheets.get_Item(1);
  6.             oWS.Cells[1, 1] = "Student ID";
  7.             oWS.Cells[1, 2] = "Student Name";
  8.             oWK.Save();
  9.  
The line that I am having issues with is
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. oWS = (Microsoft.Office.Interop.Excel._Worksheet)oWK.Worksheets[1];
. This line hade no luck either. The issue I am having is the error regarding InvalidOperationException: Dynamic operations can only be performed in homogenous AppDomain.

Any help would be greatly appreciated.
Dec 28 '13 #1
2 7283
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
Jan 1 '14 #2
leo23
1
Two ways to achieve this.
1).If you have Microsoft Excel Object Library installed,then can check the source code below
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Windows.Forms;
  3. using Excel = Microsoft.Office.Interop.Excel; 
  4.  
  5. namespace WindowsApplication1
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void button1_Click(object sender, EventArgs e)
  15.         {
  16.             Excel.Application xlApp ;
  17.             Excel.Workbook xlWorkBook ;
  18.             Excel.Worksheet xlWorkSheet ;
  19.             object misValue = System.Reflection.Missing.Value;
  20.  
  21.             xlApp = new Excel.ApplicationClass();
  22.             xlWorkBook = xlApp.Workbooks.Add(misValue);
  23.  
  24.             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  25.             xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
  26.  
  27.             xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  28.             xlWorkBook.Close(true, misValue, misValue);
  29.             xlApp.Quit();
  30.  
  31.             releaseObject(xlWorkSheet);
  32.             releaseObject(xlWorkBook);
  33.             releaseObject(xlApp);
  34.  
  35.             MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
  36.         }
  37.  
  38.         private void releaseObject(object obj)
  39.         {
  40.             try
  41.             {
  42.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
  43.                 obj = null;
  44.             }
  45.             catch (Exception ex)
  46.             {
  47.                 obj = null;
  48.                 MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
  49.             }
  50.             finally
  51.             {
  52.                 GC.Collect();
  53.             }
  54.         }
  55.     }
  56. }
  57.  

REGARDS
Apr 10 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Cathy | last post by:
Hi All, Can anyone tell me how to create an Excel (.xls) file from a VB.Net app? I have a SQL Server database that contains a table with the data I want to export. How do I create the file? Any...
0
by: I Decker | last post by:
Hi all, Hope this is the right group. I am writing a program in c# to open create an excel document, enter some data, save it and then email it as an attachment. I have successfully created...
2
by: TJ | last post by:
Hi, Basically, I want to create excel file on the fly, then force users to download it without using automation. Here is one of ways.. System.IO.StringWriter sw = new...
0
by: =?Utf-8?B?Tml5YXpp?= | last post by:
Hi all, Does anyone know how to embed the Excel file in VB.NET 2005 Resources? Here is what I am doing at the moment: 1) In my VB.NET 2005 project code I load the excel with it path into...
4
by: Rahul | last post by:
I want to create an excel file for report in asp.net 2.0 ... without having office installed on the server. Rahul
0
by: Paul | last post by:
Hello, I have a VB 6 app that creates Excel files to export data that works fine. When I ported it to VB.NET it creates the file, but seems to finish writing the data and releasing the file...
7
by: rameshvummadi | last post by:
I want to create an excel file (Microsoft Excel) with tabs using perl. I need to run my perl script in the Unix environment. I can do it with Spreadsheet::WriteExcel. Can I create the excel file...
2
by: phanimadhav | last post by:
Hello Experts, I developed following code for excel file creation by using Dataset.But i got the following error(NullRefernec Exception was caught) at the response.End() My...
1
by: farnena | last post by:
I want to create a excel file every month and update details on certain events in a web application. 1. I dunno how to create Excel file in Web environment. 2. When I was trying to update the...
1
by: anandmadhu | last post by:
I am using the following code to create an excel file on the share on the remote win2007 professional. use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Variant; use...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.