Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用自定义值格式将gridview导出到excel_C#_Asp.net_Sql_Excel_Gridview - Fatal编程技术网

C# 使用自定义值格式将gridview导出到excel

C# 使用自定义值格式将gridview导出到excel,c#,asp.net,sql,excel,gridview,C#,Asp.net,Sql,Excel,Gridview,我在我的内容页中有4件事: 单一网格视图 执行数据库视图并在gridview中的视图内显示查询结果的查询按钮 “导出到excel”按钮,用于将gridview导出到excel中 将上述excel作为附件发送电子邮件 它们工作正常,但是我注意到一列中某些单元格的格式有一个奇怪的问题,该列有两种格式应用于值,“number”和“general”,即“number”是不正确的格式 下面是前几个结果的一些图片来说明我的意思: 在sql server中 在内容页中 在excel中 请注意,在sq

我在我的内容页中有4件事:

  • 单一网格视图
  • 执行数据库视图并在gridview中的视图内显示查询结果的查询按钮
  • “导出到excel”按钮,用于将gridview导出到excel中
  • 将上述excel作为附件发送电子邮件
它们工作正常,但是我注意到一列中某些单元格的格式有一个奇怪的问题,该列有两种格式应用于值,“number”和“general”,即“number”是不正确的格式

下面是前几个结果的一些图片来说明我的意思:

在sql server中

在内容页中

在excel中

请注意,在sql server和页面上,单元格以正确的格式显示,即XXXXX.etc(常规格式),但数字较多的单元格格式为“数字”

我将在下面发布一些代码:

网格视图

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>

有没有一种方法可以强制整个xls只进行“常规”格式设置?

@Poormina,谢谢您的输入,我的代码是:

protected void Buttonexcel_Click(object sender, EventArgs e)
{

    try
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.Charset = "";
        Response.AddHeader("content-disposition", "attachment;filename=dados.xls");
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
        GridView1.RenderControl(hWriter);
        string style = @"<style> .textmode {mso-number-format:General} </style>";
        Response.Output.Write(sWriter.ToString());
        Response.Flush();
        Response.End();
    }
    catch (Exception ex)
    {
        Label1.Text = ex.ToString();
    }

}
受保护的无效按钮单击(对象发送者,事件参数e)
{
尝试
{
Response.Clear();
Response.Buffer=true;
Response.ContentType=“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”;
响应。Charset=“”;
AddHeader(“内容处置”、“附件;文件名=dados.xls”);
StringWriter sWriter=新StringWriter();
hWriter=新的HtmlTextWriter(sWriter);
GridView1.渲染控制(hWriter);
字符串样式=@“.textmode{mso数字格式:General}”;
Response.Output.Write(sWriter.ToString());
Response.Flush();
Response.End();
}
捕获(例外情况除外)
{
Label1.Text=ex.ToString();
}
}
但是excel格式保持不变,一些单元格格式为“数字”,其他单元格格式为“常规”,我也尝试了:

string style = @"<style> .textmode { mso-number-format:\@; } </style>";
string style=@.textmode{mso数字格式:\@;}”;
但结果是一样的,我要指出的另一件事是,并不是ITMREF_0中的所有记录都应该像XXXXX.yyy.ZZZZZ,我将发布一个屏幕截图如下:

12000073是正确的,excel应该这样显示,22284.01.01也应该这样显示在excel中

我会很感激你的帮助


编辑:我想出来了,我只需要添加:

string style = @"<style> TD { mso-number-format:\@; } </style>";
        Response.Write(style);
string style=@“TD{mso数字格式:\@;}”;
回应。写作(风格);
excel仍然给我一个警告,告诉我数字的格式是文本,或者它有一个撇号,但没关系

编辑2:

正如我在第一篇文章中所说,我还有一个按钮,可以发送一封电子邮件,其中包含一个excel文件作为附件,其中包含gridview数据。我想对这种方法做同样的事情,将列格式化为“常规”,但我不知道如何处理

以下是“发送电子邮件”按钮代码:

protected void Buttonmail_Click(object sender, EventArgs e)
{
    fn_AttachGrid();
}

