Home > php教程 > php手册 > body text

GridView 导入Excel

WBOY
Release: 2016-06-06 20:01:32
Original
1079 people have browsed it

%@ Page EnableEventValidation="false" % 一、引用如下命名空间 using System.IO; using System.Text; Response.ClearContent(); Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF8");//防止乱码 string stuname = System.Web.HttpUtilit

 

        

 

 

 

一、引用如下命名空间
using System.IO;
using System.Text;

 

 

 

Response.ClearContent();

 

Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF8");//防止乱码

 

        string stuname = System.Web.HttpUtility.UrlEncode(this.j.Text.Trim() + "成绩单", System.Text.Encoding.UTF8);

        Response.AddHeader("content-disposition", "attachment; filename=" + stuname + ".xls");

        Response.ContentType = "application/excel";

 

        StringWriter sw = new StringWriter();

        sw.WriteLine("

大学学生成绩表

");

        sw.WriteLine("院系:" + this.yx.Text + "     " + "学制:" + "       " + "入学时间:" + "   " + "学号:" + this.xuehao.Text);

        sw.WriteLine("
专业:" + this.zy.Text + "     培养层次:" + this.chenci.Text + "      毕业时间:             姓名:" + this.j.Text);

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        GridView1.RenderControl(htw);

        Response.Write(sw.ToString());

        Response.End();

 

 

    public override void VerifyRenderingInServerForm(Control control)
    {
    }

 

 

 

在导出的时候,如果某个字段为长数字(如身份证号码511922198507151512)、以0开头的编号(如0809111212)之类的数据。如果不加处理在导出的Excel文件中将会被分别当作5.11922E+17和809111212来处理,这样与我们要达到 的实际效果不一致。所以我们要加以处理,即给单元格数据规定格式。常见的格式如下:

1) 文本:vnd.ms-excel.numberformat:@
2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
3) 数字:vnd.ms-excel.numberformat:#,##0.00
4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
5) 百分比:vnd.ms-excel.numberformat: #0.00%

使用方法如下:

//给第一个单元格设置格式为

e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");

//给第四个单元格设置格式为
e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");

 

解决乱码:

 

Response.ContentType = "application/excel";

        Response.Write("");

 

 

Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!