Advertisement
Guest User

Merge Excel Sheets, Color Problem

a guest
Jan 3rd, 2012
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. //using System.Linq;
  4. using System.Text;
  5. using Microsoft.Office.Interop.Excel;
  6. using System.Threading;
  7. using System.Globalization;
  8. using Office = Microsoft.Office.Core;
  9. using Microsoft.Office.Core;
  10.  
  11.  
  12. namespace TestExcel
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             Application xlApp = null;
  19.             Workbook xlWorkBook = null;
  20.             Workbook xlWorkBookChild = null;
  21.             Workbooks xlWorkBooks = null;
  22.             Worksheet xlWorkSheet = null;
  23.             Sheets xlSheetChild;
  24.             List<string> listaFicheiros = new List<string>();
  25.  
  26.             LanguageSettings xpto2;
  27.             try
  28.             {
  29.                 xlApp = new Application();
  30.  
  31.  
  32.                 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
  33.                
  34.                 xlWorkBook = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
  35.          
  36.  
  37.                 listaFicheiros.Add(@"\\filePath\file.xls");
  38.  
  39.                 listaFicheiros.Add(@"\\filePath\file2.xls");
  40.  
  41.        
  42.                 foreach (string fileToParse in listaFicheiros)
  43.                 {
  44.                     xlWorkBookChild = xlApp.Workbooks.Open(fileToParse, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  45.                                                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  46.  
  47.                     xlSheetChild = xlWorkBookChild.Sheets;
  48.  
  49.                     if (fileToParse.Contains("MIS_PPP_ALL"))
  50.                     {
  51.                         xlWorkBook.set_Colors(Type.Missing, xlWorkBookChild.get_Colors(Type.Missing));
  52.                     }
  53.  
  54.                
  55.                     foreach (Worksheet xlWorkSheetChild in xlSheetChild)
  56.                     {
  57.                         // Get the last sheet
  58.                         xlWorkSheet = (Worksheet)xlWorkBook.Worksheets[xlWorkBook.Worksheets.Count];
  59.                         //xlWorkBook.Worksheets.Add(Type.Missing, xlWorkSheet);
  60.  
  61.                        
  62.                         xlWorkSheetChild.Copy(Type.Missing, xlWorkSheet);
  63.                        
  64.                        
  65.  
  66.                     }
  67.                  
  68.                     //xlWorkBook.set_Colors(Type.Missing, xlWorkBookChild.get_Colors(Type.Missing));
  69.                     xlWorkBookChild.Close();
  70.                 }
  71.  
  72.  
  73.                 //Microsoft.Office.Interop.Excel.Workbook _ColorsType c =
  74.  
  75.      
  76.                 //xlWorkBook.Colors = Workbook_ColorsType.Colors;
  77.                 xlWorkBook.SaveAs(@"C:\Users\tiago.palhota\Desktop\Excel Testing\tiago", XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  78.  
  79.                 xlWorkBook.Close();
  80.                 xlApp.Quit();
  81.  
  82.  
  83.                 //Console.ReadLine();
  84.  
  85.             }
  86.             catch (Exception ex)
  87.             {
  88.  
  89.                 throw;
  90.             }
  91.             finally
  92.             {
  93.                 xlApp.Quit();
  94.  
  95.             }
  96.  
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement