Ejemplo n.º 1
0
        /// <summary>
        /// Constructor. Writes metadata and Id column header to excel.
        /// </summary>
        public ExcelPresenterBase(string type, IExcelFile file, Views.IExportDialog dialog)
        {
            Views.Excel.Excel excel = new Views.Excel.Excel();
            this._dialog = dialog;
            this._file   = file;

            this._type = type;

            this._dialog.Title              = this._type;
            this._dialog.Label1             = string.Empty;
            this._dialog.Label2             = string.Empty;
            this._dialog.StartButtonEnabled = false;

            // create styles
            // headers and metadata
            var namedStyle = excel.Package.Workbook.Styles.CreateNamedStyle(HEADER_AND_META_STYLE);

            namedStyle.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            namedStyle.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 0, 0, 170));
            namedStyle.Style.Font.Bold = true;
            namedStyle.Style.Font.Color.SetColor(Color.FromArgb(255, 255, 255, 255));
            namedStyle.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
            // even data row
            namedStyle = excel.Package.Workbook.Styles.CreateNamedStyle(EVEN_ROW_STYLE);
            namedStyle.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            namedStyle.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 239, 239, 255));
            // odd data row
            namedStyle = excel.Package.Workbook.Styles.CreateNamedStyle(ODD_ROW_STYLE);
            namedStyle.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            namedStyle.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 207, 207, 255));

            // write metadata
            excel.Worksheet = excel.Package.Workbook.Worksheets.Add(type);
            excel.Worksheet.Cells["A1"].Value        = "MyLibrary";
            excel.Worksheet.Cells[1, 1, 1, 2].Merge  = true;
            excel.Worksheet.Cells["A2"].Value        = "Type";
            excel.Worksheet.Cells["B2"].Value        = this._type + "s";
            excel.Worksheet.Cells["A3"].Value        = "App Version:";
            excel.Worksheet.Cells["B3"].Value        = Configuration.APP_VERSION.ToString();
            excel.Worksheet.Cells["A4"].Value        = "Extracted At:";
            excel.Worksheet.Cells["B4"].Value        = DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss");
            excel.Worksheet.Cells["A1:B4"].StyleName = HEADER_AND_META_STYLE;
            excel.Worksheet.Cells["A1:B4"].AutoFitColumns();
            this._excel = excel;
            // write Id column header
            WriteHeaderCell("A", "Id");
            // set the current row to the first data row, ready for writing entities
            this._currRow = HEADER_ROW + 1;

            // subscribe to dialog view's events
            this._dialog.BrowseButtonClicked += BrowseButtonClicked;
            this._dialog.StartButtonClicked  += (async(sender, args) =>
            {
                await HandleStartButtonClicked(sender, args);
            });
            this._dialog.Cancelled += Closed;

            this._dialog.Title = "Export " + this._type + "s";
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates Excel file based on T and saves it to specified path.
        /// </summary>
        public void Export <T>(IEnumerable <T> collection, List <string> columnNames, string path)
        {
            if (IsCollectionNullOrEmpty(collection) || AreColumnNamesNullOrEmpty(columnNames) || string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("One of the arguments is null or empty.");
            }

            FileInfo fileInfo = CreateFileInfo(path);

            using (IExcelFile excelFile = _excelCreator.Create(collection, sheetName, columnNames))
            {
                excelFile.Save(fileInfo);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns list of T based on *.xlsx worksheet.
        /// </summary>
        public IEnumerable <T> Import(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("Path cannot be null or empty");
            }

            IEnumerable <T> collectionToReturn = new List <T>();

            using (FileStream fileStream = _fileStreamWrapper.Init(path, FileMode.Open))
                using (IExcelFile excelFile = _excelCreator.Create(fileStream))
                {
                    collectionToReturn = new List <T>(excelFile.ConvertToObjects <T>());
                }
            return(collectionToReturn);
        }
Ejemplo n.º 4
0
        public void Test_Create_Should_Create_ExcelFile()
        {
            List <TestExcelObject> testExcelObjects = new List <TestExcelObject>
            {
                new TestExcelObject("name1", 1),
                new TestExcelObject("name2", 2)
            };
            List <string> columnNames = new List <string>
            {
                "name",
                "value"
            };
            string sheetName = "sheetName";

            IExcelFile excelFile = _excelCreator.Create(testExcelObjects, sheetName, columnNames);

            Assert.IsNotNull(excelFile);
        }
Ejemplo n.º 5
0
        //Get FirstSheetName
        public static string GetFirstSheetName(string pathfile)
        {
            string firstsheet = "";
            bool   is2007     = false;

            if (IsExcelFile(pathfile, out is2007))
            {
                IExcelFile excelfile = null;
                if (is2007)
                {
                    excelfile = new Excel2007(pathfile);
                }
                else
                {
                    excelfile = new Excel2003(pathfile);
                }

                firstsheet = excelfile.GetFirstSheetName();
            }
            return(firstsheet);
        }
Ejemplo n.º 6
0
        //Get DataSheet
        public static System.Data.DataTable LoadDataSheet(string pathfile, string sheetname)
        {
            System.Data.DataTable tbl = null;
            bool is2007 = false;

            if (IsExcelFile(pathfile, out is2007))
            {
                IExcelFile excelfile = null;
                if (is2007)
                {
                    excelfile = new Excel2007(pathfile);
                }
                else
                {
                    excelfile = new Excel2003(pathfile);
                }

                tbl = excelfile.GetDataTable_SheetName(sheetname);
            }
            return(tbl);
        }
Ejemplo n.º 7
0
        //Get number of rows data in file
        public static Int64 NumberRows(string pathfile, string sheetname)
        {
            int  num    = 0;
            bool is2007 = false;

            if (IsExcelFile(pathfile, out is2007))
            {
                IExcelFile excelfile = null;
                if (is2007)
                {
                    excelfile = new Excel2007(pathfile);
                }
                else
                {
                    excelfile = new Excel2003(pathfile);
                }

                num = excelfile.GetNumberRows(sheetname);
            }
            return(num);
        }
Ejemplo n.º 8
0
        //Load Format of data in dataTable
        public static System.Data.DataTable LoadTableColumns(string pathfile)
        {
            System.Data.DataTable tbl = null;
            bool is2007 = false;

            if (IsExcelFile(pathfile, out is2007))
            {
                IExcelFile excelfile = null;
                if (is2007)
                {
                    excelfile = new Excel2007(pathfile);
                }
                else
                {
                    excelfile = new Excel2003(pathfile);
                }

                tbl = excelfile.GetDataTable_Format();
            }
            return(tbl);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 获取office文档内容
 /// </summary>
 /// <param name="file"></param>
 /// <returns></returns>
 private static string _GetContent(IOfficeFile file)
 {
     if (file is IWordFile)
     {
         IWordFile wordFile = file as IWordFile;
         return(wordFile.ParagraphText);
     }
     else if (file is IPowerPointFile)
     {
         IPowerPointFile pptFile = file as IPowerPointFile;
         return(pptFile.AllText);
     }
     else if (file is ITextFile)
     {
         ITextFile textFile = file as ITextFile;
         return(textFile.CommentText);
     }
     else if (file is IPDFFile)
     {
         IPDFFile pdfFile = file as IPDFFile;
         return(pdfFile.CommentText);
     }
     else if (file is IHtmlFile)
     {
         IHtmlFile htmlFile = file as IHtmlFile;
         return(htmlFile.CommentText);
     }
     else if (file is IExcelFile)
     {
         IExcelFile excelFile = file as IExcelFile;
         return(excelFile.CommentText);
     }
     else
     {
         return(String.Format("无法解析"));
     }
 }
Ejemplo n.º 10
0
 public ExcelClusterPattern(IExcelFile _myFile)
 {
     myFile = _myFile;
 }
Ejemplo n.º 11
0
 public ExcelStaticFileCluster(IExcelFile _myFile) : base(_myFile)
 {
 }
Ejemplo n.º 12
0
 public ExcelOpenedFileCluster(IExcelFile _myFile) : base(_myFile)
 {
 }
Ejemplo n.º 13
0
 public TagExcelPresenter(ITagService tagService, IExcelFile file, Views.IExportDialog dialog)
     : base("Tag", file, dialog)
 {
     this._tagService = tagService;
     WriteHeaders();
 }
Ejemplo n.º 14
0
 public MockPresenter(IWishlistService wishlistService, IExcelFile file, IExportDialog dialog)
     : base(wishlistService, file, dialog)
 {
 }
Ejemplo n.º 15
0
 public MockPresenter(IMediaItemService itemService, IExcelFile file, IExportDialog dialog)
     : base(itemService, file, dialog)
 {
 }
Ejemplo n.º 16
0
 public BookExcelPresenter(IBookService bookService, IExcelFile file, Views.IExportDialog dialog)
     : base("Book", file, dialog)
 {
     this._bookService = bookService;
     WriteHeaders();
 }
Ejemplo n.º 17
0
 public WishlistExcelPresenter(IWishlistService wishlistService, IExcelFile file, Views.IExportDialog dialog)
     : base("Wishlist item", file, dialog)
 {
     this._wishlistService = wishlistService;
     WriteHeaders();
 }
Ejemplo n.º 18
0
 public MockPresenter(IAuthorService authorService, IExcelFile file, IExportDialog dialog)
     : base(authorService, file, dialog)
 {
 }
Ejemplo n.º 19
0
        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);

            if (close)
            {
                if (excelFile != null)
                {
                    excelFile.Dispose();
                    excelFile = null;
                }
            }
        }
