• C#操作Excel全源码


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Drawing;
    using Excel = Microsoft.Office.Interop.Excel;


    namespace BusinessRule
    {
        public class ExcelTool
     {
            public delegate void GenerateExcelShopPlan(int postion, int totalCount);
            public event GenerateExcelShopPlan GenerateExcelShopPlanEvent;
      public ExcelTool()
      {
      
               
      }

            

            #region 生成到店计划
            public void GenerateShopPlan(int pActivityID, int pRegionID, string pFilePath)
            {
                this.SubGenerateShopPlan(pActivityID, pRegionID, pFilePath);
                //启动垃圾回收释放excel资源
                GC.Collect();
            }

            private void SubGenerateShopPlan(int pActivityID, int pRegionID, string pFilePath)
            {
                #region Excel object references.
                Excel.Application m_objExcel = null;
                Excel.Workbooks m_objBooks = null;
                Excel._Workbook m_objBook = null;
                Excel.Sheets m_objSheets = null;
                Excel._Worksheet m_objSheet = null;
                Excel.Range m_objRange = null;
                Excel.Font m_objFont = null;
                Excel.Borders m_objBorders = null;
                #endregion

                try
                {
                   
                    #region declare ver
                    DataAccess.Shops iShops = new DataAccess.Shops();
                    DataAccess.AllocationPlan iAllocationPlan = new DataAccess.AllocationPlan();
                    DataAccess.Activitys iActivitys = new DataAccess.Activitys();
                    DataAccess.Geography iGeography = new DataAccess.Geography();

                    DataTable dtTemp = new DataTable();
                    DataTable dtMaterial;
                    DataTable dtShopLevelMaterial;
                    DataTable dtORGNameList;
                    DataTable dtADManagerList;
                    DataTable dtManagerList;
                    DataTable dtShopList;

                    DataView dvTemp;

                    string sORGName;
                    string sADManager;
                    string sManager;
                    string sShopName;
                    string sShopLevel;
                    string sShopCode;

                    int intMaterialCount = 0;

                    object[] objBody;


                    string sTemp = "";
                    int AIndex = 0;
                    int[] ARegionQty;
                    int[] AORGNameQty;
                    int[] AADManagerQty;
                    int[] AManagerQty;
                    int[] AMaterialQty;
                    int bufferPerQty;
                    int bufferQty;
                    string sMaterialID;
                    int intAllocationQty = 0;
                    //物料列前的列数
                    int tableTitleCount;
                    #endregion

                    #region 生成excel对象
                    // Frequenty-used variable for optional arguments.
                    object m_objOpt = System.Reflection.Missing.Value;

                    // Paths used by the sample code for accessing and storing data.
                    //object m_strSampleFolder = argPath;
                    // Start a new workbook in Excel.
                    m_objExcel = new Excel.Application();
                    //    m_objExcel.Visible=true;
                    m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
                    m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));

                    m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
                    m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
                    #endregion

                    #region 设置头
                    //区域名称设为每个sheet name

                    string sRegionName = iGeography.GetRegionName(pRegionID);
                    m_objSheet.Name = sRegionName;
                    //取得活动名称
                    Model.ActivityInfo.T_ActivityRow iARW = iActivitys.GetActivityInfo(pActivityID);
                    string sActivity = iARW.ActivityName;
                    object[] objActivity = { sActivity };
                    m_objRange = m_objSheet.get_Range("A1", m_objOpt);
                    m_objRange.set_Value(m_objOpt, objActivity);
                    //区域名称
                    object[] objRegionName = { sRegionName };
                    m_objRange = m_objSheet.get_Range("A2", m_objOpt);
                    m_objRange.set_Value(m_objOpt, objRegionName);
                    //表头1
                    object[] objTital = { "销售部门", "高级经理", "经理","零售店编号","零售店", "零售店级别" };
                    m_objRange = m_objSheet.get_Range("A3", "F3");
                    m_objRange.set_Value(m_objOpt, objTital);
                    m_objFont = m_objRange.Font;
                    m_objFont.Bold = true;
                    tableTitleCount = 6;
                    //表头2,物料列表                            
                    dtMaterial = iAllocationPlan.GetMaterialList(pActivityID, pRegionID);
                    objTital = new object[dtMaterial.Rows.Count];
                    for (int i = 0; i < dtMaterial.Rows.Count; i++)
                    {
                        objTital[i] = CommonLibrary.StringFunc.CheckDBStringNull(dtMaterial.Rows[i]["Material"]);
                    }
                    m_objRange = m_objSheet.get_Range("G3", "CE3");
                    m_objRange = m_objRange.get_Resize(1, dtMaterial.Rows.Count);
                    m_objRange.set_Value(m_objOpt, objTital);
                    m_objFont = m_objRange.Font;
                    m_objFont.Bold = true;

                    #region 设置表头的黑框
                    m_objRange = m_objSheet.get_Range("A3", m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, tableTitleCount + dtMaterial.Rows.Count);
                    m_objBorders = m_objRange.Borders;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlMedium;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].Color = ColorTranslator.ToOle(Color.Black);

                    m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlMedium;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].Color = ColorTranslator.ToOle(Color.Black);

                    m_objBorders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeRight].Color = ColorTranslator.ToOle(Color.Black);

                    m_objBorders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlMedium;
                    m_objBorders[Excel.XlBordersIndex.xlEdgeTop].Color = ColorTranslator.ToOle(Color.Black);
                    //m_objBorders.Weight = Excel.XlBorderWeight.xlMedium;
                    //m_objBorders.Color = ColorTranslator.ToOle(Color.Black);
                    #endregion

                    #endregion

                    //物料分配列表
                    dtShopLevelMaterial = iAllocationPlan.GetAllocationMaterial(pActivityID, pRegionID);
                    //物料总列数
                    intMaterialCount = dtMaterial.Rows.Count;

                    //区域的物料总数
                    ARegionQty = new int[dtMaterial.Rows.Count];
                    //部门的物料总数
                    AORGNameQty = new int[dtMaterial.Rows.Count];
                    //高级经理的物料总数
                    AADManagerQty = new int[dtMaterial.Rows.Count];
                    //经理的物料总数
                    AManagerQty = new int[dtMaterial.Rows.Count];
                    //店面的物料总数
                    AMaterialQty = new int[dtMaterial.Rows.Count];

                    //excel当前行数
                    AIndex = 4;
                    int intORGIndex = 0;

                    #region 设置位置用于进度条
                    int postion = 0;
                    int totalCount = 0;
                    //计算出有多少个高级经理,以高级经理作为进度
                    dtORGNameList = iShops.GetPlanShopORGNameList(pRegionID);
                    for (int i = 0; i < dtORGNameList.Rows.Count; i++)
                    {
                        sORGName = dtORGNameList.Rows[i]["ORGName"].ToString();
                        dtADManagerList = iShops.GetPlanShopADManagerList(pRegionID, sORGName);
                        for (int j = 0; j < dtADManagerList.Rows.Count; j++)
                        {
                            totalCount++;
                        }
                    }
                    #endregion

                    #region 销售部门列表
                    dtORGNameList = iShops.GetPlanShopORGNameList(pRegionID);
                    for (int i = 0; i < dtORGNameList.Rows.Count; i++)
                    {
                        sORGName = dtORGNameList.Rows[i]["ORGName"].ToString();
                        intORGIndex = AIndex;
                        m_objRange = m_objSheet.get_Range("A" + AIndex.ToString(), m_objOpt);
                        m_objRange = m_objRange.get_Resize(1, 1);
                        m_objRange.set_Value(m_objOpt, sORGName);

                        #region 高级经理列表
                        dtADManagerList = iShops.GetPlanShopADManagerList(pRegionID, sORGName);
                        for (int j = 0; j < dtADManagerList.Rows.Count; j++)
                        {
                            #region 抛出事件显示进度
                            postion++;
                            this.GenerateExcelShopPlanEvent(postion, totalCount);
                            #endregion

                            sADManager = dtADManagerList.Rows[j]["EmployeeCode"].ToString();
                            sTemp = dtADManagerList.Rows[j]["EmployeeName"].ToString();
                            m_objRange = m_objSheet.get_Range("B" + AIndex.ToString(), m_objOpt);
                            m_objRange = m_objRange.get_Resize(1, 1);
                            m_objRange.set_Value(m_objOpt, sTemp);

                            #region 经理列表
                            dtManagerList = iShops.GetPlanShopManagerList(pRegionID, sORGName, sADManager);
                            for (int k = 0; k < dtManagerList.Rows.Count; k++)
                            {
                                sManager = dtManagerList.Rows[k]["EmployeeCode"].ToString();
                                sTemp = dtManagerList.Rows[k]["EmployeeName"].ToString();
                                m_objRange = m_objSheet.get_Range("C" + AIndex.ToString(), m_objOpt);
                                m_objRange = m_objRange.get_Resize(1, 1);
                                m_objRange.set_Value(m_objOpt, sTemp);

                                #region 零售店列表
                                dtShopList = iShops.GetPlanShopList(pRegionID, sORGName, sADManager, sManager);
                                for (int l = 0; l < dtShopList.Rows.Count; l++)
                                {
                                    sShopCode = dtShopList.Rows[l]["ShopCode"].ToString();
                                    sShopName = dtShopList.Rows[l]["ShopName"].ToString();
                                    sShopLevel = dtShopList.Rows[l]["ShopLevel"].ToString();

                                    objBody = new object[3 + intMaterialCount];
                                    objBody[0] = sShopCode;
                                    objBody[1] = sShopName;
                                    objBody[2] = sShopLevel;

                                    #region 添加物料分配数量
                                    dvTemp = dtShopLevelMaterial.DefaultView;
                                    for (int m = 0; m < dtMaterial.Rows.Count; m++)
                                    {
                                        sMaterialID = dtMaterial.Rows[m]["MaterialID"].ToString();
                                        dvTemp.RowFilter = "MaterialID = '" + sMaterialID + "' and ShopLevel = '" + sShopLevel + "'";
                                        if (dvTemp.Count > 0)
                                        {
                                            intAllocationQty = CommonLibrary.StringFunc.CheckDBIntNull(dvTemp[0].Row["AllocationQty"]);
                                        }
                                        else
                                        {
                                            intAllocationQty = 0;
                                        }
                                        objBody[3 + m] = intAllocationQty;                                  
                                        AMaterialQty[m] = intAllocationQty;
                                        //增加区域的物料数量
                                        ARegionQty[m] += intAllocationQty;
                                        //增加部门物料分配数量
                                        AORGNameQty[m] += intAllocationQty;
                                        //增加高级经理分配数量
                                        AADManagerQty[m] += intAllocationQty;
                                        //增加经理分配数量
                                        AManagerQty[m] += intAllocationQty;
                                    }
                                    #endregion

                                    m_objRange = m_objSheet.get_Range("D" + AIndex.ToString(), m_objOpt);
                                    m_objRange = m_objRange.get_Resize(1, 3 + intMaterialCount);
                                    m_objRange.set_Value(m_objOpt, objBody);

                                    AIndex++;
                                }
                                #endregion

                                #region 添加单个经理的各物料数总计,写入excel
                                objBody = new object[4 + intMaterialCount];
                                objBody[0] = "总数";
                                for (int l = 0; l < AManagerQty.Length; l++)
                                {
                                    objBody[4 + l] = AManagerQty[l];
                                }
                                m_objRange = m_objSheet.get_Range("C" + AIndex.ToString(), m_objOpt);
                                m_objRange = m_objRange.get_Resize(1, 4 + intMaterialCount);
                                m_objRange.set_Value(m_objOpt, objBody);
                                m_objFont = m_objRange.Font;
                                m_objFont.Bold = true;
                                //打印完总数后,将数组清零
                                for (int l = 0; l < AManagerQty.Length; l++)
                                {
                                    AManagerQty[l] = 0;
                                }
                                #endregion

                                AIndex++;
                            }
                            #endregion

                            #region 添加单个高级经理的各物料数总计,写入excel
                            objBody = new object[5 + intMaterialCount];
                            objBody[0] = "总数";
                            for (int k = 0; k < AADManagerQty.Length; k++)
                            {
                                objBody[5 + k] = AADManagerQty[k];
                            }
                            m_objRange = m_objSheet.get_Range("B" + AIndex.ToString(), m_objOpt);
                            m_objRange = m_objRange.get_Resize(1, 5 + intMaterialCount);
                            m_objRange.set_Value(m_objOpt, objBody);
                            m_objFont = m_objRange.Font;
                            m_objFont.Bold = true;
                            //打印完总数后,将数组清零
                            for (int k = 0; k < AManagerQty.Length; k++)
                            {
                                AADManagerQty[k] = 0;
                            }
                            #endregion

                            AIndex++;

                        }
                        #endregion

                        #region 添加单个销售部门的各物料数总计,写入excel
                        objBody = new object[6 + intMaterialCount];
                        objBody[0] = "总数";
                        for (int j = 0; j < AORGNameQty.Length; j++)
                        {
                            objBody[6 + j] = AORGNameQty[j];
                        }
                        m_objRange = m_objSheet.get_Range("A" + AIndex.ToString(), m_objOpt);
                        m_objRange = m_objRange.get_Resize(1, 6 + intMaterialCount);
                        m_objRange.set_Value(m_objOpt, objBody);
                        m_objFont = m_objRange.Font;
                        m_objFont.Bold = true;
                        //打印完总数后,将数组清零
                        for (int j = 0; j < AORGNameQty.Length; j++)
                        {
                            AORGNameQty[j] = 0;
                        }
                        #endregion

                        #region 设置每个销售部门的黑框
                        m_objRange = m_objSheet.get_Range("A" + intORGIndex.ToString(), m_objOpt);
                        m_objRange = m_objRange.get_Resize(AIndex - intORGIndex + 1, 6 + dtMaterial.Rows.Count);
                        m_objBorders = m_objRange.Borders;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlMedium;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].Color = ColorTranslator.ToOle(Color.Black);

                        m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlMedium;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].Color = ColorTranslator.ToOle(Color.Black);

                        m_objBorders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeRight].Color = ColorTranslator.ToOle(Color.Black);

                        m_objBorders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlMedium;
                        m_objBorders[Excel.XlBordersIndex.xlEdgeTop].Color = ColorTranslator.ToOle(Color.Black);
                        #endregion

                        AIndex++;
                    }
                    #endregion

                    #region 保存本区域总合计及预留数
                    AIndex++;
                    AIndex++;

                    #region 保存总数量
                    objBody = new object[6 + intMaterialCount];
                    objBody[0] = "总数量";
                    for (int j = 0; j < ARegionQty.Length; j++)
                    {
                        objBody[6 + j] = ARegionQty[j];
                    }
                    m_objRange = m_objSheet.get_Range("A" + AIndex.ToString(), m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, 6 + intMaterialCount);
                    m_objRange.set_Value(m_objOpt, objBody);
                    m_objFont = m_objRange.Font;
                    m_objFont.Bold = true;
                    #endregion

                    #region 保存预留数
                    AIndex++;
                    DataAccess.AllocationPlanRegion iAllocationPlanRegion =
                        new DataAccess.AllocationPlanRegion();
                    Model.AllocationPlanRegionInfo iAllocationPlanRegionInfo =
                        new Model.AllocationPlanRegionInfo();
                    objBody = new object[6 + intMaterialCount];
                    objBody[0] = "预留数量";
                    iAllocationPlanRegionInfo = iAllocationPlanRegion.GetBufferByActivityRegion(pActivityID, pRegionID);
                    for (int i = 0; i < dtMaterial.Rows.Count; i++)
                    {
                        sMaterialID = dtMaterial.Rows[i]["MaterialID"].ToString();
                        iAllocationPlanRegionInfo.T_AllocationPlanRegion.DefaultView.RowFilter = "MaterialID = " + sMaterialID + "";
                        if (iAllocationPlanRegionInfo.T_AllocationPlanRegion.DefaultView.Count > 0)
                        {
                            bufferPerQty = CommonLibrary.StringFunc.CheckDBIntNull
                                (iAllocationPlanRegionInfo.T_AllocationPlanRegion.DefaultView[0]["BufferQty"]);
                        }
                        else
                        {
                            bufferPerQty = 0;
                        }
                        bufferQty = CommonLibrary.StringFunc.CheckDBIntNull(ARegionQty[i]) * bufferPerQty / 100;
                        objBody[6 + i] = bufferQty;
                    }
                    m_objRange = m_objSheet.get_Range("A" + AIndex.ToString(), m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, 6 + intMaterialCount);
                    m_objRange.set_Value(m_objOpt, objBody);
                    m_objFont = m_objRange.Font;
                    m_objFont.Bold = true;
                    #endregion

                    #endregion

                    #region 保存文件
                    m_objBook.SaveAs(pFilePath, m_objOpt, m_objOpt,
                     m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
                     m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
                    m_objBook.Close(true, m_objOpt, m_objOpt);
                    m_objBooks.Close();
                    m_objExcel.Quit();
                    #endregion
                                   
                }
                catch
                {
                    throw;
                }
                finally
                {
                    #region 释放Excel资源
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheets);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objRange);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objFont);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBorders);


                    m_objExcel = null;
                    m_objBooks = null;
                    m_objBook = null;
                    m_objSheets = null;
                    m_objSheet = null;
                    m_objRange = null;
                    m_objFont = null;
                    m_objBorders = null;

                   
                    #endregion
                }
              
            }  
            #endregion

            #region 生成建议方案
            public void GeneratePlan(int pActivityID, int pRegionID, string pFilePath)
            {
                this.SubGeneratePlan(pActivityID, pRegionID, pFilePath);
                //启动垃圾回收释放excel资源
                GC.Collect();
            }
            private void SubGeneratePlan(int pActivityID, int pRegionID, string pFilePath)
            {
                #region Excel object references.
                Excel.Application m_objExcel = null;
                Excel.Workbooks m_objBooks = null;
                Excel._Workbook m_objBook = null;
                Excel.Sheets m_objSheets = null;
                Excel._Worksheet m_objSheet = null;
                Excel.Range m_objRange = null;
                Excel.Font m_objFont = null;
                Excel.Borders m_objBorders = null;
                #endregion

                try
                {

                    #region declare ver
                    DataAccess.Shops iShops;
                    DataAccess.AllocationPlan iAllocationPlan;
                    DataAccess.Activitys iActivitys;
                    DataAccess.Geography iGeography;
                    DataAccess.Materials iMaterials;
                    DataAccess.SysParameters iSysParameters;
                    DataAccess.AllocationPlanRegion iAllocationPlanRegion;
                    DataAccess.Dictionary idic;

                    DataAccess.AllocationMaterial iAllocationMaterial;

                    Model.AllocationMaterialInfo iAllocationMaterialInfo;
                    Model.AllocationMaterialInfo.T_AllocationMaterialRow iAMRow;
                    Model.AllocationPlanRegionInfo iAllocationPlanRegionInfo;
                    Model.ActivityInfo.T_ActivityRow iARW;

                    DataTable dtTemp = new DataTable();
                    DataTable dtMaterialList;
                    DataTable dtShopLevelMaterial;
                    DataTable dtShopList;
                    DataTable dtShopLevel;

                    DataView dvShopLevelMaterial;
                    DataView dvShopList;

                    decimal decWeightPrice;

                    string sShopLevel;
                    string sMaterialName;
                   
                    int shopCount;
                    int bufferPerQty;
                    int allocationQty;

                    object[] objAllocationQty;
                    object[] objShopCount;
                   
                    string sMaterialID;               
                    string sRegionName;
                    string sActivity;
                    #endregion

                    #region 生成excel对象
                    // Frequenty-used variable for optional arguments.
                    object m_objOpt = System.Reflection.Missing.Value;

                    // Paths used by the sample code for accessing and storing data.
                    //object m_strSampleFolder = argPath;
                    // Start a new workbook in Excel.
                    m_objExcel = new Excel.Application();
                    //    m_objExcel.Visible=true;
                    m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
                    m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));

                    m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
                    m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
                    #endregion

                    int postion = 0;
                    int totalCount = 0;
                    //物料分配列表
                    iAllocationPlan = new DataAccess.AllocationPlan();
                    iMaterials = new DataAccess.Materials();
                    idic = new DataAccess.Dictionary();
                    dtShopLevel = idic.GetShopLevelList();
                    dtMaterialList = iAllocationPlan.GetMaterialList(pActivityID, pRegionID);
                    totalCount = dtMaterialList.Rows.Count * dtShopLevel.Rows.Count + 2;

                    #region 设置头
                    //区域名称设为每个sheet name
                    iGeography = new DataAccess.Geography();
                    sRegionName = iGeography.GetRegionName(pRegionID);
                    m_objSheet.Name = sRegionName;
                    //取得活动名称
                    iActivitys = new DataAccess.Activitys();
                    iARW = iActivitys.GetActivityInfo(pActivityID);
                    sActivity = iARW.ActivityName;
                    object[] objActivity = { sActivity };
                    m_objRange = m_objSheet.get_Range("A1", m_objOpt);
                    m_objRange.set_Value(m_objOpt, objActivity);
                    m_objFont = m_objRange.Font;
                    m_objFont.Size = "18";
                    m_objFont.Bold = true;
                    //区域名称
                    object[] objRegionName = { sRegionName };
                    m_objRange = m_objSheet.get_Range("A2", m_objOpt);
                    m_objRange.set_Value(m_objOpt, objRegionName);


                    #region 设置第一行列头
                    //取得零售店级别列表设置列头

                    m_objRange = m_objSheet.get_Range("B3", m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3);
                    for (int i = 0; i < dtShopLevel.Rows.Count; i++)
                    {
                        m_objRange = m_objSheet.get_Range("B3", m_objOpt);
                        m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3);
                        m_objRange = m_objRange.get_Offset(0, i * 3);
                        m_objRange = m_objRange.get_Resize(1, 3);
                        m_objRange.Merge(m_objOpt);
                        m_objRange.HorizontalAlignment = Excel.Constants.xlCenter;
                        m_objRange.set_Value(m_objOpt, dtShopLevel.Rows[i]["ShopLevel"].ToString());
                    }
                    #region 抛出事件显示进度
                    postion++;
                    this.GenerateExcelShopPlanEvent(postion, totalCount);
                    #endregion
                    //设置,预留数(%),总数量,总制作费,总运输费
                    m_objRange = m_objSheet.get_Range("B3", m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3 + 4);
                    m_objRange = m_objRange.get_Offset(0, dtShopLevel.Rows.Count * 3);
                    m_objRange = m_objRange.get_Resize(1, 4);
                    object[] objFirstTitle = { "预留数(%)","总数量","总制作费","总运输费" };
                    m_objRange.set_Value(m_objOpt, objFirstTitle);
                    //设置表头的黑框
                    m_objRange = m_objSheet.get_Range("B3", m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3 + 4);
                    m_objBorders = m_objRange.Borders;
                    m_objBorders.Weight = Excel.XlBorderWeight.xlMedium;
                    m_objBorders.Color = ColorTranslator.ToOle(Color.Black);
                    #endregion

                    #region 合并第一列
                    m_objRange = m_objSheet.get_Range("A3", "A4");
                    m_objRange.Merge(m_objOpt);
                    //设置黑框
                    m_objBorders = m_objRange.Borders;
                    m_objBorders.Weight = Excel.XlBorderWeight.xlMedium;
                    m_objBorders.Color = ColorTranslator.ToOle(Color.Black);
                    #endregion

                    #region 设置第二行列头
                    m_objRange = m_objSheet.get_Range("B4", m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3 + 4);
                    object[] objSecondTitle = new object[dtShopLevel.Rows.Count * 3 + 4];
                    for (int i = 0; i < dtShopLevel.Rows.Count; i++)
                    {
                        objSecondTitle[i * 3] = "店数";
                        objSecondTitle[i * 3 + 1] = "分配原则";
                        objSecondTitle[i * 3 + 2] = "分配数量";

                        #region 设置黑框
                        m_objRange = m_objSheet.get_Range("B4", m_objOpt);
                        m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3 + 4);
                        m_objRange = m_objRange.get_Offset(0, i * 3);
                        m_objRange = m_objRange.get_Resize(1, 3);
                        m_objBorders = m_objRange.Borders;
                        this.SetBorders(m_objBorders);
                        #endregion
                    }
                    m_objRange = m_objSheet.get_Range("B4", m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3 + 4);
                    m_objRange.set_Value(m_objOpt, objSecondTitle);
                    //设置后四个title黑框
                    m_objRange = m_objSheet.get_Range("B4", m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3 + 4);
                    m_objRange = m_objRange.get_Offset(0, dtShopLevel.Rows.Count * 3);
                    m_objRange = m_objRange.get_Resize(1, 4);
                    m_objBorders = m_objRange.Borders;
                    m_objBorders.Weight = Excel.XlBorderWeight.xlMedium;
                    m_objBorders.Color = ColorTranslator.ToOle(Color.Black);
                    #endregion
                   
                    #endregion

                    #region 抛出事件显示进度
                    postion++;
                    this.GenerateExcelShopPlanEvent(postion, totalCount);
                    #endregion

                    #region 设置零售店数

                    objShopCount = new object[dtShopLevel.Rows.Count * 3];
                    iShops = new DataAccess.Shops();
                    //获得每种零售店类型的数量列表
                    dtShopList = iShops.GetShopCountByRegionLevel(pRegionID);
                    dvShopList = dtShopList.DefaultView;
                    for (int i = 0; i < dtShopLevel.Rows.Count; i++)
                    {
                        sShopLevel = dtShopLevel.Rows[i]["ShopLevel"].ToString();
                        dvShopList.RowFilter = "ShopLevel = '" + sShopLevel + "'";
                        if (dvShopList.Count > 0)
                        {
                            shopCount = StringFunc.CheckDBIntNull(dvShopList[0]["ShopLevelCount"]);
                        }
                        else
                        {
                            shopCount = 0;
                        }
                        objShopCount[i * 3] = shopCount;
                    }
                    m_objRange = m_objSheet.get_Range("B5", m_objOpt);
                    m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3);
                    m_objRange.set_Value(m_objOpt, objShopCount);
                    #endregion

                    #region 设置分配计划数量和实际分配数量
                    dtShopLevelMaterial = iAllocationPlan.GetAllocationMaterial(pActivityID, pRegionID);
                    dvShopLevelMaterial = dtShopLevelMaterial.DefaultView;

                    objAllocationQty = new object[dtShopLevel.Rows.Count * 3 + 1 + 4];
                    //取得单位运输费
                    iSysParameters = new DataAccess.SysParameters();
                    decWeightPrice = Convert.ToDecimal(iSysParameters.GetWeightPrice());

                    //取得预留百分数
                    iAllocationPlanRegion = new DataAccess.AllocationPlanRegion();
                    iAllocationPlanRegionInfo = new Model.AllocationPlanRegionInfo();
                    iAllocationPlanRegionInfo = iAllocationPlanRegion.GetBufferByActivityRegion(pActivityID, pRegionID);

                    //取出物料

                    int AIndex = 0;
                    int materialAllocationTotalQty = 0;
                    string curIndex = "";
                    //

                    for (int i = 0; i < dtMaterialList.Rows.Count; i++)
                    {
                        sMaterialID = dtMaterialList.Rows[i]["MaterialID"].ToString();
                        sMaterialName = dtMaterialList.Rows[i]["Material"].ToString();
                        objAllocationQty[0] = sMaterialName;
                        materialAllocationTotalQty = 0;

                        #region 添加各级别零售店的分配数
                        for (int j = 0; j < dtShopLevel.Rows.Count; j++)
                        {
                            #region 抛出事件显示进度
                            postion++;
                            this.GenerateExcelShopPlanEvent(postion, totalCount);
                            #endregion

                            //取得当前零售店级别的零售店数
                            sShopLevel = dtShopLevel.Rows[j]["ShopLevel"].ToString();
                            dvShopList.RowFilter = "ShopLevel = '" + sShopLevel + "'";
                            if (dvShopList.Count > 0)
                            {
                                shopCount = StringFunc.CheckDBIntNull(dvShopList[0]["ShopLevelCount"]);
                            }
                            else
                            {
                                shopCount = 0;
                            }
                            //取得当前零售店级别分配计划数
                            dvShopLevelMaterial.RowFilter = "MaterialID = " + sMaterialID + " and ShopLevel = '" + sShopLevel + "'";
                            if (dvShopLevelMaterial.Count > 0)
                            {
                                allocationQty = StringFunc.CheckDBIntNull(dvShopLevelMaterial[0]["AllocationQty"]);
                            }
                            else
                            {
                                allocationQty = 0;
                            }
                            //保存分配数量
                            objAllocationQty[j * 3 + 2] = allocationQty;
                            objAllocationQty[j * 3 + 2 + 1] = allocationQty * shopCount;
                            //总数量
                            materialAllocationTotalQty += allocationQty * shopCount;
                        }
                        #endregion

                        #region 保存预留百分数
                        iAllocationPlanRegionInfo.T_AllocationPlanRegion.DefaultView.RowFilter = "MaterialID = " + sMaterialID + "";
                        if (iAllocationPlanRegionInfo.T_AllocationPlanRegion.DefaultView.Count > 0)
                        {
                            bufferPerQty = StringFunc.CheckDBIntNull
                                (iAllocationPlanRegionInfo.T_AllocationPlanRegion.DefaultView[0]["BufferQty"]);
                        }
                        else
                        {
                            bufferPerQty = 0;
                        }
                        objAllocationQty[dtShopLevel.Rows.Count * 3 + 1 + 0] = bufferPerQty;
                        #endregion


                        #region 保存总数,总制作费,总运输费
                        //保存总数
                        objAllocationQty[dtShopLevel.Rows.Count * 3 + 1 + 1] = materialAllocationTotalQty;
                        //取得运输费和制作费
                        iAllocationMaterial = new DataAccess.AllocationMaterial();
                        iAllocationMaterialInfo = iAllocationMaterial.GetPriceWeightByActivityMaterial(pActivityID);
                        iAllocationMaterialInfo.T_AllocationMaterial.DefaultView.RowFilter = "MaterialID = " + sMaterialID + "";
                        iAMRow = (Model.AllocationMaterialInfo.T_AllocationMaterialRow)
                            iAllocationMaterialInfo.T_AllocationMaterial.DefaultView[0].Row;

                        decimal materialPrice = materialAllocationTotalQty * iAMRow.UnitPrice;
                        decimal materialTranportPrice = materialAllocationTotalQty * iAMRow.UnitWeight * decWeightPrice;
                        //总制作费
                        objAllocationQty[dtShopLevel.Rows.Count * 3 + 1 + 2] = materialPrice;
                        //总运输费
                        objAllocationQty[dtShopLevel.Rows.Count * 3 + 1 + 3] = materialTranportPrice;
                        #endregion

                        //写入excel
                        curIndex = Convert.ToString(6+AIndex);
                        m_objRange = m_objSheet.get_Range("A" + curIndex, m_objOpt);
                        m_objRange = m_objRange.get_Resize(1, dtShopLevel.Rows.Count * 3 + 5);
                        m_objRange.set_Value(m_objOpt, objAllocationQty);

                        //
                        AIndex++;

                    }
                    #endregion

                    #region 设置边框
                    //设置第一列的黑框
                    m_objRange = m_objSheet.get_Range("A5", m_objOpt);
                    m_objRange = m_objRange.get_Resize(dtMaterialList.Rows.Count + 1, 1);
                    m_objBorders = m_objRange.Borders;
                    this.SetBorders(m_objBorders);
                    //设置零售店类型黑框
                    for (int i = 0; i < dtShopLevel.Rows.Count; i++)
                    {
                        #region 设置黑框
                        m_objRange = m_objSheet.get_Range("B5", m_objOpt);
                        m_objRange = m_objRange.get_Resize(dtMaterialList.Rows.Count + 1, dtShopLevel.Rows.Count * 3);
                        m_objRange = m_objRange.get_Offset(0, i * 3);
                        m_objRange = m_objRange.get_Resize(dtMaterialList.Rows.Count + 1, 3);
                        m_objBorders = m_objRange.Borders;
                        this.SetBorders(m_objBorders);
                        #endregion
                    }
                    //设置后四个title黑框
                    m_objRange = m_objSheet.get_Range("B5", m_objOpt);
                    m_objRange = m_objRange.get_Resize(dtMaterialList.Rows.Count + 1, dtShopLevel.Rows.Count * 3 + 4);
                    m_objRange = m_objRange.get_Offset(0, dtShopLevel.Rows.Count * 3);
                    m_objRange = m_objRange.get_Resize(dtMaterialList.Rows.Count + 1, 1);
                    m_objBorders = m_objRange.Borders;
                    this.SetBorders(m_objBorders);
                    m_objRange = m_objRange.get_Resize(dtMaterialList.Rows.Count + 1, 2);
                    m_objBorders = m_objRange.Borders;
                    this.SetBorders(m_objBorders);
                    m_objRange = m_objRange.get_Resize(dtMaterialList.Rows.Count + 1, 3);
                    m_objBorders = m_objRange.Borders;
                    this.SetBorders(m_objBorders);
                    m_objRange = m_objRange.get_Resize(dtMaterialList.Rows.Count + 1, 4);
                    m_objBorders = m_objRange.Borders;
                    this.SetBorders(m_objBorders);
                    #endregion
                   
                    #region 保存文件
                    m_objBook.SaveAs(pFilePath, m_objOpt, m_objOpt,
                     m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
                     m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
                    m_objBook.Close(true, m_objOpt, m_objOpt);
                    m_objBooks.Close();
                    m_objExcel.Quit();
                    #endregion

                }
                catch
                {
                    throw;
                }
                finally
                {
                    #region 释放Excel资源
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheets);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objRange);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objFont);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBorders);


                    m_objExcel = null;
                    m_objBooks = null;
                    m_objBook = null;
                    m_objSheets = null;
                    m_objSheet = null;
                    m_objRange = null;
                    m_objFont = null;
                    m_objBorders = null;


                    #endregion
                }

            }  
            #endregion

            #region 设置四个边为黑框
            private void SetBorders(Excel.Borders m_objBorders)
            {
                m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
                m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlMedium;
                m_objBorders[Excel.XlBordersIndex.xlEdgeBottom].Color = ColorTranslator.ToOle(Color.Black);

                m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
                m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlMedium;
                m_objBorders[Excel.XlBordersIndex.xlEdgeLeft].Color = ColorTranslator.ToOle(Color.Black);

                m_objBorders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                m_objBorders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                m_objBorders[Excel.XlBordersIndex.xlEdgeRight].Color = ColorTranslator.ToOle(Color.Black);

                m_objBorders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
                m_objBorders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlMedium;
                m_objBorders[Excel.XlBordersIndex.xlEdgeTop].Color = ColorTranslator.ToOle(Color.Black);
            }
            #endregion             
         
        }
    }


     

    记住该记住的,忘记该忘记的,改变能改变的,接受不能改变的!
  • 相关阅读:
    三分法
    牛客网 TaoTao要吃鸡 ( 0/1背包变形 )
    POJ 3275 Ranking the cows ( Floyd求解传递闭包 && Bitset优化 )
    Codeforces 912E Prime Gift ( 二分 && 折半枚举 && 双指针技巧)
    Codeforces 912D Fishs ( 贪心 && 概率期望 && 优先队列 )
    Codeforces 919D Substring ( 拓扑排序 && DAG上的DP )
    队列构造拓扑排序
    Codeforces 919E Congruence Equation ( 数论 && 费马小定理 )
    Codeforces 916B Jamie and Binary Sequence ( 模拟 && 思维 )
    POJ 1769 Minimizing maximizer ( 线段树 && DP )
  • 原文地址:https://www.cnblogs.com/yuanermen/p/662179.html
Copyright © 2020-2023  润新知