简体   繁体   English

在2 Tab Control C#中将数据从DataGridView导出到Excel

[英]Export data to Excel from DataGridView in 2 Tab Control C#

I want to ask something I can't figure out how to fix it. 我想问一些我不知道如何解决的问题。

Please help me, so I need to develop an app meanwhile it's been ready to export to excel, but this app only can export the file one by one of the Tabs, so I need to join them manually, I need to know how to make it to be just 1 file and maybe can be 2 sheets excel file, 请帮帮我,所以我需要开发一个应用程序,同时准备将其导出到excel,但是此应用程序只能按一个选项卡之一导出文件,因此我需要手动加入它们,我需要知道如何制作只能是1个文件,也可以是2张excel文件,

Thanks 谢谢

private void DOCUMENT_GENERATOR(object sender, string DocPath)
{
    try
    {
        foreach string DateTimeID = string.Format("{0:yyyyMMMdd}", DateTime.Now) + "_" + string.Format("{0:hhmmss}", DateTime.Now);
        var resul = from c in Class.TreeMenusCls.items where c.ID.ToString() == treeView1.SelectedNode.Name select new { c.ID, c.MenuName, c.ParentID, c.Tag, c.CreateCtl, c.HeaderOnly };
        foreach (var cat in resul)
        {
            var get_query = from s in QueryCollectionTable.Select() where Convert.ToInt16(s["KEY_MENUS"]) == MenuIDSelected orderby s["PRIORITY"] select s;
            foreach (var Eq in get_query)
            {
                TabControl Tab;
                Tab = (TabControl)this.Controls.Find("Tab" + Eq["KEY_MENUS"], true)[0];
                Control.ControlCollection SearchCtl = Tab.TabPages[Convert.ToInt16(Eq["PRIORITY"])].Controls;

                foreach (Control c in SearchCtl)
                {
                    if (c.Name == "Grid" + Eq["NO_ID"])
                    {
                        Thread[] Tr = new Thread[0];
                        Array.Resize(ref Tr, Tr.Length + 1);

                        int priority = Convert.ToInt16(Eq["PRIORITY"]);
                        string query_name = Eq["QUERY_NAME"].ToString();
                        string menu_name = cat.MenuName;
                        int formated = Convert.ToInt16(Eq["FORMATED"]);
                        string row_start = Eq["ROW_START"].ToString();
                        string draft_location = Eq["DRAFT_LOCATION"].ToString();
                        string run_macro_name = Eq["RUN_MACRO_NAME"].ToString();

                        if (sender == EXCEL_CMD_GENERATE)
                        {
                            if (formated == 0)
                                Tr[Tr.Length - 1] = new Thread(new ThreadStart(delegate { this.Obj[0].ExcelGenerator((DataGridView)c, Tab, priority, query_name, menu_name, DateTimeID, DocPath); }));
                            else if (formated == 1)
                                Tr[Tr.Length - 1] = new Thread(new ThreadStart(delegate { this.Obj[0].ExcelFormated((DataGridView)c, Tab, priority, query_name, menu_name, DateTimeID, DocPath, formated, row_start, draft_location, run_macro_name); }));
                        }
                        if (sender == TEXT_CMD_GENERATE)
                            Tr[Tr.Length - 1] = new Thread(new ThreadStart(delegate { this.Obj[0].TextGenerator((DataGridView)c, Tab, priority, query_name, menu_name, DateTimeID, DocPath); }));
                        if (sender == CSV_CMD_GENERATE)
                            Tr[Tr.Length - 1] = new Thread(new ThreadStart(delegate { this.Obj[0].CsvGenerator((DataGridView)c, Tab, priority, query_name, menu_name, DateTimeID, DocPath); }));
                        if (sender == XML_CMD_GENERATE)
                            Tr[Tr.Length - 1] = new Thread(new ThreadStart(delegate { this.Obj[0].XmlGenerator((DataGridView)c, Tab, priority, query_name, menu_name, DateTimeID, DocPath); }));
                        Tr[Tr.Length - 1].Start();
                    }
                }
            }
        }
    }
    catch (ArgumentNullException a)
    {
        Console.WriteLine(a.Message);
    }
}

For export any data to excel, you can use ClosedXML.Report. 要将任何数据导出到excel,可以使用ClosedXML.Report。 It is a tool for report generation with which you can easily export any data from your .NET classes to Excel using a XLSX-template. 它是用于生成报告的工具,您可以使用XLSX模板轻松地将.NET类中的任何数据导出到Excel。

See more here https://github.com/ClosedXML/ClosedXML.Report 在此处查看更多信息https://github.com/ClosedXML/ClosedXML.Report

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM