0

Hi everyone i have a problem on C# Excel usage.I'm copying worksheet because i have to change someting in worksheet. After the copy proccess i cant open worksheet on my application. Please help thank you :)

There is open file code

private void btnDosyaSec_Click(object sender, EventArgs e)
    {
        try
        {
            chkBunlariYaz.Checked = false;
            chkOtoSayfa.Checked = false;
            txtOtelAdi.Enabled = false;
            chkSecilendenSonrasiniYaz.Checked = false;
            txtYazilacaklar.Clear();
            Excel.Application _app;
            Excel.Workbooks _books;
            Excel.Workbook _book;
            Excel.Sheets _sheets;
            Excel.Worksheet _sheet;
            _app = new Excel.Application();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                yol = openFileDialog1.FileName;
                txtAdres.Text = yol;
                kullanilacakYol = Environment.CurrentDirectory + "\\exceller\\" + rnd.Next(0, 999999999) + ".xlsx";
                File.Copy(yol, kullanilacakYol);
                _books = _app.Workbooks;
                _book = _books.Open(kullanilacakYol, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                _sheets = _book.Worksheets;
                cmSayfaNumarasi.Items.Clear();
                int sayfaMiktari = _book.Sheets.Count;
                for (int i = 1; i <= sayfaMiktari; i++)
                {
                    cmSayfaNumarasi.Items.Add(i.ToString());
                    _sheet = (Excel.Worksheet)_sheets[i];
                    _sheet.Select(Type.Missing);
                    otelAdi = _sheet.Cells[3, 1].Value.ToString();
                    Excel.Range range = _sheet.get_Range("A1:Q3", Type.Missing);
                    range.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
                    NAR(range);
                }
                _book.Save();
                _book.Close(false, Type.Missing, Type.Missing);
                NAR(_book);
                _app.Quit();
                NAR(_app);
                cmSayfaNumarasi.SelectedIndex = 0;
                sayfaNumarasi = int.Parse(cmSayfaNumarasi.SelectedItem.ToString());
                doldur(kullanilacakYol, sayfaNumarasi);
                otelAdi = otelAdi.Replace("FİRMA ADI: ", "").Replace("                                   ", "");
                txtOtelAdi.Text = otelAdi;
            }
        }
        catch (Exception ee)
        {
            MessageBox.Show("Bir hata ile karşılaşıldı.\nHata Kodu=btnDosyaSec_Click \n" + ee.Message, "Hata !!!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

There is error picture(Turkish sorry) enter image description here

And the Worksheet it say the file is maked readonlt from Bazes(thats my username on pc) Open read only or click report to if this file don't using any other programs enter image description here

Thank you again

  • In your code,you have used NAR(_book), what does it do ? I suspect because of this references are not getting released. – Shashi Bhushan Feb 05 '17 at 14:46
  • I suspect that that `NAR(COMobj)` is an attempt at deterministic COM object release. Such a technique often fails as you invariably miss releasing some COM object. It is best to let the .Net garbage collector do this for you. See [this post](http://stackoverflow.com/a/36578663/2592875) for a recommended procedure. – TnTinMn Feb 05 '17 at 21:11
  • What's the best excel proccess library ? – Gökhan Gökce Feb 06 '17 at 17:45
  • The best one is [the one Microsoft provides for working with xlsx files](https://msdn.microsoft.com/en-us/library/office/bb448854.aspx) IMHO. – Scott Chamberlain Feb 09 '17 at 20:00

1 Answers1

0

Maybe you can try a finally after your try - catch when you close your Excel

Try this:

finally
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
}
ghostwar 88
  • 547
  • 1
  • 6
  • 17