Ê×Ò³ Ê«´Ê ×Öµä °å±¨ ¾ä×Ó ÃûÑÔ ÓÑ´ð ÀøÖ¾ ѧУ ÍøÕ¾µØÍ¼
µ±Ç°Î»ÖÃ: Ê×Ò³ > ½Ì³ÌƵµÀ > ÍøÕ¾¿ª·¢ > asp.net >

µ¼³öµ½ExcelÂÒÂëµÄÓйØÎÊÌâ

2012-05-21 
µ¼³öµ½ExcelÂÒÂëµÄÎÊÌâ.»·¾³: Windows 2003 ServerÓ¢Îİæ,Sql2005Ó¢Îİæ, .Net 1.0(C#)ÎÊÌâ: ½«Êý¾Ý¿âÀïµÄ

µ¼³öµ½ExcelÂÒÂëµÄÎÊÌâ.
»·¾³: Windows 2003 ServerÓ¢Îİæ,Sql2005Ó¢Îİæ, .Net 1.0(C#)

ÎÊÌâ: ½«Êý¾Ý¿âÀïµÄÊý¾Ýµ¼³öµ½ExcelÖÐʱ,·ÇÓ¢ÎÄ×Ö·û³öÏÖÂÒÂë. ÔÚÊý¾Ý¿âÖÐÏÔʾÕý³£.

ÎÒÃǵÄϵͳÓкܶàÖÖÓïÑÔ,ÈçºÎ²ÅÄܽ«·ÇÓ¢ÎÄ×Ö·ûµ¼³öµ½ExcelÖжø²»³öÏÖÂÒÂë?

²¿·Ö´úÂëÈçÏÂ:

C# code
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";Response.Clear();System.Web.HttpContext.Current.Response.Buffer = true;System.Web.HttpContext.Current.Response.AddHeader( "Content-Disposition", "attachment;filename="+filename);System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";Response.ContentEncoding = System.Text.Encoding.UTF8;//byte[] array1 = new byte[] {0xFF, 0xFE };byte[] array1 = new byte[] {0xEF, 0xBF, 0xBE}; Response.BinaryWrite(array1);System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content='text/html; charset=utf-8'>");String s;s = "<P>¤ª¤Ï¤è¤¦¤´¤¶¤¤¤Þ¤¹?????§©§Õ§â§Ñ§Ó§ã§ä§Ó§å§ä§Ö</P>";//            byte[] converted = System.Text.Encoding.GetEncoding("utf-8").GetBytes(s.ToString()); //            Response.BinaryWrite(converted); Response.Write(s);Response.Flush();Response.End();


Çó¸ßÊÖ°ïÖú.лл.

[½â¾ö°ì·¨]

µ«ÊÇÒ»ÊÔ,±ã³öÏÖÁËÂÒÂë,½â¾ö·½°¸ÈçÏÂ
public static void Export(System.Web.UI.Page page, System.Web.UI.Control dg, string fileName, string typeName)
{
System.Web.HttpResponse httpResponse =page.Response;
httpResponse.AppendHeader
("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
httpResponse.ContentType = typeName;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
dg.RenderControl(hw);
string filePath = page.Server.MapPath("..") + fileName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();

DownFile(httpResponse, fileName, filePath);
httpResponse.End();
}
public static bool DownFile(System.Web.HttpResponse Response, string fileName, string fullPath)
{
try
{
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" +
HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
System.IO.FileStream fs = System.IO.File.OpenRead(fullPath);
long fLen = fs.Length;
int size = 102400;//ÿ100KͬʱÏÂÔØÊý¾Ý
byte[] readData = new byte[size];//Ö¸¶¨»º³åÇøµÄ´óС
if (size > fLen) size = Convert.ToInt32(fLen);
long fPos = 0;
bool isEnd = false;
while (!isEnd)
{
if ((fPos + size) > fLen)
{
size = Convert.ToInt32(fLen - fPos);
readData = new byte[size];
isEnd = true;
}
fs.Read(readData, 0, size);//¶ÁÈëÒ»¸öѹËõ¿é
Response.BinaryWrite(readData);
fPos += size;
}
fs.Close();
System.IO.File.Delete(fullPath);
return true;
}
catch
{
return false;
}
}
Ôõôµ÷ÓÃ?? Æäʵ¾ÍÊÇÔõôµ÷ÓÃExportÕâ¸öº¯Êý°ÕÁË.



ÒÔÉÏ·½·¨¿ÉÒÔºÜÓÐЧµØ½â¾öµ¼³öµÄÂÒÂëÎÊÌâ
[½â¾ö°ì·¨]

C# code
Response.Charset = "UTF-8";  Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
[½â¾ö°ì·¨]
mark
±àÂëµÄÎÊÌâÊDZȽÏÂé·³.
¿´ÄãµÄ´úÂë,ÊÇÏëÏò¿Í»§¶Ëд¸öxlsµÄÎļþ,µ«ÊÇÄãдµÄÄÚÈÝÊÇ "<P>¤ª¤Ï¤è¤¦¤´¤¶¤¤¤Þ¤¹?????§©§Õ§â§Ñ§Ó§ã§ä§Ó§å§ä§Ö</P>" 
Ò²ÐíÊǿͻ§¶Ë°ÑÕâ¶ÎÎÄ×Öµ±×÷xlsµÄÎļþÀ´´¦ÀíÁË,
Èç¹û²»·ûºÏxlsµÄÎļþ¸ñʽ,µ±×÷xlsÀ´ÏÔʾºÜ¿ÉÄÜÊÇÂÒÂë. 
±ÈÈçÄãÓüÇʱ¾´ò¿ªÒ»¸öexe Îļþ,¾ÍÓÐÕâÑùµÄÇé¿ö.

¶ÔxlsµÄÎļþ¸ñʽ²»Á˽â,²Â²âÒ»ÏÂ,ÒªÊDz»¶Ô,ιÖ.
[½â¾ö°ì·¨]
Äã²Î¿¼Ò»ÏÂÏÂÃæµÄ´úÂë°É£¡ÎÒÒ»Ö±ÓõÄÊÇÕâÖÖ. 

DataTable dt = ´ÓÊý¾Ý¿âÖеõ½Êý¾Ý

System.Web.HttpContext curContext = System.Web.HttpContext.Current;
if (dt != null)
{
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//¸Ä³Éutf-8
curContext.Response.ContentType = "application/excel";
curContext.Response.ClearContent();
curContext.Response.AddHeader("content-disposition", "attachment; filename=filename.xls");

//µ¼³öexcelÎļþ
StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
GridView dg = new GridView();
dg.DataSource = dt.DefaultView;
dg.DataBind();
dg.RenderControl(htw);
dg.Dispose();
curContext.Response.Write(sw.ToString());

curContext.Response.End();
}
[½â¾ö°ì·¨]
C# code
 curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//ÆðµÄ×÷Óà                curContext.Response.ContentType = "application/excel";                 curContext.Response.Charset = "utf-8";//ÆðµÄ×÷Óà                curContext.Response.ClearContent(); 

ÈȵãÅÅÐÐ