asp.net导出CSV逗号分隔值文件数字自动识别问题
来源:互联网 发布:手机美工软件 编辑:程序博客网 时间:2024/06/05 11:26
问题就是excel太聪明太智能了,网站也有很多人这样说了,
比如:自动识别数字和字符串,而且要把超过11位的数字自动变为科学计数法的格式,你试试输入“123456789012”,离开那个单元格,就成“123457E+11”了,够聪明吧,不过有时会让我们感觉不便,因为我输入的就是我自己的身份证号码,尾巴上没有“X”,本来好好的15位数字,得现在成这么个计数法了。那我就改改显示格式吧,改为把数字显示为文本,好了。可是国家的身份证升级了,号码变成18位,我把它输入到数据库,导出来时,用刚才的方法处理过,18位没错,可是最后三位怎么都是零了!
excel管的事实在太多了,有的时候我们只需要按照数据表里的数据准确的显示出来,我们就需要excel傻瓜化,只要按照字符串文本的形式显示出来,方法如下:
导出CSV逗号分隔值文件:
如果不需要自动识别数字等格式,只要文件按照字符串文本显示,
就在文本前面加上 /t ,这样就ok了,当excel打开它的时候就知道它不是数字列了;
导出excel文件:
在文本前面加上单引号,就ok了
下面是导出csv文件的具体方法:
语言 :C# 2.0
- /// <summary>
- /// 按照逗号分隔值文件格式整理GridView,转换、清理超文本标记
- /// </summary>
- /// <param name="dgdData">要导出的GridView</param>
- /// <param name="iFromCol">要导出的开始行索引</param>
- /// <param name="iToCol">要导出的结束行索引</param>
- /// <param name="sFileName">要生成的文件名</param>
- /// <param name="bOnlyVisuableCol">是否只显示Visuable为true的列</param>
- /// <param name="sTitle">输入到文件第一行的标题</param>
- /// <returns></returns>
- public static string ExportDvToCsvFile(GridView dgdData, int iFromCol, int iToCol, string sFileName, bool bOnlyVisuableCol, string sTitle)
- {
- bool flag;
- Type type;
- Control current;
- string str = "";
- if ((dgdData.Rows.Count == 0) | (dgdData.Rows.Count == 0))
- {
- return "未找到任何可导出的数据,此时不能导出!";
- }
- bool allowPaging = dgdData.AllowPaging;
- if (allowPaging)
- {
- dgdData.AllowPaging = false;
- dgdData.DataBind();
- }
- StringBuilder writer = new StringBuilder();
- if (StringType.StrCmp(sTitle, "", false) != 0)
- {
- writer.AppendLine(sTitle);
- }
- int num = iToCol;
- int num2 = iFromCol;
- while (num2 <= num)
- {
- if (!(!bOnlyVisuableCol | (bOnlyVisuableCol & dgdData.Columns[num2].Visible)))
- {
- goto Label_027D;
- }
- flag = false;
- type = dgdData.Columns[num2].GetType();
- if (type == typeof(HyperLinkField))
- {
- flag = true;
- }
- if (type == typeof(TemplateField))
- {
- IEnumerator enumerator = dgdData.Rows[0].Cells[num2].Controls.GetEnumerator();
- while (enumerator.MoveNext())
- {
- current = (Control)enumerator.Current;
- if (current.GetType() == typeof(Label))
- {
- flag = true;
- goto Label_01CD;
- }
- }
- }
- Label_01CD:
- if (type == typeof(BoundField))
- {
- flag = true;
- }
- if (type == typeof(TemplateField))
- {
- IEnumerator enumerator2 = dgdData.Rows[0].Cells[num2].Controls.GetEnumerator();
- while (enumerator2.MoveNext())
- {
- current = (Control)enumerator2.Current;
- if (current.GetType() == typeof(TextBox))
- {
- flag = true;
- goto Label_0256;
- }
- }
- }
- Label_0256:
- if (flag)
- {
- str = str + "/t" + ClearHtmlCont(dgdData.Columns[num2].HeaderText) + ",";
- }
- Label_027D:
- num2++;
- }
- str = Strings.Left(str, str.Length - 1);
- writer.AppendLine(str);
- int num3 = dgdData.Rows.Count - 1;
- string sLeft = "";
- for (int i = 0; i <= num3; i++)
- {
- str = "";
- int num5 = iToCol;
- num2 = iFromCol;
- while (num2 <= num5)
- {
- if (!(!bOnlyVisuableCol | (bOnlyVisuableCol & dgdData.Columns[num2].Visible)))
- {
- goto Label_052E;
- }
- type = dgdData.Columns[num2].GetType();
- flag = false;
- if (type == typeof(HyperLinkField))
- {
- IEnumerator enumerator3 = dgdData.Rows[i].Cells[num2].Controls.GetEnumerator();
- while (enumerator3.MoveNext())
- {
- current = (Control)enumerator3.Current;
- if (current.GetType() == typeof(HyperLink))
- {
- sLeft = ((HyperLink)current).Text;
- flag = true;
- goto Label_0391;
- }
- }
- }
- Label_0391:
- if (type == typeof(TemplateField))
- {
- IEnumerator enumerator4 = dgdData.Rows[i].Cells[num2].Controls.GetEnumerator();
- while (enumerator4.MoveNext())
- {
- current = (Control)enumerator4.Current;
- if (current.GetType() == typeof(Label))
- {
- sLeft = ((Label)current).Text;
- flag = true;
- goto Label_0419;
- }
- }
- }
- Label_0419:
- if (type == typeof(BoundField))
- {
- sLeft = dgdData.Rows[i].Cells[num2].Text;
- flag = true;
- }
- if (type == typeof(TemplateField))
- {
- IEnumerator enumerator5 = dgdData.Rows[i].Cells[num2].Controls.GetEnumerator();
- while (enumerator5.MoveNext())
- {
- current = (Control)enumerator5.Current;
- if (current.GetType() == typeof(TextBox))
- {
- sLeft = ((TextBox)current).Text;
- flag = true;
- }
- }
- }
- if (flag)
- {
- if (StringType.StrCmp(sLeft, " ", false) == 0)
- {
- sLeft = "";
- }
- if (num2 == 0)
- {
- string str5 = "";
- if (StringType.StrCmp(str5, sLeft, false) == 0)
- {
- sLeft = "";
- }
- else
- {
- str5 = sLeft;
- }
- }
- str = str + "/t" + ClearHtmlCont(sLeft.Replace("/r", " ")) + ",";
- }
- Label_052E:
- num2++;
- }
- str = Strings.Left(str, str.Length - 1);
- writer.AppendLine(str);
- }
- if (dgdData.ShowFooter)
- {
- str = "";
- int num6 = iToCol;
- for (num2 = iFromCol; num2 <= num6; num2++)
- {
- str = str + "/t" + dgdData.Columns[num2].FooterText + ",";
- }
- str = Strings.Left(str, str.Length - 1);
- writer.AppendLine(str);
- }
- if (allowPaging)
- {
- dgdData.AllowPaging = true;
- dgdData.DataBind();
- }
- return writer.ToString();
- }
- /// <summary>
- /// 直接输出Csv文件
- /// </summary>
- /// <param name="pgeParent">当前页面page对象</param>
- /// <param name="dgdData">要导出的GridView</param>
- /// <param name="iFromCel">要导出的开始行索引</param>
- /// <param name="iToCol">要导出的结束行索引</param>
- /// <param name="sFileName">要生成的文件名</param>
- /// <param name="bOnlyVisuableCol">是否只显示Visuable为true的列</param>
- /// <param name="sTitle">输入到文件第一行的标题</param>
- public static void ExportToCsvFile(ref Page pgeParent, GridView dgdData, int iFromCel, int iToCol, string sFileName, bool bOnlyVisuableCol, string sTitle)
- {
- if ((dgdData.Rows.Count == 0) | (dgdData.Rows.Count == 0))
- {
- RunScript(ref pgeParent, "<script language=javascript>alert('未找到任何可导出的数据,此时不能导出!')</script>");
- }
- else
- {
- HttpContext.Current.Response.Clear();
- HttpContext.Current.Response.Buffer = true;
- HttpContext.Current.Response.ContentType = "text/csv";//application/ms-excel
- HttpContext.Current.Response.AppendHeader("Content-Disposition",
- "attachment;filename=" +
- HttpUtility.UrlEncode(sFileName + ".csv", Encoding.UTF8));
- HttpContext.Current.Response.ContentEncoding = Encoding.Default; //Encoding.GetEncoding("GB2312");
- HttpContext.Current.Response.Write(
- ExportDvToCsvFile(dgdData, iFromCel, iToCol, sFileName, bOnlyVisuableCol, sTitle));
- HttpContext.Current.Response.End();
- }
- }
- /// <summary>
- /// 去除html超文本标记
- /// </summary>
- /// <param name="sCont"></param>
- /// <returns></returns>
- public static string ClearHtmlCont(string sCont)
- {
- int index = sCont.IndexOf("<");
- if (index >= 0)
- {
- int num2 = sCont.IndexOf(">");
- if (num2 > 0)
- {
- sCont = sCont.Remove(index, (num2 + 1) - index);
- return ClearHtmlCont(sCont);
- }
- }
- return sCont;
- }
- /// <summary>
- /// 清除指定html超文本标记
- /// 注:区分大小写
- /// </summary>
- /// <param name="sCont">要清除的字符串</param>
- /// <param name="name">指定清除的标签名,如超连接 a </param>
- /// <returns></returns>
- public static string ClearHtmlCont(string sCont,string name)
- {
- int index = sCont.IndexOf(string.Format("<{0}",name));
- if (index >= 0)
- {
- int num2 = sCont.IndexOf(">",index);
- if (num2 > 0)
- {
- sCont = sCont.Remove(index, (num2 + 1) - index);
- return ClearHtmlCont(sCont, name);
- }
- }
- index = sCont.IndexOf(string.Format("</{0}", name));
- if (index >= 0)
- {
- int num2 = sCont.IndexOf(">", index);
- if (num2 > 0)
- {
- sCont = sCont.Remove(index, (num2 + 1) - index);
- return ClearHtmlCont(sCont, name);
- }
- }
- return sCont;
- }
优点:
1、随时生成,不需要占用资源
2、可以结合身份认证
3、同样利于数据交换
csv文件是逗号分隔值文件,这样的文件当然有它的优点:
1、csv文件是文本文件,逗号隔开字段,回车隔开行,当直接输出到浏览器的时候,减少了很大的输出量,易于数据导入导出。
2、也是因为生成了文件,所以占用了服务器的空间,但是可以把文件名存放到数据库,再次给客户下载的时候不需要重复生成文件。
3、可以进行身份认证后给客户下载,如果放到非web目录就没有对应的url,客户无法随时下载。
其实你可以把同样的数据分别导出到成csv文件和excel文件,然后用记事本打开看看就知道量的差距了!
所以这里我就不说导出excel文件的方法了,网上的方法也不少!
- asp.net导出CSV逗号分隔值文件数字自动识别问题
- CSV逗号分隔值文件
- Mysql导出逗号分隔的csv文件
- asp.net导出csv文件
- 逗号分隔的值--CSV
- [概念]CSV逗号分隔值
- 导出到csv 文件(逗号分隔文件)不用poi 导出文件
- csv(逗号分隔值)文件用Excel编辑
- Asp.net 解决导出CSV文件乱码的问题
- CSV逗号分隔符文件解析
- 导出下载csv格式的表格(逗号分隔文件)的servlet代码,留待以后调用
- CSV(逗号分隔)、文本文件(制表符分隔) 等文件的读取
- 导出csv文件时,处理分隔符问题
- 导出csv文件时,处理分隔符问题
- 导出csv文件时,处理分隔符问题
- 导出csv文件时,处理分隔符问题
- 导出csv文件时,处理分隔符问题
- 导出csv文件时,处理分隔符问题
- 给自己看的哦
- java的序列化
- vi的各模式功能键
- 前端开发 IE 中的常用调试工具
- 老外一个很牛的画板
- asp.net导出CSV逗号分隔值文件数字自动识别问题
- Internet Explorer 编程简述(一)WebBrowser还是WebBrowser_V1收藏
- JavaScript调试工具大全【更新发布】【08.07.17】
- java读取文件的方法总结
- 人生感悟:社会从小教育我们要争强自立,可实际上我们更需要匹配思维
- C#如何实现两个WINFORM之间的访问?
- 程序员提高C#编程水平的50个要点[转]
- iframe自适应高度
- BeginUpdate()