Ejemplo n.º 20
0
 public void Browse()
 {
     var dialog = new OpenFileDialog();
     dialog.FileName = Filename;
     dialog.Filter = null;
     var result = dialog.ShowDialog() ?? false;
     if (result)
     {
         Filename = dialog.FileName;
         if (excelFile != null)
         {
             excelFile.Dispose();
             excelFile = null;
         }
         excelFile = excelService.Open(filename);
         Sheets.Clear();
         var worksheets = excelFile.Worksheets;
         foreach (var sheet in worksheets)
         {
             Sheets.Add(sheet.Name);
         }
     }
 }
Ejemplo n.º 21
0
 public MockPresenter(IBookService bookService, IExcelFile file, IExportDialog dialog)
     : base(bookService, file, dialog)
 {
 }
Ejemplo n.º 22
0
 public AuthorExcelPresenter(IAuthorService authorService, IExcelFile file, Views.IExportDialog dialog)
     : base("Author", file, dialog)
 {
     this._authorService = authorService;
     WriteHeaders();
 }
Ejemplo n.º 23
0
 public MediaItemExcelPresenter(IMediaItemService tagService, IExcelFile file, Views.IExportDialog dialog)
     : base("Media item", file, dialog)
 {
     this._mediaItemService = tagService;
     WriteHeaders();
 }
 public MockPresenter(IPublisherService publisherService, IExcelFile file, IExportDialog dialog)
     : base(publisherService, file, dialog)
 {
 }
Ejemplo n.º 25
0
 public MockPresenter(ITagService tagService, IExcelFile file, IExportDialog dialog)
     : base(tagService, file, dialog)
 {
 }
Ejemplo n.º 26
0
 public PublisherExcelPresenter(IPublisherService publisherService, IExcelFile file, Views.IExportDialog dialog)
     : base("Publisher", file, dialog)
 {
     this._publisherService = publisherService;
     WriteHeaders();
 }