Click here to Skip to main content
15,921,452 members
Home / Discussions / C#
   

C#

 
GeneralRe: what to do after a read from a socket Timeout Pin
manustone29-Jun-10 23:16
manustone29-Jun-10 23:16 
AnswerRe: what to do after a read from a socket Timeout Pin
Ennis Ray Lynch, Jr.25-Jun-10 4:15
Ennis Ray Lynch, Jr.25-Jun-10 4:15 
GeneralRe: what to do after a read from a socket Timeout Pin
manustone29-Jun-10 23:16
manustone29-Jun-10 23:16 
QuestionCollection of unique ids Pin
Chiman125-Jun-10 0:38
Chiman125-Jun-10 0:38 
AnswerRe: Collection of unique ids Pin
Luc Pattyn25-Jun-10 0:51
sitebuilderLuc Pattyn25-Jun-10 0:51 
AnswerRe: Collection of unique ids Pin
OkkiePepernoot25-Jun-10 0:52
OkkiePepernoot25-Jun-10 0:52 
AnswerRe: Collection of unique ids Pin
David Skelly25-Jun-10 2:03
David Skelly25-Jun-10 2:03 
QuestionCom Exception - excel Pin
KaurGurpreet24-Jun-10 23:26
KaurGurpreet24-Jun-10 23:26 
A simple code to export data to excel
public partial class Form1 : Form
  {

    Microsoft.Office.Interop.Excel.Application _objAppln;
    Workbook _objWorkBook;
    Workbooks _objWorkBooks;
    Worksheet _objWorkSheet;


    public Form1()
    {
      InitializeComponent();
      InitializeExcelObjectModel();
    }

    ~Form1()
    {
      DisposeExcelObjects();
    }

    void SaveExcel()
    {

      _objAppln.DisplayAlerts = false;//Since, we are using SaveFileDialog's overwrite prompt(control is on view). 
      
      _objWorkBook.SaveAs("C:\\tmp.xls",
        XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlNoChange,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
      _objWorkBook.Close(true, "C:\\tmp.xls", false);
      _objAppln.DisplayAlerts = true;//restore back for other display alerts 
    }

    private void InitializeExcelObjectModel()
    {

      _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file   
      //_objAppln.Visible = true; 
      if (_objAppln != null)
      {
        _objWorkBooks = _objAppln.Workbooks;
        _objWorkBook = _objWorkBooks.Add(Type.Missing); // To add workbook with sheets in excel file         
        _objWorkSheet = (Worksheet)_objAppln.ActiveSheet; // To get the current active sheet in excel file              

      }
    }

    public void DisposeExcelObjects()
    {

      System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkSheet);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkBook);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(_objWorkBooks);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(_objAppln);
      _objWorkSheet = null; _objWorkBooks = null; _objWorkBooks = null; _objAppln = null;
    }

    private void button1_Click(object sender, EventArgs e)
    {

      Range objRange=null;
      string cell1 = string.Empty, cell2 = string.Empty;
      string[] chars = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T" };


      for (int row = 1; row < 1000; row++)
      {
        for (int column = 0; column < 20; column++)
        {
          cell1 = chars[column] + row.ToString();
          objRange = _objWorkSheet.get_Range(cell1, cell1);
          objRange.Value2 = cell1;
          
          
        }
      }
      System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange);
      objRange = null;
      SaveExcel();
    }
  }


Step 1: Add the above code to a solution and add reference for Microsoft.Office.Interop.Excel.dll
Step 2: Run the application and Execute the code in button1_Click.
Step 3: While the processing is on, open any other excel file and just click on cells here and there.
COM exception occurs. Anything I am doing wrong above?

How to resolve?
Gurpreet

AnswerRe: Com Exception - excel Pin
freakyit24-Jun-10 23:38
freakyit24-Jun-10 23:38 
GeneralRe: Com Exception - excel Pin
KaurGurpreet25-Jun-10 0:00
KaurGurpreet25-Jun-10 0:00 
AnswerRe: Com Exception - excel Pin
OkkiePepernoot25-Jun-10 0:30
OkkiePepernoot25-Jun-10 0:30 
GeneralRe: Com Exception - excel Pin
KaurGurpreet25-Jun-10 0:35
KaurGurpreet25-Jun-10 0:35 
GeneralRe: Com Exception - excel Pin
OkkiePepernoot25-Jun-10 0:45
OkkiePepernoot25-Jun-10 0:45 
GeneralRe: Com Exception - excel Pin
KaurGurpreet25-Jun-10 0:52
KaurGurpreet25-Jun-10 0:52 
GeneralRe: Com Exception - excel Pin
freakyit25-Jun-10 3:25
freakyit25-Jun-10 3:25 
GeneralRe: Com Exception - excel Pin
KaurGurpreet27-Jun-10 19:07
KaurGurpreet27-Jun-10 19:07 
GeneralRe: Com Exception - excel Pin
EliottA25-Jun-10 3:31
EliottA25-Jun-10 3:31 
AnswerRe: Com Exception - excel Pin
KaurGurpreet27-Jun-10 19:23
KaurGurpreet27-Jun-10 19:23 
GeneralRe: Com Exception - excel Pin
EliottA28-Jun-10 1:25
EliottA28-Jun-10 1:25 
GeneralRe: Com Exception - excel Pin
KaurGurpreet28-Jun-10 2:09
KaurGurpreet28-Jun-10 2:09 
GeneralRe: Com Exception - excel Pin
EliottA28-Jun-10 2:18
EliottA28-Jun-10 2:18 
Question"Unable to load the project file xx.csproj" Pin
Emmet_Brown24-Jun-10 22:36
Emmet_Brown24-Jun-10 22:36 
AnswerRe: "Unable to load the project file xx.csproj" Pin
Praveen Raghuvanshi24-Jun-10 22:49
professionalPraveen Raghuvanshi24-Jun-10 22:49 
GeneralRe: "Unable to load the project file xx.csproj" Pin
Emmet_Brown24-Jun-10 23:02
Emmet_Brown24-Jun-10 23:02 
AnswerRe: "Unable to load the project file xx.csproj" Pin
Pete O'Hanlon24-Jun-10 22:51
mvePete O'Hanlon24-Jun-10 22:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.