»·¾³: 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Õâ¸öº¯Êý°ÕÁË.