动态导出Excel(列从xml中取)

    技术2022-05-18  22

            /// <summary>        /// 导出        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnOutExcel_Click(object sender, EventArgs e)        {            //ExportToExcel ete = new ExportToExcel();            //ete.DataGridViewToExcel(this.dgvList);

                if (RecordCount.Equals(0))            {                return;            }            int totalPage = 0;            int totalRecord = 0;            Lamp.WinControl.tcLoadFlash loadFlash = new tcLoadFlash();            loadFlash.Show();            Application.DoEvents();            IList<B_LampPost> postList = PB_LampPost.GetLampPostListByPage(Program.dbname, sqlWhere, this.tcPager1.PageSize, 1, out totalPage, out totalRecord);            List<B_LampPost> allList = new List<B_LampPost>();            allList.AddRange(postList);            for (int i = 2; i <= totalPage; i++)            {                postList = PB_LampPost.GetLampPostListByPage(Program.dbname, sqlWhere, this.tcPager1.PageSize, i, out totalPage, out totalRecord);                allList.AddRange(postList);            }            loadFlash.Dispose();

                if (postList.Count > 0)            {                DataTable dt = new DataTable();                #region 生成DataTable

                    XmlDocument doc = new XmlDocument();                string xmlFile = System.Environment.CurrentDirectory + "//GridViewXml//frmLampPost.xml";                doc.Load(xmlFile);                XmlNode node = doc.SelectNodes("//Fields")[0];

                    foreach (XmlNode n in node.ChildNodes)                {                    string dataField = n.Attributes["DataField"].InnerText;                    string name = n.InnerText;                    string strVisible = n.Attributes["Visible"].InnerText;                    if (strVisible == "1")                    {                        dt.Columns.Add(new DataColumn(name, typeof(string)));                    }

                    }

     

     

                    #endregion                //dt.Columns.Add(new DataColumn("序号", typeof(string)));                //dt.Columns.Add(new DataColumn("灯杆ID", typeof(string)));                //dt.Columns.Add(new DataColumn("灯杆编号", typeof(string)));                //dt.Columns.Add(new DataColumn("灯杆类型", typeof(string)));                //dt.Columns.Add(new DataColumn("所在道路", typeof(string)));                //dt.Columns.Add(new DataColumn("路侧", typeof(string)));                //dt.Columns.Add(new DataColumn("所属变压器", typeof(string)));                //dt.Columns.Add(new DataColumn("变压器编号", typeof(string)));                //dt.Columns.Add(new DataColumn("所属回路", typeof(string)));                //dt.Columns.Add(new DataColumn("电源类型", typeof(string)));                //dt.Columns.Add(new DataColumn("灯源个数", typeof(string)));                //dt.Columns.Add(new DataColumn("责任人", typeof(string)));                //dt.Columns.Add(new DataColumn("国标编号", typeof(string)));                //dt.Columns.Add(new DataColumn("高度", typeof(string)));                //dt.Columns.Add(new DataColumn("价格", typeof(string)));                //dt.Columns.Add(new DataColumn("安装日期", typeof(string)));                //dt.Columns.Add(new DataColumn("生产厂商", typeof(string)));                //dt.Columns.Add(new DataColumn("描述", typeof(string)));

                    DataRow row = null;                int rowNumber = allList.Count;                if (rowNumber > 0)                {                    System.Reflection.PropertyInfo[] propertyInfo = allList[0].GetType().GetProperties();                    for (int j = 0; j < rowNumber; j++)                    {                        row = dt.NewRow();

                            foreach (XmlNode n in node.ChildNodes)                        {                            string dataField = n.Attributes["DataField"].InnerText;                            string name = n.InnerText;                            string strVisible = n.Attributes["Visible"].InnerText;                            object objAttribute = allList[j].GetType().InvokeMember(dataField,                                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty,                                    null,                                    allList[j],                                    null,                                    null,                                    null,                                    null);                            string strAttribute = "";                            if (objAttribute!=null)                            {                                strAttribute = objAttribute.ToString();                            }                            if (strVisible == "1"&&strAttribute!="")                            {                                row[name] = strAttribute;                            }

                            }                        //row["序号"] = allList[j].LampPostNumber;                        //row["灯杆ID"] = allList[j].LampPostID;                        //row["灯杆编号"] = allList[j].LamppostCode;                        //row["灯杆类型"] = allList[j].LampPostTypeName;                        //row["所在道路"] = allList[j].StreetName;                        //row["路侧"] = allList[j].StreetSideTypeName;                        //row["所属变压器"] = allList[j].TransformerName;                        //row["变压器编号"] = allList[j].TransCode;                        //row["所属回路"] = allList[j].RelayID;                        //row["电源类型"] = allList[j].PowerType;                        //row["灯源个数"] = allList[j].LampCount;                        //row["责任人"] = allList[j].Persons;                        //row["国标编号"] = allList[j].GBCode;                        //row["高度"] = allList[j].Height;                        //row["价格"] = allList[j].Price;                        //row["安装日期"] = allList[j].AddDate;                        //row["生产厂商"] = allList[j].ManufacturerName;                        //row["描述"] = allList[j].LampPostIntro;

                            dt.Rows.Add(row);                    }

                    }                dt.AcceptChanges();                Dictionary<string, System.Data.DataTable> TableList = new Dictionary<string, System.Data.DataTable>();                TableList.Add("灯杆信息", dt);                Inc.ExportListToExcel(TableList);                //Inc.ExportToExcel(dt);               

                }        }


    最新回复(0)