在mvc4中,出口贬值导致零损失

时间:2013-12-16 12:59:52

标签: asp.net-mvc excel asp.net-mvc-4

我正在研究mvc4项目。我遇到了导出到excel代码的问题。我的员工ID是varchar字段......并且有前导零即:0289707,2909878等。

所以当我将数据导出到excel时,它会失去前导零点。 那么我如何导出数据呢?

控制器代码如下:

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

1 个答案:

答案 0 :(得分: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