SoFunction
Updated on 2025-03-07

C# efficiently export example code of multidimensional table header excel


[DllImport("", CharSet = )]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        private void ExportToExcel(string fielName)
        {
//Instantiate an object
            excel = new ();

            try
            {
                if (dgv_Result.DataSource == null)
                    return;
                if (dgv_Result. == 0)
                    return;


//A new workbook is added. Workbook is saved directly, and the save dialog box will not pop up. Adding Application will pop up the save dialog box. If the value is false, an error will be reported.
                xlBook = (true);

//1. Add the table header
                [1, 1] = tyclass;
                for (int i = 0; i < dgv_Result.; i++)
                {

                    [2, i + 1] = dgv_Result.Columns[i].Name;
                }


#region 2. Implement Excel multi-dimensional table headers and adopt the method of merging cells
                sheet = ();
                excelRange = sheet.get_Range([1, 1], [1, 2]);
                excelRange1 = sheet.get_Range([1, 3], [1, 4]);
                excelRange2 = sheet.get_Range([1,5], [1, 6]);
                excelRange3 = sheet.get_Range([1,7], [1, 8]);
                excelRange4 = sheet.get_Range([1, 2], [1, 3]);
                excelRange5 = sheet.get_Range([1, 6], [1, 7]);
                excelRange6 = sheet.get_Range([1, 4], [1,5]);
                ();
                ();
                ();
                ();
                ();
                ();
                ();
columnRange = sheet.get_Range("A1", "H2");   //Get Range Range A-H represents 1-8 columns, 1-2 represents several rows across
                 = ;
                 = 10;
                 = true;

                #endregion

               
#region 3. Adding row data and directly assigning values ​​to Range can improve efficiency

range = sheet.get_Range("A3", "H" + (dgv_Result. + 2).ToString());   //Get the Range Range
                string[,] AryData = new string[dgv_Result.-1, dgv_Result.];
                for (int i = 0; i < dgv_Result. - 1; i++)
                {
                    for (int j = 0; j < dgv_Result.; j++)
                    {

                        AryData[i,j] = dgv_Result.Rows[i].Cells[j].();
                    }
                }
                range.Value2 = AryData;
                = ;
                #endregion

();//Set the column width of the Excel table
= 1;//Set Excel cell alignment
= false; //Set the question prompt box that prohibits pop-up save and overwrite.
                = false;
//Save excel file
                (fielName, , , , , , , , , , , );
("Export was successful!", "Tip");
            }
            catch (Exception ex)
            {
(, "Error prompt");
            }
            finally
            {
                IntPtr pt = new IntPtr();
                int k = 0;
                GetWindowThreadProcessId(pt, out k);
                p = (k);
                ();


            }

        }