Exemple #1
0
        protected byte[] Export <T>(IList <T> DataSource, MyFileType fileType)
        {
            if ((fileType != MyFileType.EXCEL) && (fileType != MyFileType.EXCEL2003))
            {
                throw new ExportException("不能导出类型" + fileType.ToString());
            }
            int columnStartIndex = this.ColumnStartIndex;

            foreach (string str in this.Head)
            {
                this.defineLocation.Add(str, columnStartIndex);
                columnStartIndex++;
            }
            ExportHelper.headStyles = new Dictionary <string, ICellStyle>();
            List <string>         sheetName   = new List <string>();
            Dictionary <int, int> columnWidth = new Dictionary <int, int>();
            BeginExportEventArgs  e           = new BeginExportEventArgs(sheetName, this.defineLocation, columnWidth);

            this.BeginExport(e);
            this.CurWorkBook        = ExportCoreHelper.GetVersionWorkbook(fileType);
            ExportHelper.CellStyles = new Dictionary <StyleXls, ICellStyle>();

            ExportHelper.CellStyles.Add(StyleXls.默认, ExportCoreHelper.Getcellstyle(CurWorkBook, StyleXls.默认));
            ExportHelper.CellStyles.Add(StyleXls.时间, ExportCoreHelper.Getcellstyle(CurWorkBook, StyleXls.时间));
            ExportHelper.CellStyles.Add(StyleXls.数字, ExportCoreHelper.Getcellstyle(CurWorkBook, StyleXls.数字));

            if (this.RowStartIndex == 0)
            {
                this.RowStartIndex = this.HeadRowIndex + 1;
            }
            if (sheetName.Count == 0)
            {
                sheetName.Add("sheet1");
            }
            using (MemoryStream stream = new MemoryStream())
            {
                foreach (string str2 in sheetName)
                {
                    ISheet sheet = this.CurWorkBook.CreateSheet(str2);
                    foreach (string str3 in this.defineLocation.Keys)
                    {
                        int key = this.defineLocation[str3];
                        if (columnWidth.ContainsKey(key))
                        {
                            sheet.SetColumnWidth(key, columnWidth[key] * 50);
                        }
                        else
                        {
                            int num3 = StringHelper.trueLength(str3);
                            if (num3 > 14)
                            {
                                sheet.SetColumnWidth(key, 330 * num3);
                            }
                            else
                            {
                                sheet.SetColumnWidth(key, 0x1130);
                            }
                        }
                    }
                    this.SetListHead(sheet);
                    this.SetListData(sheet);
                    //AutoSizeColumns(sheet);
                }
                this.CurWorkBook.Write(stream);
                return(stream.ToArray());
            }
        }
Exemple #2
0
 protected virtual void BeginExport(BeginExportEventArgs e)
 {
     e.Raise <BeginExportEventArgs>(this, ref this.BgExport);
 }