示例#1
1
        protected void Workbook_SheetActivate(object Sh) {
            Excel.Workbook wb = dsoFramerWordControl1.AxFramerControl.ActiveDocument as Excel.Workbook;
            ExcelAccess ea = new ExcelAccess();
            ea.MyWorkBook = wb;
            ea.MyExcel = wb.Application;
            Excel.Worksheet sheet;
            sheet = wb.ActiveSheet as Excel.Worksheet;
            if (activeSheetName != sheet.Name) {
                sheet.Name = activeSheetName;
            }
            if (wb.Application.Sheets[activeSheetIndex] != wb.Application.Sheets[activeSheetName]) {

                sheet = wb.Application.Sheets[activeSheetName] as Excel.Worksheet;
                sheet.Move(Type.Missing, wb.Application.Sheets[activeSheetIndex]);
            }
            ea.ActiveSheet(activeSheetName);
        }
示例#2
0
        public Excel.Worksheet GetGraphsAndTablesWorksheet()
        {
            Excel.Worksheet newWorksheet = null;

            Worksheet activeWorksheet = (Worksheet)Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.ActiveSheet);

            foreach (Excel.Worksheet worksheet in Application.Worksheets)
            {
                if (worksheet.Name.Equals("Wykresy i Tabele"))
                {
                    newWorksheet = worksheet;
                }
            }


            if (newWorksheet == null)
            {
                newWorksheet = (Excel.Worksheet)Application.Worksheets.Add();
                newWorksheet.Move(After: Application.Worksheets.Item[Application.Worksheets.Count]);
                newWorksheet.Name = "Wykresy i Tabele";
                activeWorksheet.Activate();
            }

            return(newWorksheet);
        }
        private void tsbSortDesc_Click(object sender, EventArgs e)
        {
            System.Collections.ArrayList al = new System.Collections.ArrayList();
            Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook;
            int            x  = 0;

            try
            {
                foreach (Excel.Worksheet s in wb.Sheets)
                {
                    al.Add(s.Name);
                }
                al.Sort();

                foreach (string item in al)
                {
                    Excel.Worksheet s = (Excel.Worksheet)wb.Sheets[item];
                    int             y = wb.Sheets.Count - x;
                    s.Move(wb.Sheets[y]);
                    x += 1;
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.DisplayMessage(ex);
            }
            finally
            {
                LoadListview();
            }
        }
示例#4
0
 //현재 Active한 excel file에 새로운 worksheet append
 public static void Append()
 {
     Excel.Worksheet newWS = null;
     newWS = eWB.Worksheets.Item[eWB.Worksheets.Count];
     eWS   = eWB.Worksheets.Add();
     eWS.Move(After: newWS);
 }
示例#5
0
        protected override void Execute(CodeActivityContext context)
        {
            Console.WriteLine("Append");

            var    EngineInstance = (Program)instance.Get(context);
            string InstanceName   = instanceName.Get(context);
            object excel;

            //Console.WriteLine(sendingInstance.GetType());

            if (EngineInstance.appInstance.TryGetValue(InstanceName, out excel))
            {
                Console.WriteLine("Success");
                //Excel.Workbook eWB = (Excel.Workbook)excel;
                Excel.Application eXL   = (Excel.Application)excel;
                Excel.Workbook    eWB   = eXL.ActiveWorkbook;
                Excel.Worksheet   eWS   = eWB.Worksheets.Item[eWB.Worksheets.Count];
                Excel.Worksheet   newWS = eWB.Worksheets.Add();
                newWS.Move(After: eWS);
                //ReleaseExcelObject(eWB);
            }
            else
            {
                Console.WriteLine("Fail");
            }
        }
示例#6
0
 public Excel.Worksheet addShtAtLst(string shtnm)
 {
     wrkbook.Worksheets.Add(After: wrkbook.Worksheets[wrkbook.Worksheets.Count]);
     Excel.Worksheet sht = (Excel.Worksheet)wrkbook.Worksheets[wrkbook.Worksheets.Count];
     sht.Name = shtnm;
     sht.Move(Before: wrkbook.Worksheets["Sheet1"]);
     return(sht);
 }
示例#7
0
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            object misValue;

            xlApp = new Excel.Application();

            misValue      = System.Reflection.Missing.Value;
            xlWorkBook    = xlApp.Workbooks.Add(misValue);
            xlWorkSheet   = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet2  = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
            xlWorkSheet3  = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
            xlWorkSheet4  = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            xlWorkSheet5  = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            xlWorkSheet6  = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            xlWorkSheet7  = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            xlWorkSheet8  = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            xlWorkSheet9  = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            xlWorkSheet10 = (Excel.Worksheet)xlWorkBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            xlWorkSheet.Name   = "카드정보";
            xlWorkSheet2.Name  = "소속";
            xlWorkSheet3.Name  = "부서";
            xlWorkSheet4.Name  = "직위";
            xlWorkSheet5.Name  = "카드상태";
            xlWorkSheet6.Name  = "카드종류";
            xlWorkSheet7.Name  = "카드포멧";
            xlWorkSheet8.Name  = "발급사유";
            xlWorkSheet9.Name  = "발급유형";
            xlWorkSheet10.Name = "권한정보";

            xlWorkSheet.Move(misValue, xlApp.Worksheets[1]);
            xlWorkSheet2.Move(misValue, xlApp.Worksheets[2]);
            xlWorkSheet3.Move(misValue, xlApp.Worksheets[3]);
            xlWorkSheet4.Move(misValue, xlApp.Worksheets[4]);
            xlWorkSheet5.Move(misValue, xlApp.Worksheets[5]);
            xlWorkSheet6.Move(misValue, xlApp.Worksheets[6]);
            xlWorkSheet7.Move(misValue, xlApp.Worksheets[7]);
            xlWorkSheet8.Move(misValue, xlApp.Worksheets[8]);
            xlWorkSheet9.Move(misValue, xlApp.Worksheets[9]);
            xlWorkSheet10.Move(misValue, xlApp.Worksheets[10]);

            xlWorkSheet.Select(misValue);



            makeHeader();


            // 상단 첫번째 Row 타이틀 볼드
            SetHeaderBold(xlWorkSheet, 1);

            // 자동 넓이 지정
            // (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1,1].Interior.ColorIndex = 15;

            xlApp.Visible = true;
        }
