Asp.net 将DataGrid分多个Sheet导出Excel
2012-02-27 11:27
169 查看
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.IO; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; /// <summary> ///ExcelUtil 的摘要说明 /// </summary> public class ExcelUtil { public ExcelUtil() { // //TODO: 在此处添加构造函数逻辑 // } string _Author; string _Company; StringBuilder sbBody = new StringBuilder(); StringBuilder sbSheet = new StringBuilder(); public ExcelUtil(string Author, string Company) { _Author = Author; _Company = Company; sbBody.AppendFormat( "MIME-Version: 1.0\r\n" + "X-Document-Type: Workbook\r\n" + "Content-Type: multipart/related; boundary=\"-=BOUNDARY_EXCEL\"\r\n\r\n" + "---=BOUNDARY_EXCEL\r\n" + "Content-Type: text/html; charset=\"big5\"\r\n\r\n" + "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n" + "xmlns:x=\"urn:schemas-microsoft-com:office:excel\">\r\n\r\n" + "<head>\r\n" + "<xml>\r\n" + "<o:DocumentProperties>\r\n" + "<o:Author>...{0}</o:Author>\r\n" + "<o:LastAuthor>...{0}</o:LastAuthor>\r\n" + "<o:Created>...{1}</o:Created>\r\n" + "<o:LastSaved>...{1}</o:LastSaved>\r\n" + "<o:Company>...{2}</o:Company>\r\n" + "<o:Version>11.5606</o:Version>\r\n" + "</o:DocumentProperties>\r\n" + "</xml>\r\n" + "<xml>\r\n" + "<x:ExcelWorkbook>\r\n" + "<x:ExcelWorksheets>\r\n" , _Author , DateTime.Now.ToString() , _Company); } private string ExportExcel() { StringBuilder sb = new StringBuilder(sbBody.ToString()); sb.Append("</x:ExcelWorksheets>\r\n" + "</x:ExcelWorkbook>\r\n" + "</xml>\r\n" + "</head>\r\n" + "</html>\r\n\r\n"); sb.Append(sbSheet.ToString()); sb.Append("---=BOUNDARY_EXCEL--"); return sb.ToString(); } public void AddGrid(DataGrid grid, string sheetName) { string gid = Guid.NewGuid().ToString(); sbBody.AppendFormat("<x:ExcelWorksheet>\r\n" + "<x:Name>...{0}</x:Name>\r\n" + "<x:WorksheetSource HRef=\"cid:...{1}\"/>\r\n" + "</x:ExcelWorksheet>\r\n" , sheetName.Replace(":", "").Replace("\\", "").Replace("/", "").Replace ("?", "").Replace("*", "").Replace("[", "").Replace("]", "").Trim() , gid); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); grid.RenderControl(htw); sbSheet.AppendFormat("---=BOUNDARY_EXCEL\r\n" + "Content-ID: ...{0}\r\n" + "Content-Type: text/html; charset=\"big5\"\r\n\r\n" + "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n" + "xmlns:x=\"urn:schemas-microsoft-com:office:excel\">\r\n\r\n" + "<head>\r\n" + "<xml>\r\n" + "<x:WorksheetOptions>\r\n" + "<x:ProtectContents>False</x:ProtectContents>\r\n" + "<x:ProtectObjects>False</x:ProtectObjects>\r\n" + "<x:ProtectScenarios>False</x:ProtectScenarios>\r\n" + "</x:WorksheetOptions>\r\n" + "</xml>\r\n" + "</head>\r\n" + "<body>\r\n" , gid); sbSheet.Append(sw.ToString()); sbSheet.Append("</body>\r\n" + "</html>\r\n\r\n"); sw.Close(); htw.Close(); } public void Export(Page page, string FileName) { page.Response.Clear(); page.Response.Buffer = true; page.Response.Charset = ""; page.Response.AddHeader("content-disposition", string.Format ("attachment;filename=...{0}.xls", FileName)); page.Response.ContentEncoding = Encoding.GetEncoding("big5"); page.Response.ContentType = "application/vnd.ms-excel"; page.Response.Write(ExportExcel()); page.Response.End(); } public void Clear() { sbBody.Remove(0, sbBody.Length); sbSheet.Remove(0, sbBody.Length); } }
调用方法:
ExcelUtil eu=new ExcelUtil("Jeff_Yeh","隨手記");
eu.AddGrid(givw, "TestA");
eu.AddGrid(givw, "TestB");
eu.Export(this,"Jeff");
相关文章推荐
- Asp.net将DataTable分多个Sheet导出到Excel
- Asp.net将DataTable分多个Sheet导出到Excel
- Asp.net中把DataTable或DataGrid导出为Excel
- Asp.net中把DataTable或DataGrid导出为Excel
- Asp.net中把DataTable或DataGrid导出为Excel
- asp.net中将DataGrid数据导出到excel或word文件中
- asp.net中将DataGrid数据导出到excel或word文件中
- asp.net导出Excel生成多个Sheet
- ASP.NET 中 DataGrid 导出到 Excel 的封装实现
- ASP.NET 中 DataGrid 导出到 Excel 的封装实现
- asp.net中将DataGrid的内容导出为excel文件
- Asp.net中把DataTable或DataGrid导出为Excel
- Asp.net 依据DataGrid与DataTable不在服务器保存导出EXCEL
- ASP.NET 中 DataGrid 导出到 Excel 的封装实现 (转)
- ASP.net 把 DataGrid 数据导出到 Excel .
- asp.net中将DataGrid数据导出到excel或word文件中
- ASP.NET将数据导出到一个EXCEL文件的多个SHEET中
- ASP.net 把 DataGrid 数据导出到 Excel .
- ASP.NET用XML的方式导出到excel多sheet的实现方式
- ASP.NET 分Sheet导出EXCEL 2003