在Excel中导出网格视图 [英] Export Grid View in Excel

查看:131
本文介绍了在Excel中导出网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导出GridView到Excel工作正常但我的问题是当excel打开

文件时,它变成了数字,这可能会导致问题,导致该字段是一个信用

卡号(例如1234567812345678),excel将显示1.23457E + 15,而

实际值变为1234567812345670,这是完全错误的。



我该怎么办

Export GridView to Excel is working Fine But My problem is when excel open the
file, it became numeric, this may cause problem, cause the field is a credit
card number (e.g 1234567812345678 ), the excel will show 1.23457E+15, and the
actual value became 1234567812345670, that is totally wrong.

so what Can I do

推荐答案

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
    </asp:gridview>
    <asp:button id="btnExporttoExcel" runat="server" onclick="btnExporttoExcel_Click" xmlns:asp="#unknown">
        Text="Export to Excel" />
    <asp:sqldatasource id="SqlDataSource1" runat="server"></asp:sqldatasource>
    </form>
</body>
</html></asp:button></html>







using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;



public partial class Export : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            mvBindgrid(); 
        }

    }
    public void mvBindgrid()
    {
        SqlConnection cn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter adap = new SqlDataAdapter();
        cn = new SqlConnection("Add your Connection string here");
        string strstring = "Select * from test";
        cmd = new SqlCommand(strstring, cn);
        adap = new SqlDataAdapter(cmd);
        adap.Fill(ds);
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
    }

    public void mvExportoExel()
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=FileName1.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        DataGrid g = new DataGrid();
        this.form1.Controls.Add(g);
        g.HeaderStyle.BackColor = System.Drawing.Color.Red;
        g.DataSource = ds;
        g.DataBind();
        foreach (DataGridItem i in g.Items)
        {

            foreach (TableCell tc in i.Cells)

                tc.Attributes.Add("class", "text");


        }
        g.RenderControl(htmlWrite);
        string style = @"<style> .text { mso-number-format:\@; } </style> ";

        Response.Write(style);

        Response.Write(stringWrite.ToString());

        Response.End();
    }

    protected void btnExporttoExcel_Click(object sender, EventArgs e)
    {
        mvBindgrid();
        mvExportoExel();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}


这篇关于在Excel中导出网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