You must Sign In to post a response.
  • Category: .NET

    Close Excel after read in C# windows application

    Hi,

    I am unable to open the excel file after read in C# application. Even after the application was close i cannot open the excel file. How could i do it.

    my coding:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace Excel_to_Notepad
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
    Excel.Application xlapp = new Excel.Application();
    Excel.Workbook xlworkbook = xlapp.Workbooks.Open("D:\\test.xlsx");
    Excel.Worksheet xlworksheet = xlworkbook.Sheets[1];
    Excel.Range xlrange = xlworksheet.UsedRange;

    int rowcount = xlrange.Rows.Count;
    int colcount = xlrange.Columns.Count;

    for (int i = 1; i <= rowcount; i++)
    {
    for (int j = 1; j <= colcount; j++)
    {
    MessageBox.Show(xlrange.Cells[i, j].value2.ToString() + "\t");

    }

    }
    xlworkbook.Close(false, xlapp);
    xlapp.Quit();
    xlapp = null;

    System.GC.Collect();
    GC.WaitForPendingFinalizers();
    }
    }
    }
  • #742866
    Any error

    as you have called GC.Collect so every unused object will be destroyed.

    Please let us know the exact error.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #742933
    No, i did not get any error. Application run smoothly, but after completion of task and quit the application i am unable to open the excel file. I am using XLSX (2007 office version)
    -----------------------------------------------------------------------------
    Regards,
    Gopi A.
    +91 9894315571
    Skype:gopi.net
    http://asaigopi-dotnet.blogspot.in/


  • Sign In to post your comments