Skip to content

Instantly share code, notes, and snippets.

@ebubekirbastama
Last active September 7, 2023 06:51
Show Gist options
  • Save ebubekirbastama/c32c8ffd36cc2be7b5d8af6f95df3a5c to your computer and use it in GitHub Desktop.
Save ebubekirbastama/c32c8ffd36cc2be7b5d8af6f95df3a5c to your computer and use it in GitHub Desktop.
void excellaktar()
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application
{
Visible = false
};
object Missing = Type.Missing;
Workbook workbook = excel.Workbooks.Add(Missing);
Worksheet sheet1 = (Worksheet)workbook.Sheets[1];
int StartCol = 1;
int StartRow = 1;
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
Range myRange = (Range)sheet1.Cells[StartRow, StartCol + j];
myRange.Value2 = dataGridView1.Columns[j].HeaderText;
}
StartRow++;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
Range myRange = (Range)sheet1.Cells[StartRow + i, StartCol + j];
myRange.Value2 = dataGridView1[j, i].Value == null ? "" : dataGridView1[j, i].Value;
myRange.Select();
}
}
// Dosyayı belirli bir konuma kaydet
string dosyaYolu = System.Windows.Forms.Application.StartupPath + "\\EBSCikti.xlsx";
workbook.SaveAs(dosyaYolu);
workbook.Close();
excel.Quit();
Marshal.ReleaseComObject(sheet1);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(excel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment