Ejemplo n.º 1
0
 public void Export(string FileUrl, MyFileType FileType)
 {
     if ((FileType != MyFileType.EXCEL) && (FileType != MyFileType.EXCEL2003))
     {
         throw new ExportException("不能导入类型:" + FileType.ToString());
     }
     FileUrl = ExportHelper.GetMatchUrl(FileUrl, FileType);
     try
     {
         byte[]    buffer;
         IWorkbook versionWorkbook = ExportCoreHelper.GetVersionWorkbook(FileType);
         ExportHelper.CellStyles = new Dictionary <StyleXls, ICellStyle>();
         ExportHelper.CellStyles.Add(StyleXls.时间, ExportCoreHelper.GetDateTimeCellStyle(versionWorkbook));
         using (MemoryStream stream = new MemoryStream())
         {
             ISheet sheet     = versionWorkbook.CreateSheet();
             IRow   headerRow = sheet.CreateRow(this.HeadRowIndex);
             this.SetListHead(headerRow);
             if (this.RowStartIndex == 0)
             {
                 this.RowStartIndex = this.HeadRowIndex + 1;
             }
             int rowStartIndex = this.RowStartIndex;
             foreach (T local in base.DataSource)
             {
                 IRow row2             = sheet.CreateRow(rowStartIndex);
                 int  columnStartIndex = this.ColumnStartIndex;
                 foreach (PropertyInfo info in this.HeadSupport.Values)
                 {
                     ICell exCell = row2.CreateCell(columnStartIndex);
                     if (info == null)
                     {
                         exCell.SetCellValue("");
                     }
                     else
                     {
                         object obj2 = info.GetValue(local, null);
                         ExportHelper.SetCellValue(exCell, obj2, info.PropertyType.Name);
                     }
                     columnStartIndex++;
                 }
                 rowStartIndex++;
             }
             //AutoSizeColumns(sheet);
             versionWorkbook.Write(stream);
             buffer = stream.ToArray();
         }
         using (FileStream stream2 = new FileStream(FileUrl, FileMode.Create, FileAccess.Write))
         {
             stream2.Write(buffer, 0, buffer.Length);
             Console.WriteLine("文件导出成功!" + FileUrl);
         }
     }
     catch (Exception exception)
     {
         throw new ExportException(exception.Message);
     }
 }
Ejemplo n.º 2
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());
            }
        }