【问题标题】:Button click pulls wrong DataTable for export into Excel按钮单击拉出错误的数据表以导出到 Excel
【发布时间】:2020-10-30 22:34:35
【问题描述】:

我正在从 Web API 中提取公司日程数据并格式化数据,以便可以在本地站点上整齐地显示,也可以导出到 Excel 模板中,以便在整个公司内分发。

我在网站底部添加了一个按钮来显示前几周的日程安排,另一个按钮用于将数据导出到 Excel。单击“上一周”时,它会使用上周的数据填充相同的 Gridview1 表。但是当我点击导出按钮时,它会将本周的数据导出到 Excel 中。

这是我的一些代码,让我知道我还需要发布什么寻求帮助。

数据表在页面加载到网格视图时设置:

protected void Page_Load(object sender, EventArgs e)
        {

// ..... bunch of code above to call web API for date range and format table....

            DataView view = table.AsDataView();
            view.Sort = "Sort ASC";

            GridView1.DataSource = view;
            GridView1.DataBind();

            if (GridView1.Columns.Count > 0)
                GridView1.Columns[8].Visible = false;
            else
            {
                GridView1.HeaderRow.Cells[8].Visible = false;
                foreach (GridViewRow gvr in GridView1.Rows)
                {
                    gvr.Cells[8].Visible = false;
                }
            }

gridview 被拉入数据表并发送到 excel 电子表格

protected void ExportExcel(object sender, EventArgs e)
        {
            DataTable dt = new DataTable("Resident Schedule");
            foreach (TableCell cell in GridView1.HeaderRow.Cells)
            {
                dt.Columns.Add(cell.Text);
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                dt.Rows.Add();
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
                }
            }
            dt.Columns.Remove("Rotation");
            dt.Columns.Remove("Sort");

            using (XLWorkbook wb = new XLWorkbook(@"C:\template.xlsx"))
            {
                IXLWorksheet ws = wb.Worksheet("Resident Schedule");
                var rangeWithStrings = ws.Cell(5, 2).InsertTable(dt.AsEnumerable(), false);

// ..... bunch of code below

现在我有一个按钮单击方法,该方法被调用以将前一周加载到 GridView 中:

protected void PreviousWeek(object sender, EventArgs e)
        {

// ..... bunch of code above to pull and sort last weeks schedule....

            DataView view = table.AsDataView();
            view.Sort = "Sort ASC";

            GridView1.DataSource = view;
            GridView1.DataBind();

            if (GridView1.Columns.Count > 0)
                GridView1.Columns[8].Visible = false;
            else
            {
                GridView1.HeaderRow.Cells[8].Visible = false;
                foreach (GridViewRow gvr in GridView1.Rows)
                {
                    gvr.Cells[8].Visible = false;
                }
            }

所以,我的问题是,如果单击按钮正在重新填充 GridView1 数据,为什么 ExportExcel 方法会返回到页面加载数据?

【问题讨论】:

    标签: c# asp.net gridview datatable closedxml


    【解决方案1】:

    单击按钮,发送回发事件,似乎您错过了检查请求是否回发。因此,每当您单击按钮时,总是会使用当前周数据填充 gridview。检查回发请求,然后在加载事件上填充 gridview。

    if (!IsPostBack)
    {
         // GridView Populate code
    }
    

    【讨论】:

    • 谢谢!我在 page_load 事件周围添加了它,现在它从按钮单击事件中提取
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-16
    • 1970-01-01
    相关资源
    最近更新 更多