コード例 #1
0
ファイル: UsingExcel.cs プロジェクト: Jugius/PicturesUploader
        public List <PictureItem> GetPhotoItems(UploadingExcelParameters excelInfo)
        {
            this.OpenedExcelFile = excelInfo.FilePath;
            try
            {
                InitializeExcelApplication();
                this.xlWorkBook  = IExcelProcessor.OpenWorkbook(this.xlApp, excelInfo.FilePath);
                this.xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[excelInfo.SheetIndex];

                List <PictureItem> Items = new List <PictureItem>();
                for (int i = excelInfo.RowBegin; i <= excelInfo.RowEnd; i++)
                {
                    string picName = null;
                    if (!string.IsNullOrEmpty(excelInfo.ColumnWithNames))
                    {
                        if (xlWorkSheet.Range[excelInfo.ColumnWithNames + i].Value != null)
                        {
                            picName = xlWorkSheet.Range[excelInfo.ColumnWithNames + i].Value.ToString().Trim();
                        }
                    }

                    if (string.IsNullOrEmpty(picName))
                    {
                        picName = Guid.NewGuid().ToString();
                    }

                    PictureItem item = new PicturesUploader.PictureItem(i, picName);
                    Uri         url  = GetUrlFromCell(xlWorkSheet.Range[excelInfo.ColumnWithLinks + i]);

                    if (url == null)
                    {
                        item.Error = new Exception("Не найдена ссылка в ячейке");
                    }
                    else
                    {
                        item.Address = url;
                    }

                    Items.Add(item);
                }

                return(Items);
            }
            finally
            {
                xlWorkBook.Close();
                ExitExcelApplication();
            }
        }
コード例 #2
0
ファイル: UsingExcel.cs プロジェクト: Jugius/PicturesUploader
        public void UpdatePhotoItems(IEnumerable <PictureItem> items, UploadingExcelParameters excelInfo)
        {
            try
            {
                InitializeExcelApplication();
                this.xlWorkBook  = IExcelProcessor.OpenWorkbook(this.xlApp, excelInfo.FilePath);
                this.xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[excelInfo.SheetIndex];
                var    sheetInfo    = GetSheetInfo(this.xlWorkSheet);
                string targetColumn = ExcelStatic.GetColumnName(sheetInfo.LastCell.Column + 1);

                foreach (var item in items)
                {
                    if (item.Error == null)
                    {
                        string link = (item.Address.Scheme == Uri.UriSchemeFile) ? item.Address.LocalPath : item.Address.AbsoluteUri;
                        string val  = excelInfo.IncludeLinkToCell ? link : "Фото";
                        xlWorkSheet.Hyperlinks.Add(Anchor: xlWorkSheet.Range[targetColumn + item.RowIndex], Address: link, TextToDisplay: val);
                    }
                    else
                    {
                        xlWorkSheet.Range[targetColumn + item.RowIndex].Value = item.Error.Message;
                    }
                }
                xlWorkBook.Save();
                xlWorkBook.Close();
                ExitExcelApplication();
            }
            catch
            {
                if (xlWorkBook != null)
                {
                    xlWorkBook.Close();
                }

                if (xlApp != null)
                {
                    ExitExcelApplication();
                }

                throw;
            }
        }
コード例 #3
0
ファイル: UsingExcel.cs プロジェクト: Jugius/PicturesUploader
        public ExcelFileInfo ReadExcelFileInfo(string fileName)
        {
            try
            {
                InitializeExcelApplication();
                this.xlWorkBook = IExcelProcessor.OpenWorkbook(this.xlApp, fileName, true);

                List <ExcelSheet> sheets       = new List <ExcelSheet>();
                Excel.Sheets      xlWorkSheets = xlWorkBook.Sheets;

                foreach (Excel.Worksheet sheet in xlWorkSheets)
                {
                    sheets.Add(GetSheetInfo(sheet));
                }

                ExcelFileInfo result = new ExcelFileInfo(fileName, sheets);

                xlWorkBook.Close();
                ExitExcelApplication();
                return(result);
            }
            catch
            {
                if (xlWorkBook != null)
                {
                    xlWorkBook.Close();
                }

                if (xlApp != null)
                {
                    ExitExcelApplication();
                }

                throw;
            }
        }