コード例 #1
0
        public void UpdatePhotoItems(IEnumerable <PictureItem> items, ExcelWorkSpaceInfo excelInfo, BackgroundWorker bw)
        {
            string targetColumn = ExcelStatic.GetColumnName(excelInfo.SelectedSheet.LastCell.Column + 1);

            try {
                bw.ReportProgress(0, "Записываем результат в Excel файл");
                Excel.Workbook  xlWorkBook  = OpenExcelFile(excelInfo.WorkBook.Path);
                Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[excelInfo.SelectedSheetIndex];

                for (int i = excelInfo.RowBeginUpload; i <= excelInfo.RowEndUpload; i++)
                {
                    var name = xlWorkSheet.Range[excelInfo.ColumnPictureNames + i].Value.ToString().Trim();
                    var item = items.FirstOrDefault(a => a.Name == name);
                    if (item.Status)
                    {
                        xlWorkSheet.Hyperlinks.Add(Anchor: xlWorkSheet.Range[targetColumn + i], Address: item.Address, TextToDisplay: "Фото");
                    }
                    else
                    {
                        xlWorkSheet.Range[targetColumn + i].Value = item.Address;
                    }
                }
                xlWorkBook.Save();
                xlWorkBook.Close();
                Release(xlWorkSheet);
                Release(xlWorkBook);
                ExitExcelApplication();
            }
            catch (Exception ex)
            {
                try
                {
                    if (xlApp != null)
                    {
                        ExitExcelApplication();
                    }
                }
                finally
                {
                    ReleaseUnmanaged();
                    throw ex;
                }
            }
        }
コード例 #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;
            }
        }