Şekil 1
Şekil 2
Form1.cs
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 System.Data.OleDb;
using mexcel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet TabloAl;
private void button1_Click(object sender, EventArgs e)
{
OleDbConnectionStringBuilder sb = new OleDbConnectionStringBuilder();
sb.Provider = "Microsoft.Jet.Oledb.4.0";
sb.DataSource = "bilgi.mdb";
OleDbConnection yenibaglanti = new OleDbConnection(sb.ConnectionString);
string sorgu = "Select * From personel";
OleDbDataAdapter verial = new OleDbDataAdapter(sorgu, yenibaglanti);
TabloAl = new DataSet();
verial.Fill(TabloAl, "tablo");
mexcel.Application exceluygulama = new mexcel.Application();
mexcel.Workbook excelkitap = default(mexcel.Workbook);
excelkitap = exceluygulama.Workbooks.Add(true);
mexcel.Worksheet excelsayfa = default(mexcel.Worksheet);
excelsayfa = (mexcel.Worksheet)exceluygulama.ActiveSheet;
exceluygulama.Visible = true;
int satirnumarasi = 1;
int sutunnumarasi = 0;
foreach (DataColumn sutun in TabloAl.Tables[0].Columns)
{
sutunnumarasi += 1;
excelsayfa.Cells[1, sutunnumarasi] = sutun.ColumnName;
}
foreach (DataRow satir in TabloAl.Tables[0].Rows)
{
satirnumarasi += 1;
sutunnumarasi = 0;
foreach (DataColumn sutun in TabloAl.Tables[0].Columns)
{
sutunnumarasi += 1;
excelsayfa.Cells[satirnumarasi, sutunnumarasi] = satir[sutun.ColumnName].ToString();
}
}
object Missing = Type.Missing;
XlFileFormat format = XlFileFormat.xlWorkbookDefault;
excelsayfa.Application.Visible = true;
excelsayfa.SaveAs("C:\\bilgi.xls", format, Missing, Missing, Missing, false, XlSaveAsAccessMode.xlNoChange, Missing, false, Missing );
}
}
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek dileğiyle. Hoşçakalın. Bahadır ŞAHİN
0 comments:
Yorum Gönder