导出Excel时不导出隐藏的列方法,以及重新设置表头名称

时间:2021-10-26 09:20:14

 #region 事件--导出

        private void xButtonExport_Click(object sender, EventArgs e)
        {

            //导出前验证
            if (InventoryInquiresDataGrid.Rows.Count==0)
            {
                MessageBox.Show("没有要导出的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            GetSerachInfo();     //获取查询条件 searchObject对象
            StateBar sb = new StateBar();    //调用导出运行状态栏
            sb.Show("系统正在处理,请稍等...");
            try
            {
                ResultPage_StoreInventoryInquires rpo = null;
                IStore_InventoryInquires Proxy = SPF.Create<IStore_InventoryInquires>("Store_InventoryInquires_SRV");
                rpo = Proxy.SelectStore_InventoryExport(searchObject);
                sb.Close();

           DataTable eData = HidColumnExport(rpo.DataSetData.Tables["InventoryInquiresView"], InventoryInquiresDataGrid);  //去掉不用导出的列
                eData = UpdateExportTableColumnName(eData);  // 修改导出扩展属性的列名称
                //导出
                bool export = false;
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.OverwritePrompt = false;
                sfd.Filter = "excel文件|*.xls";
                sfd.FileName = "库存导出-" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    export = new ImportExportToExcel().ExportToExcel(eData, sfd.FileName,"Export_InventoryInquires");
                }
                if (export)
                {
                    sb.Close();
                    MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            catch
            {
                sb.Close();
                MessageBox.Show("查询失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion

 

        #region 方法--隐藏不用导出的列
        public DataTable HidColumnExport( DataTable exportTable,DataGridView dv)
        {

            #region
            for (int i = 0; i < exportTable.Columns.Count; i++)
            {
                bool flag = false;
                for (int j = 0; j < dv.ColumnCount; j++)
                 {
                     if (dv.Columns[j].Visible && (exportTable.Columns[i].ColumnName == dv.Columns[j].DataPropertyName))
                     {
                         //说明存在该列
                         flag = true;
                         break;
                     }
                 }
                //列不存在就删除
                if(!flag)
                {
                    exportTable.Columns.Remove(exportTable.Columns[i]);
                    i--;
                }
            }
            #endregion
            return exportTable;


        }


        #endregion

 

 

        #region 修改导出扩展属性的列名称
        public DataTable UpdateExportTableColumnName(DataTable exportTable)
        {
            List<Set_GoodsPropertyInfoObject> list = GetPropertyInfoByGoodsId(str_CompanyID);
            if (list != null)
            {
                foreach (Set_GoodsPropertyInfoObject setGoodsPropertyInfoObject in list)
                {
                    foreach (DataColumn goodsPropertyInfoObject in exportTable.Columns)
                    {
                        if("PropertyInfo"+setGoodsPropertyInfoObject.Num==goodsPropertyInfoObject.ColumnName)
                        {
                            goodsPropertyInfoObject.Caption=setGoodsPropertyInfoObject.PropertyName;
                        }
                    }
                }
            }

            return exportTable;
        }

        #endregion