Click here to Skip to main content
15,885,998 members
Articles / Productivity Apps and Services / Microsoft Office

A Complete Excel Programming Sample

Rate me:
Please Sign up or sign in to vote.
4.33/5 (13 votes)
19 Dec 2007CPOL3 min read 357.1K   15.5K   86  
An article on Excel programming, including Excel operations in C# and VBA
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using AdventureWorks.SpecialOffer.ExcelEngine;

namespace AdventureWorks.SpecialOffer.Console
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            RunSchedul(args);
        }

        
        private static void RunSchedul(string[] args)
        {
            try
            {
                string method = "TP";
                string branchID = "";
                string sDate = "";
                string action = "";
                string filePath = "";


                for (int i = 0; i < args.Length; i++)
                {
                    switch (args[i].ToUpper())
                    {
                        case "/METHOD":
                            method = args[i + 1].ToUpper();
                            break;
                        case "/BRANCH":
                            branchID = args[i + 1].ToUpper();
                            break;
                        case "/DATE":
                            sDate = args[i + 1].ToUpper();
                            break;
                        case "/ACTION":
                            action = args[i + 1].ToUpper();
                            break;
                        case "/FILEPATH":
                            filePath = args[i + 1].ToUpper();
                            break;
                    }
                }

                switch (method)
                {
                    case "TP":
                        GenAllAPReport("PLAN", branchID, sDate, action, filePath);
                        break;
                    
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                // log exception
                System.Console.Write(ex.Message);
            }
        }


        #region 生成大区活动计划报表模板

        private static void GenAllAPReport(string templateType, string branchID, string sDate, string action, string filePath)
        {

            string returnVal = "";
            if (filePath == "")
                filePath = GetTempatePath();
            DateTime date = DateTime.Today;
            
            if (sDate != "")
                date = DateTime.Parse(sDate);

            if (action == "")
            {
                System.Console.WriteLine("Start to generate template");
                ExcelWrapper ew = new ExcelWrapper();
                ew.CallGenerateTemplate(templateType, branchID, filePath, date);
                returnVal = ew.ReturnValue;
                System.Console.WriteLine("Generate template successfully");
            }
            else
            {
                if (System.IO.File.Exists(filePath))
                {
                    System.Console.WriteLine("Start to import template");
                    ExcelWrapper ew = new ExcelWrapper();
                    ew.CallImportTemplate(templateType, branchID, filePath, date);
                    returnVal = ew.ReturnValue;
                    System.Console.WriteLine("Import template successfully");
                }
                else
                {
                    System.Console.WriteLine("The specified file does not exists");
                }
            }
        }

        private static string GetTempatePath()
        {
            string templatePath = ConfigurationSettings.AppSettings["TemplateConfigPath"].ToString().Trim();

            if (templatePath == "")
            {
                templatePath = @"C:\Windows\Temp\";
            }
            else
            {
                if (!templatePath.EndsWith(@"\"))
                {
                    templatePath = templatePath + @"\";
                }
                if (!System.IO.Directory.Exists(templatePath))
                    System.IO.Directory.CreateDirectory(templatePath);
            }

            return templatePath;
        }


        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions