none
如何能正確 render datagrid 內的中文字到excel? RRS feed

  • 問題

  •  

    大家好,

     

    我有個function把datagrid render成excel 格左供user即時下載, 一直用得很好. 程式如下:

     

    Code Snippet

    public static void RenderGridToExcelFormat(System.Web.UI.WebControls.DataGrid grid, string saveAsFile)
      {
       // check Excel rows limit
       if (grid.Items.Count + 1 < 65536)
       {
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + saveAsFile + ".xls");
        // Remove the charset from the Content-Type header.
        System.Web.HttpContext.Current.Response.Charset = "";
        //HttpContext.Current.Response.WriteFile("style.txt")
        // Turn off the view state.
        grid.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        // Get the HTML for the control.
        grid.HeaderStyle.ForeColor = System.Drawing.Color.Black;
        grid.HeaderStyle.BackColor = System.Drawing.Color.Red;
        grid.ItemStyle.ForeColor = System.Drawing.Color.Black;
        grid.BorderColor = System.Drawing.Color.White;
        ClearControls(grid);
        grid.RenderControl(hw);
        // Write the HTML back to the browser.
        System.Web.HttpContext.Current.Response.Write(tw.ToString());
        // End the response.
        System.Web.HttpContext.Current.Response.End();
       }
       else
       {
           
        System.Web.HttpContext.Current.Response.Write("Too many rows - Export to Excel not possible");
       }
      }

      public static void ClearControls(System.Web.UI.Control control)
      {
       int i;
       for (i = control.Controls.Count - 1; i >= 0; i += -1)
       {
        ClearControls(control.Controls[i]);
       }
       
       if (control is System.Web.UI.WebControls.Image)
       {
        control.Parent.Controls.Remove(control);
       }
       
       if (!(control is System.Web.UI.WebControls.TableCell))
       {
        if ((control.GetType().GetProperty("SelectedItem") != null))
        {
         System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl();
         control.Parent.Controls.Add(literal);
         try
         {
          literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
         }
         catch
         {
         }
         control.Parent.Controls.Remove(control);
        }
        else
        {
         if ((control.GetType().GetProperty("Text") != null))
         {
          System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl();
          control.Parent.Controls.Add(literal);
          literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
          control.Parent.Controls.Remove(control);
         }
        }
       }
       return;
      }
      //ClearControls

     

     

     

     

    一向都沒有問題, 但當data grid 內有中文字, render 到excel後中文字都變了怪獸字, 請問如何解決?

     

    圖片:

    WEB application screen:

    http://model.chingb.com/temp/render_1.jpg

     

     

     

    After render to excel:

    http://model.chingb.com/temp/render_2.jpg

    2008年4月8日 上午 07:26

所有回覆

  • 你沒有指定字碼,預設會以控制台中地區語系編碼解。

     

    Code Snippet

    System.Web.HttpContext.Current.Response.Charset = "";

     

     

    2008年4月8日 上午 10:05
  • 我試了好幾個組合都不成功, 能明確指示應怎樣寫嗎?

     

     -   System.Web.HttpContext.Current.Response.Charset = "utf-8";

    -    System.Web.HttpContext.Current.Response.Charset = "utf8";

    -    System.Web.HttpContext.Current.Response.Charset = "big5";


    -    System.Web.HttpContext.Current.Response.Charset = ""; 
         System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

    -    System.Web.HttpContext.Current.Response.Charset = "utf8"; 
         System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

    -    System.Web.HttpContext.Current.Response.Charset = "utf-8"; 
         System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

    -    System.Web.HttpContext.Current.Response.Charset = "big5"; 
         System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

     

    Web.config 有這項:

     
                requestEncoding="utf-8"
                responseEncoding="utf-8"
       />
      

     

    但如果我用notepad打開這excel file, 內容是對的:

    Code Snippet
    <table cellspacing="0" cellpadding="3" rules="cols" bordercolor="White" border="1" id="dg1" style="background-color:White;border-color:White;border-width:1px;border-style:None;font-size:8pt;border-collapse:collapse;">
     <tr style="color:Black;background-color:Azure;font-weight:bold;">
      <td>Staff No.</td><td>Name</td><td>&nbsp;</td><td>Company</td><td>Perm/ Contract</td><td>Dept</td><td>Position</td><td>Location</td><td>Day/ Night</td><td>Comm Date</td><td>Record Date</td><td>Rost_Code</td><td>Rost_Desc</td><td>In</td><td>Out</td><td>Late (min.)</td><td>OT</td><td>Leave</td><td>Early Leave (min.)</td><td>&nbsp;</td><td>Endorsed OT</td>
     </tr><tr style="color:Black;background-color:#EEEEEE;">
      <td>038</td><td>Tse Chi San</td><td nowrap="nowrap" style="font-family:MingLiU;">謝智新</td><td>&nbsp;</td><td>&nbsp;</td><td>JH (Day) - Warehouse staff</td><td>Warehouse Assistant</td><td>JH</td><td>D</td><td>2/1/2007</td><td>Apr-05</td><td>WSE-D1</td><td>Warehouse day shift - Mon-Fri</td><td nowrap="nowrap">07:51 AM</td><td nowrap="nowrap">05:10 PM</td><td align="Right">&nbsp;</td><td nowrap="nowrap" align="Right">&nbsp;</td><td>&nbsp;</td><td align="Right">&nbsp;</td><td></FONT>
         Endorse Absense<FONT face="·s²Ó©úÅé">
           Endorse OT
          </td><td>&nbsp;</td>
     </tr><tr style="color:Black;background-color:Gainsboro;">
      <td>109</td><td>Wong Tak Lung</td><td nowrap="nowrap" style="font-family:MingLiU;">黃德龍</td><td>&nbsp;</td><td>&nbsp;</td><td>JH (Day) - Warehouse staff</td><td>Warehouse Assistant</td><td>JH</td><td>D</td><td>2/1/2007</td><td>Apr-05</td><td>&nbsp;</td><td>&nbsp;</td><td nowrap="nowrap">&nbsp;</td><td nowrap="nowrap">&nbsp;</td><td align="Right">&nbsp;</td><td nowrap="nowrap" align="Right">&nbsp;</td><td>&nbsp;</td><td align="Right">&nbsp;</td><td></FONT>
         Endorse Absense<FONT face="·s²Ó©úÅé">
           Endorse OT
          </td><td>&nbsp;</td>
     </tr>tr style="color:Black;background-color:Gainsboro;">
      <td>4183506</td><td>FUNG PAK SHING</td><td nowrap="nowrap" style="font-family:MingLiU;">馮百勝</td><td>&nbsp;</td><td>&nbsp;</td><td>JH (Day) - Warehouse staff</td><td>Warehouse Supervisor</td><td>JH/KG</td><td>D</td><td>1/21/2006</td><td>Apr-05</td><td>&nbsp;</td><td>&nbsp;</td><td nowrap="nowrap">&nbsp;</td><td nowrap="nowrap">&nbsp;</td><td align="Right">&nbsp;</td><td nowrap="nowrap" align="Right">&nbsp;</td><td>&nbsp;</td><td align="Right">&nbsp;</td><td></FONT>
         Endorse Absense<FONT face="·s²Ó©úÅé">
           Endorse OT
          </td><td>&nbsp;</td>
     </tr>
    </table>

     

     

     

    請幫幫忙~ 先謝!

    2008年4月9日 上午 01:50