我在我的内容页面中有4件事:
它们工作正常但是我注意到列中某些单元格格式化的一个奇怪问题,该列有2种格式应用于值,“数字”和“常规”,“数字”是不正确的
这里有一些前几个结果的图片来说明我在说什么:
在sql server
中
在内容页面
in excel
请注意,在sql server和页面上,单元格显示格式正确,格式为XXXXX.etc(常规格式),但具有更多数字的单元格格式为“数字”
我会发布一些代码:
gridview的
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
导出到Excel按钮
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);
Response.Output.Write(sWriter.ToString());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
有没有办法可以强制整个xls只使用“常规”格式?
答案 0 :(得分:3)
这对我有用
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();
答案 1 :(得分:2)
@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();
}
}
然而excel格式保持不变,一些单元格被格式化为“数字”而其他单元格被格式化为“一般”,我也尝试过:
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
但结果是一样的,我应该指出的另一件事是ITMREF_0中的所有记录都不应该像XXXXX.YYYY.ZZZZZ,我要发布截图:
12000073是正确的,excel应该像那样显示,22284.01.01也应该像excel中那样显示
我会很感激一些帮助
编辑:我想出来了,我只需要添加:
string style = @"<style> TD { mso-number-format:\@; } </style>";
Response.Write(style);
excel仍然给我一个警告,说这个数字被格式化为文本或者它有一个撇号但是没关系
编辑2:
正如我在第一篇文章中所说,我还有一个按钮,发送带有excel文件的电子邮件作为附件,其中包含gridview数据,我想对此方法执行相同的操作,将列格式设置为“general “但我无法弄清楚如何。
这是发送邮件按钮代码:
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);
}
有人能帮助我吗?
答案 2 :(得分:1)
你可以用它。它对我有用..
foreach (GridViewRow row in dummyGridView.Rows)
{
foreach (TableCell item in row.Cells)
{
item.Attributes.Add("class", "textmode");
}
}