Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey Guys

I'm very new in programming with C# and I don't really know much about its commands. Can someone help me please? I want to create an Excel-file and fill cells with different values.
I allready tried the following code:

C#
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.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 
    }
}


This didn't work. I always get the message "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))". I'm really confused and need to finish the code as soon as possible.

Thanks for every help.

Greetings from
Epanjohura
Posted
Comments
Singh_Vinay 20-Jun-13 4:14am    
Hi Epanjohura,

can u add on which line this exception is coming ?
epanjohura 20-Jun-13 4:16am    
yes of corse, it happens on the 21st line:

xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
epanjohura 20-Jun-13 4:41am    
and I added the Reference (COM) Microsoft Excel 14.0 Object Library (1.7)

Hey Guys

I solved my problem.
Well, it was the same code but including two other commands (row 7 to 10):

C#
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);

...


Now it works.

Greetings from
Epanjohura
 
Share this answer
 
v2
Comments
unexpected_ 1-Oct-14 3:16am    
tnx alot it saved my day

so kindness of you sharing it
Andre Mesquita 26-Dec-14 10:35am    
Great! Clean code.
 
Share this answer
 
Comments
epanjohura 20-Jun-13 5:18am    
thanks, but isn't this about visual basic?
 
Share this answer
 
Comments
epanjohura 20-Jun-13 5:19am    
thanks for your help.. I read it, but still have no clue
This discussion[^], might help you out.
 
Share this answer
 
Comments
epanjohura 20-Jun-13 5:19am    
thank you :( i still have no idea how to solve this problem
Excel.Application xlApp;
Excel.Workbook xlWorkBook=null;
Excel.Worksheet xlWorkSheet=null;
object misValue = System.Reflection.Missing.Value;
 
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
 
Share this answer
 
v2
Comments
CHill60 30-Nov-14 17:45pm    
Is this meant to be a solution to this 18month old resolved question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900