2013-12-16 1 views
0

Я работаю над проектом mvc4. У меня проблема с экспортом в excel code.I есть идентификатор сотрудника, который является полем varchar ... и имеет начальный ноль, то есть: 0289707,2909878 и т. Д. .Экспорт в excel убытки, ведущие ноль в mvc4

Так что, когда я экспортирую данные, чтобы преуспеть, он теряет ведущий ноль. Так как я могу экспортировать данные так, как есть?

код контроллера, как показано ниже:

public ActionResult ExportToExcel(string strStartDate, string strEndDate) 
     { 
      try 
      { 

       GridView gridView = new GridView(); 
       Response.ClearContent(); 
       Response.Buffer = true; 
       Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "EmployeeReport " + Helper.GetBrazilTime(DateTime.UtcNow).ToString() + ".xls")); 
       Response.ContentType = "application/ms-excel"; 

       StringWriter stringWriter = new StringWriter(); 
       HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); 

       gridView.AllowPaging = false; 

       DateTime startDate = Convert.ToDateTime(strStartDate); 
       DateTime endDate = Convert.ToDateTime(strEndDate); 

       if (Helper.CurrentCulture == "pt-BR") 
       { 
        startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0, 0, DateTimeKind.Utc); 
        endDate = new DateTime(endDate.Year, startDate.Month, endDate.Day, 0, 0, 0, DateTimeKind.Utc); 
       } 

       gridView.DataSource = ReportExecutor.GetEmployeeReportExportData(startDate, endDate); 
       gridView.DataBind(); 

       //This will change the header background color 
       gridView.HeaderRow.Style.Add("background-color", "#FFFFFF"); // 

       //This will apply style to gridview header cells 
       for (int index = 0; index < gridView.HeaderRow.Cells.Count; index++) 
       { 
        gridView.HeaderRow.Cells[index].Style.Add("background-color", "#778899"); //Light Slate Gray 
        gridView.HeaderRow.Cells[index].Style.Add("foreground-color", "#ffffff"); // White 
       } 

       gridView.HeaderRow.Cells[0].Text = @Resources.Resource.ShopName; 
       gridView.HeaderRow.Cells[1].Text = @Resources.Resource.MachineName; 
       gridView.HeaderRow.Cells[2].Text = @Resources.Resource.ProjectIDName; 
       gridView.HeaderRow.Cells[3].Text = @Resources.Resource.BaseActivity; 
       gridView.HeaderRow.Cells[4].Text = @Resources.Resource.EmployeeID; 
       gridView.HeaderRow.Cells[5].Text = @Resources.Resource.EmployeeName; 
       gridView.HeaderRow.Cells[6].Text = @Resources.Resource.RunTime; 
       gridView.HeaderRow.Cells[7].Text = @Resources.Resource.SetUp; 
       gridView.HeaderRow.Cells[8].Text = @Resources.Resource.TearDown; 
       gridView.HeaderRow.Cells[9].Text = @Resources.Resource.Work; 
       gridView.HeaderRow.Cells[10].Text = @Resources.Resource.Rework; 
       gridView.HeaderRow.Cells[11].Text = @Resources.Resource.LunchHours; 
       gridView.HeaderRow.Cells[12].Text = @Resources.Resource.MaintenanceHours; 
       gridView.HeaderRow.Cells[13].Text = @Resources.Resource.QualityProblemHours; 
       gridView.HeaderRow.Cells[14].Text = @Resources.Resource.LOMHours; 
       gridView.HeaderRow.Cells[15].Text = @Resources.Resource.UDCIdle; 
       gridView.HeaderRow.Cells[16].Text = @Resources.Resource.UDCOthers; 
       gridView.HeaderRow.Cells[17].Text = @Resources.Resource.ActualShiftHours; 
       gridView.HeaderRow.Cells[18].Text = @Resources.Resource.Overtime; 

       int index2 = 1; 
       //This will apply style to alternate rows 
       foreach (GridViewRow gridViewRow in gridView.Rows) 
       { 
        //gridViewRow.Attributes.Add("class", "textmode"); 
        gridViewRow.BackColor = Color.White; 
        if (index2 <= gridView.Rows.Count) 
        { 
         if (index2 % 2 != 0) 
         { 
          for (int index3 = 0; index3 < gridViewRow.Cells.Count; index3++) 
          { 
           gridViewRow.Cells[index3].Style.Add("background-color", "#e6e6fa");// Lavender 
           //gridViewRow.Cells[index3].Style.Add("class", "textmode");// Apply text style to all rows 
          } 
         } 
        } 
        index2++; 
       } 

       gridView.RenderControl(htmlTextWriter); 

       Response.Write(stringWriter.ToString()); 
       Response.End(); 

      } 
      catch 
      { 

      } 
      return null; 
     } 
+0

Скрытое значение в строку. Это приведет к увеличению стоимости в кавычках, которые excel будет интерпретировать как текст. – heymega

+0

спасибо за предложение, но как это сделать? не могли бы вы дать мне код ссылки? – Mahajan344

+0

Сначала отлаживайте свой код и убедитесь, что @ Resources.Resource.EmployeeID имеет ведущие нули – heymega

ответ

0
For i As Integer = 0 To GridView1.Rows.Count - 1 
    //Apply text style to each Row.cell 
    //The attribute must be "text" not "textmode" and it must be applied to the cells 
    For j As Integer = 0 To GridView1.Rows(i).Cells.Count - 1 
     GridView1.Rows(i).Cells(j).Attributes.Add("class", "text"); 
    Next 
Next 
+0

. В вашем ответе должно быть объяснение вашего кода и описание того, как он решает проблему. – AbcAeffchen

Смежные вопросы