zoukankan      html  css  js  c++  java
  • DaGridView导出Excel

    首先添加引用Microsoft.Office.Interop.Excel;

            /// <summary>
            /// 将dataGridView导出Excel
            /// </summary>
            /// <param name="dataGridView">dataGridView</param>
            /// <returns>返回是否导出成功,成功返回True,失败返回False</returns>
            public bool ExportExcel(DataGridView dataGridView)
            {
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.Filter = "Excel files(*.xls)|*.xls";
                saveDialog.FilterIndex = 0;
                saveDialog.RestoreDirectory = true;
                saveDialog.CreatePrompt = true;

                bool fileSaved = false;

                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    if (dataGridView == null)
                    {
                        return false;
                    }

                    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

                    if (xlApp == null)
                    {
                        return false;
                    }

                    Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                    Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

                    for (int i = 0; i < dataGridView.Columns.Count; i++)
                    {
                        worksheet.Cells[1, i + 1] = dataGridView.Columns[i].HeaderText;
                    }

                    for (int r = 0; r < dataGridView.Rows.Count; r++)
                    {
                        for (int i = 0; i < dataGridView.Columns.Count; i++)
                        {
                            worksheet.Cells[r + 2, i + 1] = dataGridView.Rows[r].Cells[i].Value.ToString();
                        }
                    }

                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveDialog.FileName);
                        fileSaved = true;
                    }
                    catch (Exception)
                    {
                        fileSaved = false;
                    }

                    if (worksheet != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                        worksheet = null;
                    }
                    if (workbook != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                        workbook = null;
                    }
                    if (workbooks != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
                        workbooks = null;
                    }
                    xlApp.Application.Workbooks.Close();
                    xlApp.Quit();
                    if (xlApp != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                        xlApp = null;
                    }
                    GC.Collect();
                }
                return fileSaved;
            }

  • 相关阅读:
    将B表中符合条件的数据更新到A表中
    oracle : 无法更新 ON 子句中引用的列
    查看Oracle 版本信息
    将分组的其他内容以字符串形式展示
    将datatable 保存为 Excel文件(高效率版本)
    winform 中一个窗口嵌套到另一个窗口
    一个切换bool属性的小方法
    html5相关知识点的总结(有一些错误或者不足的地方)
    get和post的区别
    cookie的作用
  • 原文地址:https://www.cnblogs.com/rumeng/p/2750792.html
Copyright © 2011-2022 走看看