Gridview i office 2003 ve 2007 destekli türkçe karakter problemine takılmadan aktaran GridviewToX im 
GridviewToX("Siparisler.xls", GridView1,false); şeklinde kullanılır.AllowPaging sayfalama yapıp yapmayacağını belirtir.
uzantıya göre Mime sini de bulur. dosya uzantısı (.xls)veya (.doc) uzantılı olmalıdır.(.xlsx,.docx uzantısı verirseniz office 2003,2007 açamaz)
encodingi Encoding.UTF7 yaparsak türkçe karakter problemi office 2003 için kalkıyor.Fakat bu seferde office 2007 dosyayı açamıyor :=( neyseki sorunu çözdük :=)
nette yer alan birçok gridview to excel kodunun neden türkçe karakter problemi çıkarttığınıda keşfettim.Sebebi windowsun dosyaların varsayılan olarak UTF8 without BOM(bom olmadan) olarak kaydetmesiymiş.Keçileri kaçıranlara duyurulur
Sorunlu dosyayı Notepad++ gibi editörlerle açıp UTF8 olarak kaydedersek problem çözülüyor
yada dosyayı aşağıdaki fonksiyondan geçirmeniz gerekiyor.
public static void FormatDuzelt(string filePath)
{
StreamReader fileStream = new StreamReader(filePath);
string fileContent = fileStream.ReadToEnd();
fileStream.Close();
StreamWriter UTF8Writer = new StreamWriter(filePath.Replace(".doc", "-utf8.doc"), false, new UTF8Encoding(true));
UTF8Writer.Write(fileContent);
UTF8Writer.Close();
}
ile dosya formatını UTF8Encoding(true) ile UTF8 without BOM yerine UTF8 e dönüştürüyoruz.
public static void GridviewToX(string filename, GridView gv, bool AllowPaging)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
switch (System.IO.Path.GetExtension(filename).ToLower())
{
case ".doc":
HttpContext.Current.Response.ContentType = GetFileMIME(".doc");
break;
case ".xls":
HttpContext.Current.Response.ContentType = GetFileMIME(".xls");
break;
default:
break;
}
// HttpContext.Current.Response.ContentType = "text/plain; charset=utf-8";
HttpContext.Current.Response.Charset = "windows-1254";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gv.AllowPaging = AllowPaging;
gv.GridLines = GridLines.Both;
gv.DataBind();
//Change the Header Row back to white color
gv.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
for (int i = 0; i < gv.HeaderRow.Controls.Count; i++)
{
gv.HeaderRow.Cells[i].Style.Add("background-color", "green");
}
for (int i = 0; i < gv.Rows.Count; i++)
{
GridViewRow row = gv.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
for (int ix = 0; ix < row.Controls.Count; ix++)
{
row.Cells[ix].Style.Add("background-color", "#C2D69B");
}
}
}
gv.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Output.Write(sw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
public static string GetFileMIME(string fileextension) // uzantinın mimesini dönderir.uzanti .jpg vs şeklinde sorulmalıdır. using Microsoft.Win32;
{
//set the default content-type
const string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey, fileextkey;
string filecontenttype;
//the file extension to lookup
try
{
//look in HKCR
regkey = Registry.ClassesRoot;
//look for extension
fileextkey = regkey.OpenSubKey(fileextension);
//retrieve Content Type value
filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
//cleanup
fileextkey = null;
regkey = null;
}
catch
{
filecontenttype = DEFAULT_CONTENT_TYPE;
}
return filecontenttype;
}