1.组件原理
excel的数据存储是以xml格式存储的,所以导出Excel文件可以通过生成XML来实现。当然XML必须符合一定的格式要求。
2.组件实现
(1)新建类库文件“MyExcel”
(2)添加类型“WorkBook”,这里指Excel的工作表。
1 namespace MyExcel 2 { 3 public class WorkBook 4 { 5 private StringBuilder excelstr; 6 private List<WorkSheet> sheets; 7 public WorkBook() 8 { 9 this.excelstr = new StringBuilder(); 10 this.sheets = new List<WorkSheet>(); 11 } 12 public WorkSheet CreateSheet(string title) 13 { 14 if (sheets == null) 15 { 16 throw new Exception("只有先构建WorkBook对象后才能构建WorkSheet对象!"); 17 } 18 foreach (WorkSheet sht in sheets) 19 { 20 if (sht.Title.ToLower() == title.ToLower()) 21 { 22 throw new Exception("已经有名称为:" + title + " 的Sheet"); 23 } 24 } 25 WorkSheet wsheeet = new WorkSheet(title); 26 this.sheets.Add(wsheeet); 27 return wsheeet; 28 } 29 private string WriteWorkBook() 30 { 31 if (sheets == null || sheets.Count < 1) 32 { 33 throw new Exception("只有先构建WorkBook对象后才能构建行对象!"); 34 } 35 excelstr.Append("<?xml version=\"1.0\"?>\r\n"); 36 excelstr.Append("<?mso-application progid=\"Excel.Sheet\"?>\r\n"); 37 excelstr.Append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n"); 38 excelstr.Append("xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n"); 39 excelstr.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\n"); 40 excelstr.Append("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n"); 41 excelstr.Append("xmlns:html=\"http://www.w3.org/TR/REC-html40\">\r\n"); 42 excelstr.Append("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">\r\n"); 43 excelstr.Append("<Created>2006-09-16T00:00:00Z</Created>\r\n"); 44 excelstr.Append("<LastSaved>2015-07-07T05:50:29Z</LastSaved>\r\n"); 45 excelstr.Append("<Version>14.00</Version>\r\n"); 46 excelstr.Append("</DocumentProperties>\r\n"); 47 excelstr.Append("<OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">\r\n"); 48 excelstr.Append("<AllowPNG/>\r\n"); 49 excelstr.Append("<RemovePersonalInformation/>\r\n"); 50 excelstr.Append("</OfficeDocumentSettings>\r\n"); 51 excelstr.Append("<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\r\n"); 52 excelstr.Append("<WindowHeight>8010</WindowHeight>\r\n"); 53 excelstr.Append("<WindowWidth>14805</WindowWidth>\r\n"); 54 excelstr.Append("<WindowTopX>240</WindowTopX>\r\n"); 55 excelstr.Append("<WindowTopY>105</WindowTopY>\r\n"); 56 excelstr.Append("<ProtectStructure>False</ProtectStructure>\r\n"); 57 excelstr.Append("<ProtectWindows>False</ProtectWindows>\r\n"); 58 excelstr.Append("</ExcelWorkbook>\r\n"); 59 excelstr.Append("<Styles>\r\n"); 60 excelstr.Append("<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n"); 61 excelstr.Append("<Alignment ss:Vertical=\"Bottom\"/>\r\n"); 62 excelstr.Append("<Borders/>\r\n"); 63 excelstr.Append("<Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"11\" ss:Color=\"#000000\"/>\r\n"); 64 excelstr.Append("<Interior/>\r\n"); 65 excelstr.Append("<NumberFormat/>\r\n"); 66 excelstr.Append("<Protection/>\r\n"); 67 excelstr.Append("</Style>\r\n"); 68 excelstr.Append("<Style ss:ID=\"s64\">\r\n"); 69 excelstr.Append("<Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>\r\n"); 70 excelstr.Append("<Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"11\" ss:Color=\"#000000\"\r\n"); 71 excelstr.Append("ss:Bold=\"1\"/>\r\n"); 72 excelstr.Append("</Style>\r\n"); 73 excelstr.Append("</Styles>\r\n"); 74 foreach (WorkSheet item in sheets) 75 { 76 excelstr.Append(item.WriteSheet()); 77 } 78 excelstr.Append("</Workbook>\r\n"); 79 return excelstr.ToString(); 80 } 81 /// <summary> 82 /// Write Data To Stream. 83 /// </summary> 84 /// <param name="ms"></param> 85 public void WriteStream(Stream ms) 86 { 87 try 88 { 89 string msg = WriteWorkBook(); 90 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(msg); 91 ms.Write(buffer, 0, buffer.Length); 92 93 } 94 catch (Exception) 95 { 96 throw new Exception("写入流错误"); 97 } 98 99 } 100 } 101 }