public void fn_AttachGrid()
{

    StringWriter sWriter = new StringWriter();
    HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
    GridView1.RenderControl(hWriter);
    MailMessage mail = new MailMessage();
    mail.IsBodyHtml = true;
    mail.To.Add(new MailAddress(txtto.Text));
    mail.Subject = "Foi";
    System.Text.Encoding Enc = System.Text.Encoding.ASCII;
    byte[] mBArray = Enc.GetBytes(sWriter.ToString());
    string style = @"<style> TD { mso-number-format:\@; } </style>";
    Response.Write(style);
    System.IO.MemoryStream mAtt = new System.IO.MemoryStream(mBArray, false);
    mail.Attachments.Add(new Attachment(mAtt, "rotina.xls"));
    mail.Body = "Foi detectado o seguinte problema";
    SmtpClient smtp = new SmtpClient();
    mail.From = new MailAddress("email_from", "name displayed");
    smtp.Host = "smtp.gmail.com";
    smtp.UseDefaultCredentials = true;
    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
    NetworkCred.UserName = "email_from";
    NetworkCred.Password = "password";
    smtp.Credentials = NetworkCred;
    smtp.EnableSsl = true;
    smtp.Port = 587;
    smtp.Send(mail);
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Enviado com sucesso.');", true);
}
受保护的无效按钮邮件单击(对象发送者,事件参数e)
{
fn_AttachGrid();
}
公共无效fn_附件网格()
{
StringWriter sWriter=新StringWriter();
hWriter=新的HtmlTextWriter(sWriter);
GridView1.渲染控制(hWriter);
MailMessage mail=新的MailMessage();
mail.IsBodyHtml=true;
mail.To.Add(新邮件地址(txtto.Text));
mail.Subject=“Foi”;
System.Text.Encoding Enc=System.Text.Encoding.ASCII;
字节[]mBArray=Enc.GetBytes(sWriter.ToString());
字符串样式=@“TD{mso数字格式:\@;}”;
回应。写作(风格);
System.IO.MemoryStream mAtt=新的System.IO.MemoryStream(mBArray,false);
mail.Attachments.Add(新附件(mAtt,“rotina.xls”);
mail.Body=“Foi detectado o seguinte problema”;
SmtpClient smtp=新SmtpClient();
mail.From=新的邮件地址(“email_From”,“显示名称”);
smtp.Host=“smtp.gmail.com”;
smtp.UseDefaultCredentials=true;
System.Net.NetworkCredential NetworkCred=新系统.Net.NetworkCredential();
NetworkCred.UserName=“email\u from”;
NetworkCred.Password=“Password”;
smtp.Credentials=NetworkCred;
smtp.EnableSsl=true;
smtp.Port=587;
smtp.发送(邮件);
ScriptManager.RegisterClientScriptBlock(这是,typeof(Page),“anythis”,“alert('Enviado com successo.');”,true);
}

有人能帮我吗?

你可以用这个。这对我很有用

foreach (GridViewRow row in dummyGridView.Rows)
                {
                    foreach (TableCell item in row.Cells)
                    {
                        item.Attributes.Add("class", "textmode");
                    }
                }
这对我有用

        var grid = new GridView();
        grid.DataSource = candidates;
        grid.DataBind();
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=candidates.xls");
        Response.ContentType = "application/excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grid.RenderControl(htw);
        string style = @"<style> td { mso-number-format:\@;} </style>";
        Response.Write(style);
        Response.Write(sw.ToString());
        Response.End();
var grid=new GridView();
grid.DataSource=候选项;
grid.DataBind();
Response.ClearContent();
AddHeader(“内容处置”、“附件;文件名=候选者.xls”);
Response.ContentType=“应用程序/excel”;
StringWriter sw=新的StringWriter();
HtmlTextWriter htw=新的HtmlTextWriter(sw);
网格渲染控制(htw);
字符串样式=@“td{mso数字格式:\@;}”;
回应。写作(风格);
Response.Write(sw.ToString());
Response.End();

您可以尝试使用Response.Write将数字转换为字符串来添加样式。看看这个链接:我明白了,我只需要做这个字符串样式=@“TD{mso number format:\@;}”;字节[]mBArray=Enc.GetBytes(style+sWriter.ToString());在导出到excel时,我并没有像在gridiview中使用的那样获得格式或颜色。
        var grid = new GridView();
        grid.DataSource = candidates;
        grid.DataBind();
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=candidates.xls");
        Response.ContentType = "application/excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grid.RenderControl(htw);
        string style = @"<style> td { mso-number-format:\@;} </style>";
        Response.Write(style);
        Response.Write(sw.ToString());
        Response.End();