匯出excel檔案 解決科學計數法問題

來源:互聯網
上載者:User

無論在做web還是在寫winform程式是老是在匯出excel資料是遇到科學計數法問題,如果字元太長(如社會安全號碼)在匯出的excel 檔案中就會出現長字串的科學計數法表示,反覆導資料是就會出現錯誤 。

      我解決的辦法是在到處是或者儲存將要匯出時,每條記錄字串形式處理   在asp.net 中 我一般都是將要匯出的資料放到gridview網格裡,首先對網格邦定資料時 字串形式處理,然後再用普通的形式匯出excel就把問題解決了。  My Code非常簡單:在邦定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
   每條記錄都進行處理 ,如果資料量很多的話應該會影響到速度,一定還有很多更好的方法,一起學習提高。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.