C#执行Excel宏模版的方法(2)


                //=========================================================================================================
                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;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wydpyy.html