Home >Backend Development >C#.Net Tutorial >C#_Call an encapsulated class to implement the function of exporting Excel tables

C#_Call an encapsulated class to implement the function of exporting Excel tables

php是最好的语言
php是最好的语言Original
2018-08-03 11:29:522454browse

Preface

The function of exporting Excel tables is available in most forms. If a class is encapsulated, calling this class directly when using it is not more convenient? , which also reduces the duplication of code, why not?

Operation

First add a reference, select Microsoft Office 16.0 Object Library, and Microsoft Excel 16.0 Object Library in com.

C#_Call an encapsulated class to implement the function of exporting Excel tables

Add namespace:

using Microsoft.Office.Interop.Excel;//导出Excel
using Microsoft.Office.Core;
using System.Data.OleDb;
using System.Windows.Forms;

Create a new class and name it outputExcel, the code is as follows:

public  class outputExcel
    {
        //导出excel
        public void RExcel(string name, DataGridView dgv)
        {
            //总可见行列数
            int rowCount = dgv.Rows.GetRowCount(DataGridViewElementStates.Visible);
            int colCount = dgv.Columns.GetColumnCount(DataGridViewElementStates.Visible);

            //如果没有数据
            if (dgv.Rows.Count == 0 || rowCount == 0)
            {
                MessageBox.Show("表中没有数据", "提示");
            }
            else
            {
                //创建文件的路径
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "excel files(*.xlsx)|*.xlsx";
                save.Title = "请选择要导出数据的位置";
                save.FileName = name + DateTime.Now.ToLongDateString();

                if (save.ShowDialog() == DialogResult.OK)
                {
                    string fileName = save.FileName;

                    //创建excel对象
                    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                    if (excel == null)
                    {
                        MessageBox.Show("Excel无法启动", "提示");
                        return;
                    }

                    //创建工作薄
                    Microsoft.Office.Interop.Excel.Workbook excelBook = excel.Workbooks.Add(true);
                    Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets[1];

                    //生成字段名
                    int k = 0;
                    for (int i = 0; i < dgv.ColumnCount; i++)
                    {
                        if (dgv.Columns[i].Visible)//不导出隐藏列
                        {
                            excel.Cells[1, k + 1] = dgv.Columns[i].HeaderText;
                            k++;
                        }
                    }

                    //填充数据
                    for (int i = 0; i < dgv.RowCount; i++)
                    {
                        k = 0;
                        for (int j = 0; j < dgv.ColumnCount; j++)
                        {
                            if (dgv.Columns[j].Visible)//不导出隐藏的列
                            {
                                if (dgv[j, i].ValueType == typeof(string))
                                {
                                    excel.Cells[i + 2, k + 1] = "" + dgv[j, i].Value.ToString();
                                }
                                else
                                {
                                    excel.Cells[i + 2, k + 1] = dgv[j, i].Value.ToString();
                                }
                            }
                            k++;
                        }
                    }

                    try
                    {
                        excelBook.Saved = true;
                        excelBook.SaveCopyAs(fileName);
                        MessageBox.Show("导出成功!");
                    }
                    catch
                    {
                        MessageBox.Show("导出文件失败,文件可能正在使用中", "提示");
                    }
                }
            }
        }


    }

When the form needs to use this Function, you need to write the following code:

private void btnoutExcel_Click(object sender, EventArgs e)
        {
            outputExcel form1 = new outputExcel();
            form1.RExcel("", dataGridView1);

        }

Related articles:

[c# tutorial] C# data type

mysql Connector C/C multi-threaded packaging

The above is the detailed content of C#_Call an encapsulated class to implement the function of exporting Excel tables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn