从gridview导出到excel并没有显示长数字

时间:2014-02-25 14:48:10

标签: excel gridview export

您好我正在尝试从gridview导出我的数据以擅长问题是我有一个nvarchar列具有以下条形码: 00373228210001695726 并且在导出后的excel中它看起来像这样 3,73228E + 17

我的查询如下:

   DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection("Data Source=AP;Initial Catalog=MGW;User Id=sa;Password=GaUbdFO2;App=EntityFramework;");
            try
            {
                connection.Open();
                string sqlStatement = " ";
                sqlStatement += " SELECT UserName AS UserID, Site, AddressID, Parameters, Content AS Barcode , TriggerDate, ReceivedDate, Action, DeviceID, Longitude, Latitude, CASE WHEN Latitude != '' THEN 'View Map Location' Else 'No Location' END AS LatitudeMSG FROM RequestWithLocation WHERE";
                sqlStatement += " Content = @Content ";
                if (!string.IsNullOrWhiteSpace(TextBoxStart.Text) && !string.IsNullOrWhiteSpace(TextBoxEnd.Text))
                {
                    sqlStatement += " AND TriggerDate >= @TriggerDateStart AND TriggerDate <= @TriggerDateEnd ";
                }
                sqlStatement += " ORDER BY TriggerDate DESC";
                using (SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection))
                {
                    sqlCmd.Parameters.AddWithValue("@Content", TextBoxSearch.Text);
                    if (!string.IsNullOrWhiteSpace(TextBoxStart.Text) && !string.IsNullOrWhiteSpace(TextBoxEnd.Text))
                    {
                        sqlCmd.Parameters.AddWithValue("@TriggerDateStart", Convert.ToDateTime(TextBoxStart.Text));
                        sqlCmd.Parameters.AddWithValue("@TriggerDateEnd", Convert.ToDateTime(TextBoxEnd.Text).AddDays(+1));
                    }
                    using (SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
                    {
                        sqlDa.Fill(dt);

                        if (dt.Rows.Count > 0)
                        {
                            GridDisplayData.DataSource = dt;
                            GridDisplayData.DataBind();
                            lblError.Text = "";
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('No data found');", true);
                        }
                    }
                }
            }
            catch (Exception)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Search timed-out! try again..');", true);
            }

导出功能

   protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachchment; filename=Report_EventScan.xls; IMEX=1;");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridDisplayData.RenderControl(hw);

        //Panel7.RenderControl(hw);
        Response.Write(sw.ToString());
        Response.End();
    }

2 个答案:

答案 0 :(得分:0)

使用Response.Write("'" + sw.ToString());强制在数字前加上带前缀的单引号字符。这迫使Excel采用文本格式。

(我假设+是您所用语言的字符串连接;它可能是&)。

答案 1 :(得分:0)

我解决了我的问题。这就是我做的: 您可以从此处http://epplus.codeplex.com/

下载EPPlus Excel阅读器

然后使用OfficeOpenXml;在项目的顶部和导出按钮的以下代码中,这应该适用于使用数据集的所有人,该数据集保存在 ViewState

protected void btnExportToExcel_Click(object sender, EventArgs e)
  {
     DataSet dt = new DataSet();
     dt = (DataSet)ViewState["QueryTable"];
     MemoryStream ms = new MemoryStream();
     int i = 1;
     using (ExcelPackage package = new ExcelPackage(ms))
      {
       foreach (DataTable table in dt.Tables)
      {
       ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("My Excel Report " + i++);
       worksheet.Cells["A1"].LoadFromDataTable(table, true);
            }
       Response.Clear();
       package.SaveAs(Response.OutputStream);
       Response.AddHeader("content-disposition", "attachchment; filename= test.xls");
       Response.Charset = "";
       Response.ContentType = "application/vnd.xls";
       Response.End();
        }
     }