using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
Excel.Application xlApp = new Excel.Application();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (xlApp == null)
{
label1.Text = "Excel Libary is not installed ";
label1.ForeColor = System.Drawing.Color.Red;
}
else
{
label1.Text = "Excel Libary is installed ";
label1.ForeColor = System.Drawing.Color.Green;
}
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "StudentID";
xlWorkSheet.Cells[1, 2] = "Student Name";
xlWorkSheet.Cells[1, 3] = "Marks";
xlWorkSheet.Cells[2, 1] = "1";
xlWorkSheet.Cells[2, 2] = "Ali";
xlWorkSheet.Cells[2, 3] = "50";
xlWorkSheet.Cells[3, 1] = "2";
xlWorkSheet.Cells[3, 2] = "Salman";
xlWorkSheet.Cells[3, 3] = "60";
xlWorkSheet.Cells[4, 1] = "3";
xlWorkSheet.Cells[4, 2] = "Arsalan";
xlWorkSheet.Cells[4, 3] = "100";
xlWorkBook.SaveAs(@"C:\images\docs\salman.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
MessageBox.Show("Excel file created , you can find the file d:\\csharp-Excel.xls");
}
}
}
System.Runtime.InteropServices.COMException
ReplyDeleteHResult=0x80040154
Message=Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Source=mscorlib
StackTrace:
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Simple_Input_Field_App.Form1..ctor() in C:\Users\skbha\source\repos\Simple Input Field App\Simple Input Field App\Form1.cs:line 18
at Simple_Input_Field_App.Program.Main() in C:\Users\skbha\source\repos\Simple Input Field App\Simple Input Field App\Program.cs:line 19
How to solove