62,046
社区成员
发帖
与我相关
我的任务
分享
public class Exportxls
{
public Exportxls(){ }
public static DataTable XLSDataTable = null;
public static void ExportToxls(string[] hiddenTexts,string[] headerTexts)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +
HttpUtility.UrlEncode(DateTime.Now.Ticks.ToString() + ".xls", System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = "application/ms-excel";
//page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
PrintGridViewSetting(hw, hiddenTexts, headerTexts); //设置GridView结构,然后进行打印操作
HttpContext.Current.Response.Output.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
//打印Excel的结构设置
public static void PrintGridViewSetting(HtmlTextWriter hw, string[] hiddenTexts, string[] headerTexts)
{
DataTable dtTemp = Exportxls.XLSDataTable.Copy();
//---------------------隐藏字段设置-------------
//移除不需要显示的字段
foreach (string columnName in hiddenTexts)
{
dtTemp.Columns.Remove(columnName);
}
GridView GV = new GridView(); //一个无分页的GridView
GV.DataSource = dtTemp;
GV.AllowPaging = false;
GV.DataBind();
//---------------------头部标题设置-------------
GV.HeaderRow.Cells.Clear();
foreach (string headerText in headerTexts)
{
TableCell cell = new TableCell();
cell.Text = headerText;
GV.HeaderRow.Cells.Add(cell);
}
//----------------------------------------------
GV.RenderControl(hw); //输出结构
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
//BindData()是查询返回DataView
Exportxls.XLSDataTable = BindData().ToTable();
string[] hiddenTexts = new string[]
{
"Id","CustomId","SpecialDate","Anniversary","OfficeTel","LinkNo"
};
string[] headerTexts = new string[]
{
"姓名","称呼","职务","相关客户","部门","电子邮件"
};
ExportToxls( hiddenTexts, headerTexts);
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
namespace webtest
{
public partial class download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string myname = Request["url"];
string FileName = System.IO.Path.GetFileName(myname);
int Size = Convert.ToInt32(new System.IO.FileInfo(myname).Length);
System.IO.Stream fileStream = new System.IO.FileStream(myname, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
byte[] Content = new byte[Size];
int Status = fileStream.Read(Content, 0, Size);
fileStream.Close();
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + ToHexString(FileName));
Response.ContentType = System.IO.Path.GetExtension(FileName);
Response.BinaryWrite(Content);
Response.End();
}
/// <summary>
/// 为字符串中的非英文字符编码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for (int index = 0; index < chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if (needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}
return builder.ToString();
}
/// <summary>
///指定 一个字符是否应该被编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";
if (chr > 127)
return true;
if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
return false;
return true;
}
/// <summary>
/// 为非英文字符串编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}
}
}
FileStream f= new FileStream("", FileMode.Open);
byte[] buffer = new byte[f.Length];
f.Read(buffer, 0, buffer.Length);
f.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("", System.Text.Encoding.UTF8));
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();