2014-05-13 1 views
-1

클릭하면 Excel 파일이 생성되는 아래 코드가 있습니다. 코드가 잘 실행되어 색상의 형식이 정상입니다. 그리드보기에는 106638660952840000 값으로 1.06639E + 17로 표시되는 긴 숫자 "106638660952840428"과 함께 Excel에 표시되지 않는 선행 0이있는 데이터가 있으므로 마지막 네 자리도 잃어 버립니다. 나는 같은 결과로 모든 것을 내보내는 몇 가지 방법을 시도했다. 개인적으로 셀을 포맷 할 수있는 기회를 얻으려면이 방법을 사용하고 있습니다. 질문은 동적으로 형식을 변경하는 방법을 알고 있으므로 이러한 문제가 발생하지 않습니다. 추가 할 또 다른 사항은 웹 브라우저의 격자보기가 데이터를 올바르게 표시한다는 것입니다.원래 형식으로 Excel보기로 변환하는 gridview

protected void LinkButton6_Click(object sender, EventArgs e) 
    { 
     Iframe1.Attributes.Add("src", "blank.aspx"); 

     if (CheckBox5.Checked || CheckBox6.Checked) 
     { 
      dt = (DataTable)Cache["dtable"]; 
      Response.Clear(); 
      Response.Buffer = true; 
      Response.AddHeader("content-disposition", "attachment;filename=BemisInventory.xls"); 
      Response.Charset = ""; 
      Response.ContentType = "application/vnd.ms-excel"; 
      using (StringWriter sw = new StringWriter()) 
      { 
       HtmlTextWriter hw = new HtmlTextWriter(sw); 

       //To Export all pages 
       GridView1.AllowPaging = false; 
       GridView1.DataSource = dt; 
       //GridView1.PageIndex = 0; 
       GridView1.DataBind(); 

       GridView1.HeaderRow.BackColor = Color.White; 
       foreach (TableCell cell in GridView1.HeaderRow.Cells) 
       { 
        cell.BackColor = Color.White;//GridView1.HeaderStyle.BackColor; 
       } 
       foreach (GridViewRow row in GridView1.Rows) 
       { 
        row.BackColor = Color.White; 
        foreach (TableCell cell in row.Cells) 
        { 
         if (row.RowIndex % 2 == 0) 
         { 
          cell.BackColor = Color.White; 
         } 
         else 
         { 
          cell.BackColor = Color.White; //GridView1.RowStyle.BackColor; 
         } 
         cell.CssClass = "textmode"; 
        } 
       } 

       GridView1.RenderControl(hw); 

       //style to format numbers to string 
       string style = @"<style> .textmode { } </style>"; 
       Response.Write(style); 
       Response.Output.Write(sw.ToString()); 
       Response.Flush(); 
       Response.End(); 
      } 

     }    
    } 
+0

문자열 스타일 = @ "" "; 경고가 나오지만 모든 데이터가 있습니다. – user3491110

답변

-1

문자열 스타일 = @ ".textmode {mso-number-format : \ @;}"; 경고가 나오지만 모든 데이터가 있습니다.

관련 문제