在所做的几个项目中都做过导出系统数据到EXCEL中,但屡次都出现导出长串数据到EXCEL中后显示为科学计数法,在网上查找了些方法,大致有两种解决途径:
一种是最不方便但能解决问题的方法,使用EXCEL的字符类型设置将科学计数法在EXCEL文件中转换为文本类型
此类方法可通过打开EXCEL,通过菜单的文件---->打开---->选择导出的EXCEL文件 在弹出的设置窗口中设置字段的类型为文本,通过测试能够完成,但第二天用同样方式又无法完成,不晓得为什么,郁闷啊!
此类方法还有一种操作方式就是选中科学计数法那一列,右键选择设置单元格格式---- > 数值---->小数位数设置为0---->确定就可以完成,但细看数据会发现此种方法修改后的数据末尾均变成了0,数据有误,无法满足实际需求
还有一种解决方法是在代码中解决,在网上找了些此种方法的代码


java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_uijava excel导出不适用科学计数法 oracle 导出excel总是科学计数法_ui_02解决导出数据到EXCEL科学计数法问题


java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03

 在asp.net 中 我一般都是将要导出的数据放到gridview网格里,首先对网格邦定数据时 字符串形式处理,然后再用普通的形式导出excel就把问题解决了。

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03

  我的代码非常简单:在邦定gridview控件时在rowdatabound事件中队数据格式化

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03

protected void gError_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
           //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%
        for (int i = 0; i < e.Row.Cells.Count; i++)
        
{
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Cells[i ].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        }
           
    }

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03

   然后执行到处操作就不会出现问题了

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03

protected void btnOut_Click(object sender, EventArgs e)
    
{
        
       Response.Clear();
       Response.Buffer = true;
        Response.Charset = "GB2312";
       Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
     
        Response.ContentEncoding = System.Text.Encoding.UTF7;
      //设置输出文件类型为excel文件。 
      Response.ContentType = "application/ms-excel";
       System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
       System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
       this.gError.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();
    }
 
public override void VerifyRenderingInServerForm(Control control)
    
{
        //base.VerifyRenderingInServerForm(control);
    }

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03

在winform程序开发时,处理的办法就是在导出的过程中,开始试了 处理excel对象的格式 mysheet.Cells.NumberFormat = "#";

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03

后来没有成功。最后还是用了逐条纪录进行字符格式转化的方法,即添加“ '  ”.

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03

 我写得代码主要部分如下

执行数据导出#region  执行数据导出
            try
            
{
                //到导出excel
                Excel.ApplicationClass my = new Excel.ApplicationClass();
                if (my == null)
                
{
                    MessageBox.Show("无法创建excel对象,可能您的系统没有安装excel");
                    return;
                }
                my.Visible = false;
                Excel.Workbook mybook = (Excel.Workbook)my.Workbooks.Add(1);
                ((Excel.Worksheet)mybook.Worksheets[1]).Name = "sheet1";
                Excel.Worksheet mysheet = (Excel.Worksheet)mybook.Worksheets[1];
                // mysheet.Cells.NumberFormat = "#";
               //导出列名
                for (int j = 0; j < this.dgvShow.Columns.Count; j++)
                
{
                    if (this.dgvShow.Columns[j].Visible == true)
                    
{
                        mysheet.Cells[1, j + 1] = "'" + Convert.ToString(this.dgvShow.Columns[j].HeaderText);//加"'"防止科 学计数法
                    }
                }
                //导出数据
                for (int i = 0; i < this.dgvShow.Rows.Count; i++)
                
{
                    for (int j = 0; j < this.dgvShow.Columns.Count; j++)
                    
{
                        mysheet.Cells[i + 2, j + 1] = "'" + Convert.ToString(this.dgvShow.Rows[i].Cells[j].Value);
                    }
                }
                if (savefilename != "")
                
{
                    try
                    
{
                        //mybook.Save();
                        mybook.SaveCopyAs(savefilename);
                        MessageBox.Show("excel文件导出成功!");
                    }
                    catch (Exception ex)
                    
{
                        MessageBox.Show("导出文件出现错误,文件可能正被打开!\n" + ex.Message);
                    }
                }

                GC.Collect();
            }
            catch (Exception ex)
            
{
                MessageBox.Show("数据导出时出现错误,一下是详细错误信息:\n" + ex.Message);
                return;
            }
            #endregion

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03



