Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
- using Excel = Microsoft.Office.Interop.Excel;
- namespace check_disp
- {
- public partial class check6 : Form
- {
- MySqlConnection myscon = new MySqlConnection("datasource=localhost;port=3306;initial catalog=test;username=root;password=");
- public check6()
- {
- InitializeComponent();
- }
- private void check6_Load(object sender, EventArgs e)
- {
- try
- {
- MySqlDataAdapter adapt = new MySqlDataAdapter("SELECT * FROM test_tbl", myscon);
- myscon.Open();
- DataSet dset = new DataSet();
- adapt.Fill(dset, "test_tbl");
- dataGridView1.DataSource = dset.Tables["test_tbl"];
- myscon.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Excel.Application xlApp;
- Excel.Workbook xlWorkBook;
- Excel.Worksheet xlWorkSheet;
- object misValue = System.Reflection.Missing.Value;
- xlApp = new Excel.Application();
- xlWorkBook = xlApp.Workbooks.Add(misValue);
- xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
- int i = 0;
- int j = 0;
- for (i = 0; i <= dataGridView1.RowCount - 1; i++)
- {
- for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
- {
- DataGridViewCell cell = dataGridView1[j, i];
- xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
- }
- }
- xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
- xlWorkBook.Close(true, misValue, misValue);
- xlApp.Quit();
- releaseObject(xlWorkSheet);
- releaseObject(xlWorkBook);
- releaseObject(xlApp);
- MessageBox.Show("Excel file created , you can find the file c:\csharp.net-informations.xls");
- }
- private void releaseObject(object obj)
- {
- try
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
- obj = null;
- }
- catch (Exception ex)
- {
- obj = null;
- MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
- }
- finally
- {
- GC.Collect();
- }
- }
- }
- }
- xlApp = new Excel.Application();
Add Comment
Please, Sign In to add comment