SoFunction
Updated on 2025-03-07

How to execute excel macro template in c#


namespace ExcelTest
{
    public class ExelTemplate
    {
        private static ExcelApp;//Define a Excel Application object
        private static Excel._Workbook ExcelWB;//define a Excel workbook object
        private static Excel._Worksheet ExcelWS;//define a Excel workbook worksheet
//Put the master data required for the report into dtHeader, the detailed data into dtDetail, the call template name is strTemplateFileName, and the exported report name is strOutFileName
        public static bool FillContent(string strTemplateFileName, string strOutFileName, dtHeader, dtDetail)
        {
            bool flag = true;
            FileInfo fileInfo = new FileInfo(strTemplateFileName);
            CurrentCI = ;
            = new ("en-US");
            try
            {
                //Start Excel and get Application object.               
                ExcelApp = new ();
                  = false;

                //Get a new workbook.
                ExcelWB = (Excel._Workbook)((strTemplateFileName));

//Fill content. Note that the header and Detail here correspond to the two Sheet pages of Header and Detail in the template file respectively. These two Sheets are specially used to store clear master data or detailed data.
                if (!FillWorksheet("Header", dtHeader)) return false;
                if (!FillWorksheet("Detail", dtDetail)) return false;
                //Run macro.

                ("SetData", , , , , , , , , , , , , , , , , , , , , , , , , , , , , , );

                = false;
                //=========================================================================================================
                //Delete Header and Detail.
                //((Excel._Worksheet)["Header Information"]).Delete();              
                //((Excel._Worksheet)["Detail Information"]).Delete();
 

                //=========================================================================================================
                = true;
                //Delete old file.
                (strOutFileName);
                //Save excel file.
                (strOutFileName, , , , , , , , , , , );

                // Quit Excel.
                (false, strOutFileName, );//*******************            
                MarshalReleaseComObject(ExcelWB);
                ();

               
                //Kill excel application.
                //KillProcess("EXCEL");//******************

            }
            catch (Exception ex)
            {
                throw ex;  
                flag = false;
            }
            finally
            {
                MarshalReleaseComObject(ExcelApp);
                ();
            }
            return flag;
        }
//Put the master data required for the report into dtHeader, the detailed data into dtDetail, the call template name is strTemplateFileName, and the exported report name is strOutFileName
        public static bool FillContent(string strTemplateFileName, string strOutFileName, dsdata)
        {
            bool flag = true;
            FileInfo fileInfo = new FileInfo(strTemplateFileName);
            CurrentCI = ;
            = new ("en-US");
            try
            {
                //Start Excel and get Application object.               
                ExcelApp = new ();
                = false;
                //Get a new workbook.
                 ExcelWB = (Excel._Workbook)((strTemplateFileName));
                 //Fill content.
                //if (!FillWorksheet("Header", dtHeader)) return false;
                //if (!FillWorksheet("Detail", dtDetail)) return false;
//Fill content. Note that the Sheet page in the corresponding template file is not stated here, but [i].TableName is the name of the Sheet page, which is convenient and flexible. In this way, multiple Sheets are specially used to store the main data or detailed data.
                for (int i = 0; i < ; i++)
                {
                    if (!FillWorksheet([i].TableName, [i]))
                        return false;
                }
                //Run macro.

                ("SetData", , , , , , , , , , , , , , , , , , , , , , , , , , , , , , );

                = false;
                //=========================================================================================================
                //Delete Header and Detail.
                //((Excel._Worksheet)["Header Information"]).Delete();              
                //((Excel._Worksheet)["Detail Information"]).Delete();
 

                //=========================================================================================================
                = true;
                //Delete old file.
                (strOutFileName);
                //Save excel file.

                (strOutFileName, , , , , , , , , , , );

                // Quit Excel.
                (false, strOutFileName, );//*******************            
                MarshalReleaseComObject(ExcelWB);
                ();

            }
            catch (Exception ex)
            {
                throw ex;
                flag = false;
            }
            finally
            {
                MarshalReleaseComObject(ExcelApp);
                ();
            }
            return flag;
        }
//When using Excel to export reports, the Excel process on the server is prone to death, resulting in the resource being unable to be released. Therefore, this method is needed to release the dead process.
        private static void MarshalReleaseComObject(object objCom)
        {
            try
            {
                int i = 1;
                if (objCom != null && (objCom))
                {
                    do
                    {
                        i = (objCom);
                    } while (i > 0);
                }
            }
            finally
            {
                objCom = null;
            }
        }
}