欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#执行excel宏模版的方法

程序员文章站 2023-12-22 19:35:10
在项目中如果涉及到用excel开发的报表模版来导出报表数据的话,一般都是在excel报表中使用vba做成宏来进行调用。即先使用excel自带的vba开发好报表模版,然后再将...

在项目中如果涉及到用excel开发的报表模版来导出报表数据的话,一般都是在excel报表中使用vba做成宏来进行调用。即先使用excel自带的vba开发好报表模版,然后再将模版程序放在固定目录下,在管理软件中对这些报表模版进行调用。这样就不需要使用其它报表工具来进行开发了,因为excel功能比较强大,一般的应用的报表用它来开发绰绰有余了。而且这样开发的成本也很低,不需要再购买其它专业的报表开发软件直接使用office开发即可。使用excel开发的宏模版当然要使用上层程序来调用宏模版了。我以前在工作中就开发过excel模版,并使用c#程序调用过这些模版程序。规则是首先将报表需要的数据填冲到datatable或者dataset中,然后将datatable或者dataset中数据导出excel中。
现提供导出excel模版的代码如下:

复制代码 代码如下:

namespace exceltest
{
    public class exeltemplate
    {
        private static excel.application 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
       //将报表所需的主数据放入dtheader,明细数据放入dtdetail中,调用的模版名称为strtemplatefilename,导出的报表名称为stroutfilename
        public static bool fillcontent(string strtemplatefilename, string stroutfilename, system.data.datatable dtheader, system.data.datatable dtdetail)
        {
            bool flag = true;
            fileinfo fileinfo = new fileinfo(strtemplatefilename);
            system.globalization.cultureinfo currentci = system.threading.thread.currentthread.currentculture;
            system.threading.thread.currentthread.currentculture = new system.globalization.cultureinfo("en-us");
            try
            {
                //start excel and get application object.               
                excelapp = new excel.application();
                excelapp.visible  = false;

                //get a new workbook.
                excelwb = (excel._workbook)(excelapp.workbooks.add(strtemplatefilename));

                //fill content.注意这里的header和detail分别对应模版文件中的header和detail两个sheet页,这2个sheet是专门用来存放明主数据或者明细数据的。
                if (!fillworksheet("header", dtheader)) return false;
                if (!fillworksheet("detail", dtdetail)) return false;
                //run macro.

                excelapp.run("setdata", missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value);

                excelapp.displayalerts = false;
                //=========================================================================================================
                //delete header and detail.
                //((excel._worksheet)excelwb.sheets["header information"]).delete();              
                //((excel._worksheet)excelwb.sheets["detail information"]).delete();
 

                //=========================================================================================================
                excelapp.displayalerts = true;
                //delete old file.
                file.delete(stroutfilename);
                //save excel file.
                excelwb.saveas(stroutfilename, missing.value, missing.value, missing.value, missing.value, missing.value, xlsaveasaccessmode.xlexclusive, missing.value, missing.value, missing.value, missing.value, missing.value);

                // quit excel.
                excelwb.close(false, stroutfilename, missing.value);//*******************            
                marshalreleasecomobject(excelwb);
                excelapp.quit();

               
                //kill excel application.
                //killprocess("excel");//******************

            }
            catch (exception ex)
            {
                throw ex;  
                flag = false;
            }
            finally
            {
                marshalreleasecomobject(excelapp);
                gc.collect();
            }
            return flag;
        }
        //将报表所需的主数据放入dtheader,明细数据放入dtdetail中,调用的模版名称为strtemplatefilename,导出的报表名称为stroutfilename
        public static bool fillcontent(string strtemplatefilename, string stroutfilename, system.data.dataset dsdata)
        {
            bool flag = true;
            fileinfo fileinfo = new fileinfo(strtemplatefilename);
            system.globalization.cultureinfo currentci = system.threading.thread.currentthread.currentculture;
            system.threading.thread.currentthread.currentculture = new system.globalization.cultureinfo("en-us");
            try
            {
                //start excel and get application object.               
                excelapp = new excel.application();
                excelapp.visible = false;
                //get a new workbook.
                 excelwb = (excel._workbook)(excelapp.workbooks.add(strtemplatefilename));
                 //fill content.
                //if (!fillworksheet("header", dtheader)) return false;
                //if (!fillworksheet("detail", dtdetail)) return false;
                //fill content.注意这里的没有写明对应模版文件中sheet页,但是指定了dsdata.tables[i].tablename为sheet页的名字,这样方便灵活设置,而且这样可以有多个sheet是专门用来存放明主数据或者明细数据的。
                for (int i = 0; i < dsdata.tables.count; i++)
                {
                    if (!fillworksheet(dsdata.tables[i].tablename, dsdata.tables[i]))
                        return false;
                }
                //run macro.

                excelapp.run("setdata", missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value, missing.value);

                excelapp.displayalerts = false;
                //=========================================================================================================
                //delete header and detail.
                //((excel._worksheet)excelwb.sheets["header information"]).delete();              
                //((excel._worksheet)excelwb.sheets["detail information"]).delete();
 

                //=========================================================================================================
                excelapp.displayalerts = true;
                //delete old file.
                file.delete(stroutfilename);
                //save excel file.

                excelwb.saveas(stroutfilename, missing.value, missing.value, missing.value, missing.value, missing.value, xlsaveasaccessmode.xlexclusive, missing.value, missing.value, missing.value, missing.value, missing.value);

                // quit excel.
                excelwb.close(false, stroutfilename, missing.value);//*******************            
                marshalreleasecomobject(excelwb);
                excelapp.quit();

            }
            catch (exception ex)
            {
                throw ex;
                flag = false;
            }
            finally
            {
                marshalreleasecomobject(excelapp);
                gc.collect();
            }
            return flag;
        }
        //使用excel来导出报表时,在服务器上的excel进程容易死掉,导致资源无法释放,因此需要使用这个方法来释放死掉的进程
        private static void marshalreleasecomobject(object objcom)
        {
            try
            {
                int i = 1;
                if (objcom != null && system.runtime.interopservices.marshal.iscomobject(objcom))
                {
                    do
                    {
                        i = system.runtime.interopservices.marshal.releasecomobject(objcom);
                    } while (i > 0);
                }
            }
            finally
            {
                objcom = null;
            }
        }
}

上一篇:

下一篇: