简体   繁体   中英

Export Sql Server Data To Excel

Export to Excel is working fine, but if the number has 16 digits it's seen like this:

4.26637E+11 

My Emp_ID:

426636773888

What can I do to display Emp_ID like this: 426636773888

.CS Code

string strQuery = "select * from Employee;
    SqlCommand cmd1 = new SqlCommand(strQuery);
    DataTable dt = GetData(cmd1);

    string attachment = "attachment; filename=Employee.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.ms-excel";
    string tab = "";
    foreach (DataColumn dc in dt.Columns)
    {
        Response.Write(tab + dc.ColumnName);
        tab = "\t";
    }
    Response.Write("\n");

    int i;
    foreach (DataRow dr in dt.Rows)
    {
        tab = "";
        for (i = 0; i < dt.Columns.Count; i++)
        {
            Response.Write(tab + dr[i].ToString());
            tab = "\t";
        }
        Response.Write("\n");
    }
    Response.End();

See Image 在此处输入图片说明

尝试使用ExcelPackage http://EPPlus.codeplex.com做到这一点,它将允许您指定列的数据类型,我以前在日期字段中遇到过同样的问题。

尝试为此字段以Excel公式格式="426636773888"导出长数字:

Response.Write(tab + "=\""+dr[i].ToString()+"\"");

A simple way:

Change
Response.Write(tab + dr[i].ToString());

To
Response.Write(tab + "'" + dr[i].ToString());

Just adding a single quote in front of the data, then excel will consider the data as a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM