dataset导出到Excel的格式问题
网上找了一个dataset导出到Excel的源码,能用,但问题是导出来后身份证号变成了科学计数法,日期也变了。源码如下:publicvoidCreateExcel(Dat...
网上找了一个dataset导出到Excel的源码,能用,但问题是导出来后身份证号变成了科学计数法,日期也变了。源码如下:
public void CreateExcel(DataSet ds, string FileName)
{
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/msexcel";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + "");
//this.EnableViewState = false;
string colHeaders = "", Is_item = "";
int i = 0;
//定义表对象与行对象,同时使用DataSet对其值进行初始化
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select("");
//取得数据表各列标题,标题之间以\t分割,最后一个列标题后加回车符
for (i = 0; i < dt.Columns.Count; i++)
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
colHeaders += "\n";
Response.Write(colHeaders);
//逐行处理数据
foreach (DataRow row in myRow)
{
//在当前行中,逐列取得数据,数据之间以\t分割,结束时加回车符\n
for (i = 0; i < dt.Columns.Count; i++)
{
var rowval = row[i];
if (rowval is DateTime)
{
DateTime datatime = DateTime.Parse(row[i].ToString());
rowval = " " + datatime.ToString("yyyy-MM-dd");
}
Is_item += rowval.ToString() + "\t";
}
Is_item += "\n";
Response.Write(Is_item);
Is_item = "";
}
//写缓冲区中的数据到HTTP头文件中
Response.End();
}
日期格式的问题我已经解决了,在前面加个空格就行了,但身份证号还是不行,谁告诉我这个怎么控制格式,或者怎么在这段代码的基础上将格式转为文本?(答对有奖) 展开
public void CreateExcel(DataSet ds, string FileName)
{
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/msexcel";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + "");
//this.EnableViewState = false;
string colHeaders = "", Is_item = "";
int i = 0;
//定义表对象与行对象,同时使用DataSet对其值进行初始化
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select("");
//取得数据表各列标题,标题之间以\t分割,最后一个列标题后加回车符
for (i = 0; i < dt.Columns.Count; i++)
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
colHeaders += "\n";
Response.Write(colHeaders);
//逐行处理数据
foreach (DataRow row in myRow)
{
//在当前行中,逐列取得数据,数据之间以\t分割,结束时加回车符\n
for (i = 0; i < dt.Columns.Count; i++)
{
var rowval = row[i];
if (rowval is DateTime)
{
DateTime datatime = DateTime.Parse(row[i].ToString());
rowval = " " + datatime.ToString("yyyy-MM-dd");
}
Is_item += rowval.ToString() + "\t";
}
Is_item += "\n";
Response.Write(Is_item);
Is_item = "";
}
//写缓冲区中的数据到HTTP头文件中
Response.End();
}
日期格式的问题我已经解决了,在前面加个空格就行了,但身份证号还是不行,谁告诉我这个怎么控制格式,或者怎么在这段代码的基础上将格式转为文本?(答对有奖) 展开
5个回答
2011-09-23
展开全部
绝对可以用。。你可以全部复制。。不过要加分。。
用GridView1可以设置格式啊。。靓仔
//导出EXCEL
protected void Button4_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "日志记录.xls");
}
public void Export(string FileType, string FileName)
{
Response.Charset = "big5";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//你把dataset绑定到GridView1就可以啊
不要说绑定你不会啊 。。就这两句而已
GridView1.DataSource=你的dataset
GridView1.DataBind();
你的dataset.Dispose();
//这里开始都是格式啊
GridView1.BackColor = System.Drawing.Color.White;
GridView1.ForeColor = System.Drawing.Color.Black;
GridView1.RowStyle.BackColor = System.Drawing.Color.White;
GridView1.RowStyle.ForeColor = System.Drawing.Color.Black;
GridView1.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
GridView1.AlternatingRowStyle.ForeColor = System.Drawing.Color.Black;
GridView1.BorderStyle = BorderStyle.Solid;
GridView1.BorderWidth = 1;
GridView1.HeaderStyle.BackColor = System.Drawing.Color.White;
GridView1.HeaderStyle.ForeColor = System.Drawing.Color.Black;
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.Columns[4].Visible = false;// 这一列便不会显示
//这里开始才是要用的
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
用GridView1可以设置格式啊。。靓仔
//导出EXCEL
protected void Button4_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "日志记录.xls");
}
public void Export(string FileType, string FileName)
{
Response.Charset = "big5";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = true;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//你把dataset绑定到GridView1就可以啊
不要说绑定你不会啊 。。就这两句而已
GridView1.DataSource=你的dataset
GridView1.DataBind();
你的dataset.Dispose();
//这里开始都是格式啊
GridView1.BackColor = System.Drawing.Color.White;
GridView1.ForeColor = System.Drawing.Color.Black;
GridView1.RowStyle.BackColor = System.Drawing.Color.White;
GridView1.RowStyle.ForeColor = System.Drawing.Color.Black;
GridView1.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
GridView1.AlternatingRowStyle.ForeColor = System.Drawing.Color.Black;
GridView1.BorderStyle = BorderStyle.Solid;
GridView1.BorderWidth = 1;
GridView1.HeaderStyle.BackColor = System.Drawing.Color.White;
GridView1.HeaderStyle.ForeColor = System.Drawing.Color.Black;
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.Columns[4].Visible = false;// 这一列便不会显示
//这里开始才是要用的
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
追问
我这个没有用gridveiw 控件啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
老兄 给你一个更简单 更爽的导出到excel的方法吧
#region 导出到excel
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
strContent.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
#endregion
protected void btnExport_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "Report" + DateTime.Now.ToString("MM-dd-ss") + ".xls");
}
#region 导出到excel
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
strContent.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
#endregion
protected void btnExport_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "Report" + DateTime.Now.ToString("MM-dd-ss") + ".xls");
}
追问
请问strContent.RenderControl(hw);
strContent是什么?
追答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在数据前面加单引号,变成文本格式。
追问
这个方法我试过了,导出时单引号存在的,双击一下那个单元格才能变成文本
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
@行不行
追问
这个不行
追答
如果这个数据在前台显示,你去查一下,绑定的时候可以设置列(td)的style样式,然后再导出;
如果不需要前台显示,你就找一个ExcelManager类,然后,手动设置sheet表的格式吧。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-09-25
展开全部
Excel VBA
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询