繁体   English   中英

如何将C#excel导出中的复选框替换为1和0?

[英]How do I replace the checkboxes in my C# excel export to ones and zeros?

在C#应用程序中,我动态创建了GridView并将其导出到Excel。 我遇到的问题是,由于三个字段是SQL中的Bit列,因此它们导出为复选框,而不是一和零。 我在Google上做了一些类似问题的搜索,并找到了本文的解决方案。 但是,这将无法解决,因为在对象上单击鼠标右键或鼠标左键只会选中或取消选中该框。

我还试图找到有关如何从C#中将位字段导出为1和0的信息,但找不到任何内容。 这是我的导出脚本:

StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
GridView gridView = new GridView();
gridView.DataSource = sdsResults;
gridView.AutoGenerateColumns = true;
gridView.DataBind();
gridView.HeaderRow.Style.Add("background-color", "#003c74");
gridView.HeaderRow.Style.Add("color", "#ffffff");
for (int i = 0; i < gridView.Rows.Count; i++)
        {
            GridViewRow row = gridView.Rows[i];

            //Change Color back to white
            row.BackColor = System.Drawing.Color.White;

            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");

            //Apply style to Individual Cells of Alternating Row
            if (i % 2 != 0)
            {
                row.BackColor = System.Drawing.Color.AliceBlue;
            }

            foreach(TableCell cell in row.Cells)
            {
                if(cell.HasControls() == true)
                {
                  if(cell.Controls[0].GetType().ToString() == "System.Web.Ui.WebControls.CheckBox")
                   {
                       CheckBox chk = (CheckBox)cell.Controls[0];
                        if(chk.Checked)
                        {
                           cell.Text = "1";
                        }
                        else
                        {
                            cell.Text = "0";
                        }
                   }
                }
            }
        }
gridView.RenderControl(htmlWriter);
htmlWriter.Close();

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=myfile.xls");
Response.Charset = "";
Response.Write(writer.ToString());
Response.End();
Response.End();

如果使用SQL查询构建数据源,则将位列CAST转换为int或varchar。 如果无法做到这一点,请尝试在分配数据源之前用所需的数据类型定义GridView列。

在for循环中,您可以执行以下操作:

foreach(TableCell cell in row.Cells)
 {
   if(cell.HasControls() == true)
    {
      if(cell.Controls[0].GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
       {
           CheckBox chk = (CheckBox)cell.Controls[0];
           if(chk.Checked)
            {
               cell.Text = "1";
            }
           else{
                cell.Text = "0";
               }
       }
    }
 }
protected void btnXlsExport_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    gvStandard.AllowPaging = false;
    gvStandard.DataBind();
    for (int i = 0; i <= gvStandard.Rows.Count - 1; i++) {
        GridViewRow row = gvStandard.Rows(i);
        foreach (TableCell cell in row.Cells) {
            if (cell.HasControls() == true) {
                if (cell.Controls(0).GetType().ToString() == "System.Web.UI.WebControls.CheckBox") {
                    CheckBox chk = (CheckBox)cell.Controls(0);
                    if (chk.Checked) {
                        cell.Text = "True";
                    } else {
                        cell.Text = "False";
                    }
                }
            }
        }
    }
    gvStandard.HeaderRow.Style.Add("background-color", "#FFFFFF");
    gvStandard.RenderControl(hw);
    Response.Output.Write(sw);
    Response.Flush();
    Response.End();
}

受保护的Sub btnXlsExport_Click(发送者为对象,e作为EventArgs)处理btnXlsExport.Click

    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    gvStandard.AllowPaging = False
    gvStandard.DataBind()
    For i As Integer = 0 To gvStandard.Rows.Count - 1
        Dim row As GridViewRow = gvStandard.Rows(i)
        For Each cell As TableCell In row.Cells
            If cell.HasControls() = True Then
                If cell.Controls(0).[GetType]().ToString() = "System.Web.UI.WebControls.CheckBox" Then
                    Dim chk As CheckBox = DirectCast(cell.Controls(0), CheckBox)
                    If chk.Checked Then
                        cell.Text = "True"
                    Else
                        cell.Text = "False"
                    End If
                End If
            End If
        Next
    Next
    gvStandard.HeaderRow.Style.Add("background-color", "#FFFFFF")
    gvStandard.RenderControl(hw)
    Response.Output.Write(sw)
    Response.Flush()
    Response.End()
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM