从Form窗口的Gridview导出Excel

来源:互联网 发布:推荐几个潮男淘宝店 编辑:程序博客网 时间:2024/05/16 07:56

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Office.Interop.Excel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


private void ButtonSearchOutput_Click(object sender, EventArgs e)

        {
           //导出当前页DataGridView中的数据到EXcel中
            SaveFileDialog saveFileDialog = new SaveFileDialog();//保存对话框
            
            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";//能保存的类型
                saveFileDialog.FilterIndex = 0;//设置默认文件类型顺序
                saveFileDialog.RestoreDirectory = true;//保存对话框记住上次打开的文件地址
                saveFileDialog.CreatePrompt = true;//要保存的文件名不存在,征求用户同意是否创建一个。
                saveFileDialog.Title = "导出到Excel";
                saveFileDialog.ShowDialog();
                string strName = saveFileDialog.FileName;//保存到文件
                if (strName.Length != 0)
                {
                    System.Reflection.Missing miss = System.Reflection.Missing.Value;
                    //创建一个excel
                    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                    excel.Application.Workbooks.Add(true); ;
                    excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
                   
                    if (excel == null)
                    {
                        MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }


                    Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
                    Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
                    Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
                    sheet.Name = "test";
                    
                    //生成字段名称
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText.ToString();//获取列集合的第一行
                    }
                    //填充数据
                    for (int i = 0; i < dataGridView1.RowCount - 1; i++)//获取datagridview获得行数
                    {
                        for (int j = 0; j < dataGridView1.ColumnCount; j++)
                        {
                            if (dataGridView1[j, i].Value == typeof(string))
                            {
                                excel.Cells[i + 2, j + 1] = "" + dataGridView1[i, j].Value.ToString();
                            }
                            else
                            {
                                excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
                            }
                        }
                     //   toolStripProgressBar1.Value += 100 / dataGridView1.RowCount;
                    }
                    sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
                    book.Close(false, miss, miss);
                    books.Close();
                    excel.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                    GC.Collect();
                    MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);}
0 0
原创粉丝点击