I added Microsoft.Office.Interop.Excel reference to my project. My codes are working in other project very well. But the same codes are not working in the new project. It opens an empty excel file. What is the reason in your opinion?
using System;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
private void Button1_Click(object sender, EventArgs e)
{
var mySheet = Path.Combine(Directory.GetCurrentDirectory(), "Excel/Personelaktar.xlsx");
Excel.Application xlApp = new Excel.Application()
{
Visible = true
};
if (xlApp == null)
{
MessageBox.Show("Excel dosyasi yüklenmemis!");
return;
}
}
Mehmet FatihPosted Jun 10, 2023, 1:35 PM
I have solved like that.
var mySheet = Path.Combine(Directory.GetCurrentDirectory(), "Dosya/Paktar.xlsx");
Excel.Application xlApp = new Excel.Application
{
Visible = true
};
if (xlApp == null)
{
MessageBox.Show("Excel dosyasi yüklenmemis!");
return;
}
try
{
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(mySheet);
}
catch (Exception err)
{
MessageBox.Show("Hata! " + err.Message, "Hata Olustu", MessageBoxButtons.OK, MessageBoxIcon.Error);
{
xlApp.Quit();
}
}
Tuhin PaulPosted Jun 10, 2023, 11:35 AM
You're using the Directory.GetCurrentDirectory() method to get the current directory. This might not always return the directory you expect, depending on how your project is structured and where the file is located. To ensure that you're using the correct file path, you can try using the full path instead of relying on the current directory.
Tuhin PaulPosted Jun 10, 2023, 11:34 AM
One possible reason for the code not working as expected in the new project could be related to the Excel file path. Make sure that the file path you're providing is correct and that the file "Personelaktar.xlsx" exists in the specified location.