Guest User

ExcelInteropProblem

a guest
Jan 2nd, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. // How i call the method
  2. object value = null;
  3. bool fixWorked = false;
  4.  
  5. Thread t = new Thread(() => value = LoadChartTemplate());
  6. t.SetApartmentState(ApartmentState.STA);
  7. Start();
  8. Join();
  9.  
  10. // Reading thread return
  11. fixWorked = (bool)value;
  12.  
  13.  
  14.  
  15. // My Method
  16. private static bool LoadChartTemplate ()
  17.         {
  18.             Application excelApp = null;
  19.             Workbook    workbook = null;
  20.             Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
  21.            
  22.             try
  23.             {
  24.                 excelApp = new Microsoft.Office.Interop.Excel.Application();
  25.                 workbook = excelApp.Workbooks.Open (config.DiretorioRelatorio);
  26.                 worksheet = workbook.Sheets[Consts.RECOVERED_SHEET];
  27.  
  28.                 string template = @"C:\Projetos\Marcello Lins\TIM\Relatorios_Cobranca\TimRelatorioCampEscritaXPagamento\ReportLIB\ReportPath\TimChartTemplate.crtx";
  29.  
  30.                 ChartObject charts = worksheet.ChartObjects (0);
  31.                 charts.Chart.ApplyChartTemplate (config.DiretorioRelatorio);
  32.  
  33.                 return true;
  34.             }
  35.             catch (Exception ex)
  36.             {
  37.                 Console.WriteLine (ex.Message);
  38.  
  39.                 return false;
  40.             }
  41.             finally
  42.             {
  43.                 workbook.Save ();
  44.                 workbook.Close();
  45.                 excelApp.Quit ();
  46.  
  47.                 ReleaseObject (worksheet);
  48.                 ReleaseObject (workbook);
  49.                 ReleaseObject (excelApp);
  50.             }
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment