ExportToExcel生成自定义列如何做?如何避免导出身份证号为数字?

ekinchen3 2014-06-10 03:21:17
使用ExportToExcel函数,附后,
调用如下:
BLL.farmers bll = new BLL.farmers();
DataSet ds = bll.GetListForExcel(txtSQL.Text.ToString().Trim());
IList dList = ds.Tables[0].DefaultView;
string nl = DateTime.Now.ToString();
ExportHelper.ExportToExcel(dList, new string[] { "name", "sex", "birth", "idcard", "scores", "evaluate"}, new string[] { "姓名", "性别", "出生日期", "身份证号", "得分", "评价“ }, "综合查询结果");

需求一:还想生成一列为年龄,通过出生日期计算出来的,如何做啊?好像这个函数只支持生成已有数据库字段的?
需求二:导出身份证一列为数字型,例如3.40421E+17,
如何导出字符型?


public class ExportHelper
{

public static void ExportToExcel(IList dataList, string[] fields, string[] headTexts, string title)
{
GridView gvw = new GridView();
int ColCount, i;

//如果筛选的字段和对应的列头名称个数相对的情况下只导出指定的字段
if (fields.Length != 0 && fields.Length == headTexts.Length)
{
ColCount = fields.Length;
gvw.AutoGenerateColumns = false;

for (i = 0; i < ColCount; i++)
{
BoundField bf = new BoundField();
bf.DataField = fields[i];
bf.HeaderText = headTexts[i];
gvw.Columns.Add(bf);
}
}
else
{
gvw.AutoGenerateColumns = true;
}

SetStype(gvw);
gvw.DataSource = dataList;
gvw.DataBind();

ExportToExcel(gvw, title);
}
/// <summary>
/// 导出数据到Excel
/// </summary>
/// <param name="DataList">IList Data</param>
/// <param name="Fields">要导出的字段</param>
/// <param name="HeadName">字段对应显示的名称</param>
public static void ExportToExcel(IList dataList, string[] fields, string[] headTexts)
{
ExportToExcel(dataList, fields, headTexts, string.Empty);
}

/// <summary>
/// 设置样式
/// </summary>
/// <param name="gvw"></param>
private static void SetStype(GridView gvw)
{
gvw.Font.Name = "Verdana";
gvw.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
gvw.HeaderStyle.BackColor = System.Drawing.Color.LightCyan;
gvw.HeaderStyle.ForeColor = System.Drawing.Color.Black;
gvw.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
gvw.HeaderStyle.Wrap = false;
gvw.HeaderStyle.Font.Bold = false;
gvw.HeaderStyle.Font.Size = 15;
gvw.RowStyle.Font.Size = 12;
gvw.RowStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
}
/// <summary>
/// 导出GridView中的数据到Excel
/// </summary>
/// <param name="gvw"></param>
/// <param name="DataList"></param>
private static void ExportToExcel(GridView gvw, string title)
{
string fileName;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Charset = "GB2312";
// HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
fileName = string.Format("{0:yyyy-MM-dd_HH_mm_ss}.xls", DateTime.Now);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gvw.RenderControl(hw);
if (!string.IsNullOrEmpty(title))
{
HttpContext.Current.Response.Write("<b><center><font size=5 face=Verdana color=#000>" + title + "</font></center></b>");
}
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();

gvw.Dispose();
tw.Dispose();
hw.Dispose();

gvw = null;
tw = null;
hw = null;

}


}
...全文
410 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
save4me 2014-06-10
  • 打赏
  • 举报
回复
上面代码错了

        gvw.RenderControl(hw);
        //string style = @"<style> .textmode {mso-number-format:General} </style>";
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Output.Write(tw.ToString());

save4me 2014-06-10
  • 打赏
  • 举报
回复
处理输出文本关键是在RenderControl后面加一行输出的格式

        gvw.RenderControl(hw);
        //string style = @"<style> .textmode {mso-number-format:General} </style>";
        string style = @"style> .textmode { mso-number-format:\@; } </style> </style>";
        Response.Output.Write(tw.ToString());
save4me 2014-06-10
  • 打赏
  • 举报
回复
因为你的数据源是DataTable,所以你可以在DataTable中添加一列年龄列,关于如何在DataTable中添加列,参考 http://msdn.microsoft.com/ZH-CN/library/hfx3s9wd(v=vs.110).aspx 创建表达式列 关于输出数据类型的问题,看看下面的链接,或者你给身份证那列前面加上单引号,这样Excel会识别为文本 Export GridView to Excel in ASP.Net with Formatting using C# and VB.Net
  • 打赏
  • 举报
回复
用模板Excel导出,不要直接导出,然后设置模板里面的身份证号码那列为数字就可以了
ekinchen3 2014-06-10
  • 打赏
  • 举报
回复
没人帮忙吗~~~~~~~~~~~

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