GridView导出Excel

        HttpResponse resp;
        resp = Page.Response;
        resp.Charset = "GB2312";
        resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");//HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)
        resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//.UTF8
        resp.ContentType = "application/ms-excel";

   Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");

   //解决导出乱码的问题

        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        gvAsset.DataSource = GridViewBridge("");//这个GridView为在前台定义的控件,有分页
        gvAsset.DataBind();

        GridView gv = new GridView();//实例化一个GridView,无分页,内容同gvAsset  

        gv.DataSource = GridViewBridge("");//数据源
        gv.DataBind();
        gv.RenderControl(oHtmlTextWriter);//gv是定义的不分页Gridview的名称
        Response.Write(oStringWriter.ToString());//Excel导出不支持分页
        Response.End();


    private DataSet GridViewBridge(string strWhere)
    {
        int iAssedId;
        decimal iPriceMin;
        decimal iPriceMax;

        typeModel.cType_Code = dropType.SelectedValue;
        wareModel.cWare_Code = dropWare.SelectedValue;
        if (txtEmpName.Text == "" || txtEmpName.Text == null)
        {
            assetModel.emp_Id = dropEmp.SelectedValue.Trim();
        }
        else
        {
            employeeModel.Emp_Name = txtEmpName.Text;//
        }
        if (dropDepartment.SelectedValue != "")
        {
            Desktop.BLL.Department newdepartment = new Desktop.BLL.Department();
            departmentModel.cChild = newdepartment.GetcChild(dropDepartment.SelectedValue);
        }
        employeeModel.beWorking = dropUserStatus.SelectedValue;
        assetModel.cAsset_Name = txtName.Text;
        assetModel.cAsset_Set = txtSet.Text;
        assetModel.cAsset_From = dropFrom.SelectedItem.Text;
        assetModel.cAsset_Status = dropStatus.SelectedItem.Text;
        if (dtpBeginTime.HasValue == true)
        {
            assetModel.dBuy_Date = dtpBeginTime.Value;
        }
        if (dtpEndTime.HasValue == true)
        {
            assetModel.dReg_Date = dtpEndTime.Value;
        }
        if (txtPriceMin.Text != "")
        {
            if (!decimal.TryParse(txtPriceMin.Text, out iPriceMin))
            {
                MessageBox.Show(this, "请输入数字型最小值");
                txtPriceMin.Focus();
                return null;
            }
            else
            {
                assetModel.iPrice = decimal.Parse(txtPriceMin.Text);
            }
        }
        if (txtPriceMax.Text != "")
        {
            if (!decimal.TryParse(txtPriceMax.Text, out iPriceMax))
            {
                MessageBox.Show(this, "请输入数字型最大值");
                txtPriceMax.Focus();
                return null;
            }
            else
            {
                assetModel.iCurPrice = decimal.Parse(txtPriceMax.Text);
            }
        }
        assetModel.cCheck = dropCheckStatus.SelectedValue;
        assetModel.cBudNo = txtBudNo.Text;
        assetModel.cAccNo = txtAccNo.Text;
        assetModel.cAssetNo = txtAssetNo.Text;
        assetModel.cCheckNo = txtCheckNo.Text;
        assetModel.cAccCorp = txtAccCorp.Text;
        assetModel.cAccCurCorp = txtAccCurCorp.Text;
        assetModel.cSelYes = DropYes.SelectedValue;
        if (txtAssetID.Text != "")
        {
            if (!int.TryParse(txtAssetID.Text, out iAssedId))
            {
                MessageBox.Show(this, "输入系统编号不正确");
                txtAssetID.Focus();
                return null;
            }
            {
                assetModel.iAsset_ID = int.Parse(txtAssetID.Text);
            }
        }

        DataSet ds = asset.GetAssetMain(assetModel, typeModel, wareModel, employeeModel, departmentModel, strWhere);
        return ds;
        // GridViewBind(strWhere);

    }

即数据显示的时候用gvAsset显示,有分页功能,但是Excel导出数据的时候从我们自己定义的gv里面取数据,注意gv是没有分页功能的,可以导出完整数据

你可能感兴趣的:(GridView)