列值自定义 - 在 ASP.NET 中导出 Excel

发布于 2024-11-01 04:44:28 字数 213 浏览 1 评论 0原文

我必须将gridview导出到excel工作表2003中。使用我的代码,我可以将数据直接导出到eXcel工作表中。但是如果该列有空值,那么如何在 Excel 工作表中替换为空字符串。有一个日期列为空,在单击导出按钮和将数据加载到 Excel 之间是否发生任何事件处理程序。如果有的话,我可以比较数据库中的值,并可以用空字符串替换空值。在事件处理程序中。

请指出我正确的方向。

谢谢

I have to export gridview in to the excel sheet 2003. With my code I can Export the data into eXcel Sheet directly. But If there is a null value for the column, Then how do I replace with empty string in Excel sheet. There is a date column which is null, is there any event handler that occurs between export button click and the loading the data into excel. If there is one I can compare the values in the database and can replace the null value with empty String. in the evnt handler.

Please point me in right direction.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2024-11-08 04:44:28

单击导出按钮后,您可以遍历网格行并替换您需要的任何内容。例如,下面的代码将用 . 替换 :

protected void btnExport_Click(object sender, EventArgs e)
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    string attachment = "attachment; filename=SummaryReport" + DateTime.Now.ToString() + ".xls";

    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";

    foreach (GridViewRow grdRow in grdProjectTasks.Rows)
    {
        Label lblActualDuration = (Label)grdRow.FindControl("lblActualDuration");

        lblActualDuration.Text = lblActualDuration.Text.Replace(":", ".");
    }

    grdProjectTasks.RenderControl(htw);

    Response.Write(sw.ToString());
    Response.End();
}

On click of the export button, you can iterate through your grid rows and replace whatever you need. For example, the code below will replace : with .:

protected void btnExport_Click(object sender, EventArgs e)
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    string attachment = "attachment; filename=SummaryReport" + DateTime.Now.ToString() + ".xls";

    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";

    foreach (GridViewRow grdRow in grdProjectTasks.Rows)
    {
        Label lblActualDuration = (Label)grdRow.FindControl("lblActualDuration");

        lblActualDuration.Text = lblActualDuration.Text.Replace(":", ".");
    }

    grdProjectTasks.RenderControl(htw);

    Response.Write(sw.ToString());
    Response.End();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文