示例#8
0
        public void moveSheetToEnd(Excel.Worksheet sheet, Excel.Workbook target)
        {
            // moves the sheet to the end of the workbook
            int sheetCount = target.Sheets.Count;

            try
            {
                sheet.Move(Type.Missing, target.Sheets[sheetCount]);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, "Worksheet Move Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
示例#9
0
        static public bool CreatReportFile(string fileName, string[] nameSheet)
        {
            if (nameSheet.Length == 0)
            {
                return(false);
            }

            try
            {
                Microsoft.Office.Interop.Excel.Application xlApp       = null;
                Microsoft.Office.Interop.Excel.Workbook    xlWorkBook  = null;
                Microsoft.Office.Interop.Excel.Worksheet   xlWorkSheet = null;
                object misValue = System.Reflection.Missing.Value;

                xlApp      = new Microsoft.Office.Interop.Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);

                for (int i = 0; i < nameSheet.Length; i++)
                {
                    xlWorkSheet      = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.Add();
                    xlWorkSheet.Name = nameSheet[i];
                    xlWorkSheet.Move(System.Reflection.Missing.Value, xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
                }
                Console.WriteLine("Delete default sheet: " + xlWorkBook.Worksheets[1].Name);
                xlWorkBook.Worksheets[1].Delete();

                xlWorkBook.SaveAs(fileName);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                Marshal.ReleaseComObject(xlWorkSheet);
                Marshal.ReleaseComObject(xlWorkBook);
                Marshal.ReleaseComObject(xlApp);

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }
        }
示例#10
0
        static void Main(string[] args)
        {
            string templatePath = args.Length > 0 ? args[0] : Path.Combine(Environment.CurrentDirectory, "template.xlsm");

            if (!File.Exists(templatePath))
            {
                Console.Error.WriteLine("File not found: {0}", templatePath);
                return;
            }

            getTerm("ca");//dummy just for console output;

            var reports = getIndexedRecords().OrderBy(o => o.name.ToLower()).ToArray();

            Console.WriteLine("{0} records found", reports.Count());

            foreach (var r in reports)
            {
                Console.WriteLine("{0} - {1}", r.government, r.name);
            }

            Excel.Application xlApp;
            Excel.Workbook    xlWorkBook;

            Console.WriteLine();
            Console.WriteLine("Loading Excel template {0}", Path.GetFileName(templatePath));

            using (new XLDisposable(xlApp = new Excel.Application()))
                using (new XLDisposable(xlWorkBook = (Excel.Workbook)xlApp.Workbooks.Open(templatePath, ReadOnly: true)))
                {
                    var oSheetNames = xlWorkBook.Worksheets.OfType <Excel.Worksheet>().Select(o => o.Name).ToList();

                    Excel.Worksheet xlWorkSheetTemplate = (Excel.Worksheet)xlWorkBook.Worksheets["{{template}}"];

                    Excel.Worksheet xlWorkSheetMenu = (Excel.Worksheet)xlWorkBook.Worksheets["MENU"];

                    var bindings = getBindings(xlWorkSheetTemplate);
                    int row      = 3;

                    foreach (var report in reports)
                    {
                        Console.WriteLine();

                        if (oSheetNames.Contains(report.name))
                        {
                            Console.WriteLine("SKIP: Sheet already exists: {0}", report.name);
                            continue;
                        }

                        oSheetNames.Add(report.name);

                        xlWorkSheetTemplate.Copy(Before: xlWorkSheetTemplate);

                        while (!string.IsNullOrWhiteSpace((string)xlWorkSheetMenu.Cells[row, 2].Value2))
                        {
                            row++; // next empty row
                        }
                        xlWorkSheetMenu.Cells[row, 2] = report.name;

                        Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets[xlWorkSheetTemplate.Index - 1];

                        var values = mapValues(report.record);

                        xlWorkSheet.Name = report.name;

                        populate(xlWorkSheet, values, bindings);
                    }

                    xlWorkSheetTemplate.Move(After: (Excel.Worksheet)xlWorkBook.Worksheets[xlWorkBook.Worksheets.Count]);

                    xlWorkSheetMenu.Activate();

                    var outPath = Path.Combine(Path.GetDirectoryName(templatePath), string.Format("out-{0:yyyy-MM-dd-HH-mm-ss}", DateTime.Now) + Path.GetExtension(templatePath));

                    xlWorkBook.SaveAs(outPath);
                }
        }
示例#11
0
        /// <summary>
        /// create report file with fomat Battery LOG
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        static public bool CreateReportFile(string fileName)
        {
            List <String> lStr = new List <string>();

            lStr.Add("log_data");

            for (int i = 1; i <= NUMS_BATTERY; i++)
            {
                lStr.Add("Bat " + i);
            }

            try
            {
                Microsoft.Office.Interop.Excel.Application xlApp           = null;
                Microsoft.Office.Interop.Excel.Workbook    xlWorkBook      = null;
                Microsoft.Office.Interop.Excel.Worksheet   xlWorkSheet     = null;
                Microsoft.Office.Interop.Excel.Worksheet   xlDataWorkSheet = null;
                object misValue = System.Reflection.Missing.Value;

                xlApp      = new Microsoft.Office.Interop.Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);

                for (int i = 0; i < lStr.Count; i++)
                {
                    xlWorkSheet      = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.Add();
                    xlWorkSheet.Name = lStr[i];
                    xlWorkSheet.Move(System.Reflection.Missing.Value, xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
                }
                Console.WriteLine("Delete default sheet: " + xlWorkBook.Worksheets[1].Name);
                xlWorkBook.Worksheets[1].Delete();

                xlDataWorkSheet = xlWorkBook.Worksheets[1];
                string fomular;
                int    shiftCol;
                // assign fomular for each sheet
                for (int i = 2; i <= lStr.Count; i++)// sheet
                {
                    xlWorkSheet = xlWorkBook.Worksheets[i];
                    shiftCol    = 9 * (i - 2);

                    // set header
                    xlWorkSheet.Cells[1, 2] = "temperature";
                    xlWorkSheet.Cells[1, 3] = "remain capacity";
                    xlWorkSheet.Cells[1, 4] = "vol of bat";
                    xlWorkSheet.Cells[1, 5] = "avg current";
                    xlWorkSheet.Cells[1, 6] = "bat percent";
                    xlWorkSheet.Cells[1, 7] = "state of bat";
                    xlWorkSheet.Cells[1, 8] = "Number Run";
                    xlWorkSheet.Cells[1, 9] = "res of bat";

                    // end of set header

                    // set fomular for each cell
                    for (int j = 2; j < gNumsRow; j++)// row
                    {
                        //                1                      2          3          4           5         6       7         8                         9
                        // 1.______________________________  temperature  remain   capacity   vol of bat    avg    current    bat     percent state of bat Number Run res of bat
                        // 2.[Sat Oct 27 09:35:35.512 2018]       38.6        610      3604       1661        31       10       10                       0

                        // net add shift for time log
                        fomular = getExtractString(xlDataWorkSheet.Name, j, 1);
                        xlWorkSheet.Cells[j, 1].Formula = fomular;

                        // add shift
                        for (int k = 2; k <= 9; k++)
                        {
                            fomular = getExtractString(xlDataWorkSheet.Name, j, shiftCol + k);
                            xlWorkSheet.Cells[j, k].Formula = fomular;
                        }
                    }

                    for (int h = 2; h <= 9; h++)
                    {
                        // add chart
                        Microsoft.Office.Interop.Excel.Range        chartRange;
                        Microsoft.Office.Interop.Excel.ChartObjects xlCharts  = (Microsoft.Office.Interop.Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
                        Microsoft.Office.Interop.Excel.ChartObject  myChart   = (Microsoft.Office.Interop.Excel.ChartObject)xlCharts.Add(500, 300 + (350 + 100) * (h - 2), 1000, 350);
                        Microsoft.Office.Interop.Excel.Chart        chartPage = myChart.Chart;

                        // TODO: hard code
                        chartRange = xlWorkSheet.Range[String.Format("A1:A{0},{1}1:{1}{0}", gNumsRow + "", (char)((int)'A' + h - 1))];
                        chartPage.SetSourceData(chartRange, misValue);
                        //chartPage.Name = (string)xlWorkSheet.Cells[1, h].Value;
                        chartPage.ChartType       = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
                        chartPage.ChartTitle.Text = (string)xlWorkSheet.Cells[1, h].Value;
                    }
                }
                //

                xlWorkBook.SaveAs(fileName);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                Marshal.ReleaseComObject(xlWorkSheet);
                Marshal.ReleaseComObject(xlWorkBook);
                Marshal.ReleaseComObject(xlApp);

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }
        }
示例#12
0
        /// <summary>
        /// Moves worksheet to desired location within the current workbook
        /// </summary>
        /// <param name="worksheet">Worksheet being moved</param>
        /// <param name="workbook">Active workbook the worksheet exists in</param>
        /// <param name="positional">Desired position of worksheet. Default position is the end of the workbook.</param>
        /// <param name="referenceName">Name of worksheet position is relative to</param>
        /// <param name="referenceType">If relative worksheet is referenced through name or index</param>
        public static void MoveWorksheet(this Excel.Workbook workbook, string worksheetName,
                                         PositionalEnum positional, string referenceName, ReferenceEnum referenceType = ReferenceEnum.ByName)
        {
            Excel.Worksheet worksheet   = null;
            var             sheetCount  = workbook.Worksheets.Count;
            int             indexValue  = 1;
            bool            isAnInteger = false;

            // Checks if target sheet exists
            if (!workbook.WorksheetExists(worksheetName))
            {
                throw new ArgumentException($"Worksheet [{worksheetName}] does not exist in the workbook");
            }

            worksheet = workbook.GetWorksheet(worksheetName);

            // If relative sheet for positioning is required
            if (positional != PositionalEnum.AtBeginning && positional != PositionalEnum.AtEnd)
            {
                if (string.IsNullOrEmpty(referenceName))
                {
                    throw new ArgumentException("A worksheet reference is required due to selected option");
                }
            }

            if (referenceType == ReferenceEnum.ByIndex)
            {
                // Test if sheet name/index is an integer value
                isAnInteger = int.TryParse(referenceName, out indexValue);

                if (isAnInteger == false)
                {
                    throw new ArgumentException("Index value given is either not a string or is not an integer");
                }

                // Check index value exists within the current workbook
                if (indexValue <= 0 || indexValue > sheetCount)
                {
                    throw new ArgumentException("Index value is out of range");
                }

                if (worksheet.Index == indexValue)
                {
                    throw new ArgumentException("Cannot move a worksheet when referencing its own index");
                }
            }
            else
            {
                // If relative worksheet name is needed
                if (positional != PositionalEnum.AtBeginning && positional != PositionalEnum.AtEnd)
                {
                    // If relative worksheet does not exist
                    if (!workbook.WorksheetExists(referenceName))
                    {
                        throw new ArgumentException("Relative worksheet name does not exist");
                    }
                }
            }

            // Moves worksheet to desired position
            switch (positional)
            {
            case PositionalEnum.AtBeginning:
                worksheet.Move(workbook.Worksheets[1]);
                break;

            case PositionalEnum.AtEnd:
                worksheet.Move(After: workbook.Worksheets[sheetCount]);
                break;

            case PositionalEnum.After:
                if (referenceType == ReferenceEnum.ByIndex && isAnInteger == true)
                {
                    worksheet.Move(After: workbook.Worksheets[indexValue]);
                }
                else
                {
                    worksheet.Move(After: workbook.Worksheets[$"{referenceName}"]);
                }

                break;

            case PositionalEnum.Before:
                if (referenceType == ReferenceEnum.ByIndex && isAnInteger == true)
                {
                    worksheet.Move(workbook.Worksheets[indexValue]);
                }
                else
                {
                    worksheet.Move(workbook.Worksheets[$"{referenceName}"]);
                }

                break;
            }
        }
示例#13
0
        protected void Workbook_SheetActivate(object Sh)
        {
            Excel.Workbook wb = dsoFramerWordControl1.AxFramerControl.ActiveDocument as Excel.Workbook;
            ExcelAccess ea = new ExcelAccess();
            ea.MyWorkBook = wb;
            ea.MyExcel = wb.Application;
            Excel.Worksheet sheet;
            sheet = wb.ActiveSheet as Excel.Worksheet;
            sheet.SelectionChange += new Microsoft.Office.Interop.Excel.DocEvents_SelectionChangeEventHandler(sheet_SelectionChange);

            if (activeSheetName != sheet.Name)
            {
                sheet.Name = activeSheetName;
            }
            if (wb.Application.Sheets[activeSheetIndex] != wb.Application.Sheets[activeSheetName])
            {
                sheet = wb.Application.Sheets[activeSheetName] as Excel.Worksheet;
                sheet.Move(Type.Missing, wb.Application.Sheets[activeSheetIndex]);
            }

            try
            {
                ea.ActiveSheet(activeSheetName);
            }
            catch { }

        }
示例#14
0
        private static bool CreateNewSheet(Dictionary <string, List <string> > strDetail, Microsoft.Office.Interop.Excel.Application xlApp,
                                           List <string> listOfNewSheetColumns, List <Dictionary <string, Dictionary <string, string> > > listForWebData,
                                           List <string> symbolListsFinal, string filePath, string dialogResult, Dictionary <string, List <string> > historyList)
        {
            var isCreated = true;
            int j         = 0;

            Excel.Worksheet xlNewsheet = null;
            Excel.Workbook  xlWorkbook = xlApp.Workbooks.Open(filePath, 0, false, 5, "", "", false,
                                                              Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            Excel.Sheets workSheets = xlWorkbook.Worksheets;
            try
            {
                xlApp.DisplayAlerts = false;
                xlNewsheet          = (Excel.Worksheet)workSheets.Add(workSheets[1], Type.Missing, Type.Missing, Type.Missing);
                xlNewsheet.Move(Missing.Value, xlWorkbook.Sheets[xlWorkbook.Sheets.Count]);
                xlNewsheet.Name = DateTime.Now.ToString("s").Replace('-', '.').Replace(':', '.');
                xlNewsheet      = (Excel.Worksheet)xlWorkbook.Worksheets.Item[xlWorkbook.Sheets.Count];
                xlNewsheet.Select();

                // For header Names.

                for (int i = 0; i < listOfNewSheetColumns.Count; i++)
                {
                    xlNewsheet.Cells[1, ++j] = listOfNewSheetColumns[i];
                }

                xlNewsheet.Columns.AutoFit();

                // For Header colors
                j = 0;
                for (int i = 0; i < listOfNewSheetColumns.Count; i++)
                {
                    xlNewsheet.Cells[1, ++j].Interior.Color = Excel.XlRgbColor.rgbBlanchedAlmond;
                }

                //writing the web data respective to their column.
                int k = 0;
                for (int i = 2; i < listForWebData.Count + 2; i++)
                {
                    var i1 = i;
                    Console.WriteLine("Writing teh web data respective to their column for row: " + i1 + "...");
                    int m = 0, l = 0;
                    var symbolDictionary = listForWebData[k][symbolListsFinal[k]];
                    for (j = 1; j <= listOfNewSheetColumns.Count; j++)
                    {
                        if (j <= listOfNewSheetColumns.Count - 3)
                        {
                            xlNewsheet.Cells[i, j] = symbolDictionary[listOfNewSheetColumns[m]];
                        }
                        else
                        {
                            xlNewsheet.Cells[i, j] = strDetail.ContainsKey(symbolListsFinal[k])
                                    ? strDetail[symbolListsFinal[k]][l]
                                    : string.Empty;
                            l++;
                        }
                        m++;
                    }
                    k++;
                }
                //if (dialogResult == "0") //need to check for the Yahoo working api.
                //{
                var lastRow = listOfNewSheetColumns.Count + 1;
                xlNewsheet.Cells[1, lastRow] = "Priority";
                xlNewsheet.Columns.AutoFit();

                //For Header colors
                xlNewsheet.Cells[1, lastRow].Interior.Color = Excel.XlRgbColor.rgbBlanchedAlmond;
                DoProcess(historyList);
                //writing the priority data respective to their column.
                k = 2;
                foreach (var value in _result.Values)
                {
                    var i1 = k;
                    Console.WriteLine("Updating the priority excel sheet column: " + i1 + " ...");
                    xlNewsheet.Cells[k, lastRow] = value;
                    k++;
                }
                //}
                xlNewsheet.Columns.AutoFit();
                xlWorkbook.Save();
            }
            catch (Exception e)
            {
                isCreated = false;
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                xlWorkbook.Close();
                releaseObject(xlNewsheet);
                releaseObject(workSheets);
                releaseObject(xlWorkbook);
            }
            return(isCreated);
        }
示例#15
0
        private static bool CreatePriorityExcel(Dictionary <string, int> result)
        {
            var isCreated            = true;
            int j                    = 0;
            var listOfNewShetColumns = new List <string>()
            {
                "Symbol", "Priority"
            };
            string priorityExcelPath = @"C:\MarketWatch\GoogleResult";
            string priorityExcelName = "\\PriorityExcel.xlsx";
            string fullPath          = string.Empty;

            Excel.Worksheet   xlNewSheet = null;
            Excel.Application xlApp      = new Excel.Application();
            Excel.Workbook    xlWorkBook = null;

            CheckAndCreateDirectories(priorityExcelPath, String.Empty, out fullPath);
            xlWorkBook = xlApp.Workbooks.Open(fullPath + priorityExcelName, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
                                              false, 0, true, false, false);
            Excel.Sheets workSheets = xlWorkBook.Worksheets;

            try
            {
                Console.WriteLine("Creating priority excel sheet");
                xlApp.DisplayAlerts = false;
                xlNewSheet          = (Excel.Worksheet)workSheets.Add(workSheets[1], Type.Missing, Type.Missing, Type.Missing);
                xlNewSheet.Move(Missing.Value, xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
                xlNewSheet.Name = DateTime.Now.ToString("s").Replace('-', '.').Replace(':', '.');
                xlNewSheet      = (Excel.Worksheet)xlWorkBook.Worksheets.Item[xlWorkBook.Sheets.Count];
                xlNewSheet.Select();

                // For header Names.

                for (int i = 0; i < listOfNewShetColumns.Count; i++)
                {
                    xlNewSheet.Cells[1, ++j] = listOfNewShetColumns[i];
                }

                xlNewSheet.Columns.AutoFit();

                // For Header colors
                j = 0;
                for (int i = 0; i < listOfNewShetColumns.Count; i++)
                {
                    xlNewSheet.Cells[1, ++j].Interior.Color = Excel.XlRgbColor.rgbBlanchedAlmond;
                }

                int k = 2;
                foreach (var key in result.Keys)
                {
                    var i1 = k;
                    Console.WriteLine("Updating the priority excel sheet column1: " + i1 + " ...");
                    xlNewSheet.Cells[k, 1] = key;
                    k++;
                }

                k = 2;
                foreach (var value in result.Values)
                {
                    var i1 = k;
                    Console.WriteLine("Updating the priority excel sheet column2: " + i1 + " ...");
                    xlNewSheet.Cells[k, 2] = value;
                    k++;
                }
            }
            catch (Exception e)
            {
                isCreated = false;
                Console.WriteLine("Exception: " + e.Message);
            }

            finally
            {
                xlWorkBook.Close();
                releaseObject(xlNewSheet);
                releaseObject(workSheets);
                releaseObject(xlWorkBook);
            }
            return(isCreated);
        }