#region 版权所有 杨立伟 Benny //************************************ // Created Time 2008-07-12 // Created By Benny //************************************ #endregion using System; using System.Collections; using System.Web; using System.Data; using System.Text; namespace YTSoft.Common.Office { /// /// Excel工作本 /// public class ExcelWorkbook { private string filename=Guid.NewGuid().ToString().Replace("-",""); /// /// 导出EXCEL文件名 /// public string FileName { set { filename = HttpUtility.UrlEncode(value, System.Text.Encoding.UTF8); } } ExcelStyleCollection styles = new ExcelStyleCollection(); /// /// 样式集合 /// public ExcelStyleCollection Styles { set { styles = value; } } WorksheetCollection worksheets = new WorksheetCollection(); /// /// 表单集合 /// public WorksheetCollection WorkSheets { get { return worksheets; } set { worksheets = value; } } public void OutPut() { HttpResponse Response = HttpContext.Current.Response; Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename="+filename+".xls"); Response.ContentEncoding = Encoding.GetEncoding("GB2312"); Response.ContentType = "application/ms-excel"; FormatExcel(Response); Response.End(); } private void FormatExcel(HttpResponse Response) { WriteBegininfo(Response); WriteDocumentProperties(Response); WriteOfficeDocumentSettings(Response); WriteExcelWorkbook(Response); WriteStyles(Response, styles); WriteWorkSheets(Response, worksheets); Response.Write(""); } private void WriteWorkSheets(HttpResponse Response, WorksheetCollection worksheets) { foreach (Worksheet ws in worksheets) { ws.OutPut(); WriteWorksheetOptions(Response); Response.Write(""); } } private void WriteWorksheetOptions(HttpResponse Response) { Response.Write(""); Response.Write("