海纳百川

记录学习中的点点滴滴

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

无论在做web还是在写winform程序是老是在导出excel数据是遇到科学计数法问题,如果字符太长(如身份证号)在导出的excel 文件中就会出现长字符串的科学计数法表示,反复导数据是就会出现错误 。
 

      我解决的办法是在到处是或者存储将要导出时,每条记录字符串形式处理

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

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

 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:@");
        }
          

    }

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

 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);
    }

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

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

 我写得代码主要部分如下

  #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

posted on 2010-03-30 10:44  Avatar  阅读(7079)  评论(1编辑  收藏  举报