C#Excel复制后保持打开状态

时间:2017-02-05 10:56:11

标签: c# excel forms winforms file

大家好我对C#Excel的使用有疑问。我正在复制工作表,因为我必须更改工作表中的内容。复制过程后我无法在我的应用程序上打开工作表。请帮忙谢谢:)

有开放文件代码

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);
        }
    }

有错误图片(土耳其人抱歉) enter image description here

并且工作表它说该文件是来自Bazes的readonlt(这是我在PC上的用户名)打开只读或点击报告,如果此文件不使用任何其他程序 enter image description here

再次感谢您

1 个答案:

答案 0 :(得分:0)

也许您可以在关闭Excel后finally后尝试try - catch

试试这个:

finally
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
}