此种方法尝试过仍然失败,EXCEL科学计数法问题解决方法继续寻找中......

孟子E章源码实例 
http://dotnet.aspx.cc/ShowDetail.Aspx?id=8A4CBF47-B888-4832-3389-ED3A3A3C8AAB

OutPutExcel.aspx

Code    1
<%
@ Page language="c#" Codebehind="OutPutExcel.aspx.cs"
 2
 AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %>
 3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 4
<HTML>
 5
<HEAD>
 6
<title>OutPutExcel</title>    
 7
</HEAD>
 8
<body>
 9
<form id="Form1" method="post" runat="server">
10
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
11
<asp:Button id="Button1" runat="server" Text="输出到Excel"></asp:Button>
12
</form>
13
</body>
14
</HTML>
15
  
OutPutExcel.aspx.cs  
   Code     1
using System;
  2
using System.Collections;
  3
using System.ComponentModel;
  4
using System.Data;
  5
using System.Drawing;
  6
using System.Web;
  7
using System.Web.SessionState;
  8
using System.Web.UI;
  9
using System.Web.UI.WebControls;
 10
using System.Web.UI.HtmlControls;
 11

 12
namespace eMeng.Exam
 13
{
 14
/**//// <summary>
 15
/// OutPutExcel 的摘要说明。
 16
/// </summary>
 17
public class OutPutExcel : System.Web.UI.Page
 18
{
 19
protected System.Web.UI.WebControls.Button Button1;
 20
protected System.Web.UI.WebControls.DataGrid DataGrid1;
 21

 22
private void Page_Load(object sender, System.EventArgs e)
 23
{
 24
// 在此处放置用户代码以初始化页面
 25
DataGrid1.DataSource=CreateDataSource();
 26
DataGrid1.DataBind();
 27
}
 28
/**//// <summary>
 29
/// 创建数据源
 30
/// </summary>
 31
/// <returns>DataView</returns>
 32
ICollection CreateDataSource() 
 33
{
 34

 35
DataTable dt = new DataTable();
 36
DataRow dr;
 37
dt.Columns.Add(new DataColumn("身份证号码", typeof(string)));
 38
dt.Columns.Add(new DataColumn("图书单价",typeof(decimal)));
 39
dt.Columns.Add(new DataColumn("购买数量",typeof(Int32)));
 40
dt.Columns.Add(new DataColumn("总价格",typeof(decimal)));
 41

 42

 43
for (int i = 0; i < 30; i++) 
 44
{
 45
dr = dt.NewRow();
 46

 47
dr[0] = "123456789123456789";
 48
dr[1] = 100 * i /3.0;
 49
dr[2] = i + 5;
 50
dr[3] = (decimal)dr[1] * (Int32)dr[2];                
 51
dt.Rows.Add(dr);
 52
}
 53
DataView dv = new DataView(dt);
 54
return dv;
 55
}
 56
/**//// <summary>
 57
/// 输出到Excel
 58
/// </summary>
 59
/// <param name="sender"></param>
 60
/// <param name="e"></param>
 61
private void Button1_Click(object sender, System.EventArgs e)
 62
{
 63
Response.Clear(); 
 64
Response.Buffer= true; 
 65
Response.Charset="GB2312";    
 66
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls"); 
 67
Response.Cnotallow=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
 68
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
 69
this.EnableViewState = false;    
 70
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
 71
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
 72
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
 73
this.DataGrid1.RenderControl(oHtmlTextWriter); 
 74
Response.Write(oStringWriter.ToString());
 75
Response.End();
 76
}
 77

 78
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 79
override protected void OnInit(EventArgs e)
 80
{
 81
//
 82
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 83
//
 84
InitializeComponent();
 85
base.OnInit(e);
 86
}
 87

 88
/**//// <summary>
 89
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
 90
/// 此方法的内容。
 91
/// </summary>
 92
private void InitializeComponent()
 93
{    
 94
this.Button1.Click += new System.EventHandler(this.Button1_Click);
 95
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
 96
this.Load += new System.EventHandler(this.Page_Load);
 97

 98
}
 99
#endregion
100

101
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
102
{
103
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
104
{
105
e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
106
e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");
107
}
108
}
109
}
110
}
111

java excel导出不适用科学计数法 oracle 导出excel总是科学计数法_System_03