Thursday, June 26, 2014

example C# and EXCEL 2

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;

namespace ClassCodeShim
{
    class ExcelFileCheck
    {
        public int sheetcount;
        public string[] sheetnames;
        public int[] columncountpersheet;
        public List<string>[] columnnamespersheet;
    }//class ExcelFileCheck

    class Program
    {
        static void ScanDirectory(List<string> keeplist, string searchpath)
        {
            try
            {
                List<string> folderfiles = new List<string>(Directory.EnumerateFiles(searchpath));
                foreach (string myfile in folderfiles)
                {
                    string allupperstr = myfile.ToUpper();
                    if (allupperstr.EndsWith(".SENT"))
                        keeplist.Add(allupperstr);
                }

                List<string> folderdirs = new List<string>(Directory.EnumerateDirectories(searchpath));
                foreach (string mydir in folderdirs)
                {
                    ScanDirectory(keeplist, mydir);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside ScanDirectory an exception was caught!");
                Console.WriteLine(e.Message);
            }
        }//ScanDirectory

        static void GetSentFilesInfo(List<string> keeplist, string[] createtime, string[] modtime)
        {
            try
            {
                for (int ii = 0; ii < keeplist.Count; ii++ )
                {
                    string excelfilepath = keeplist[ii];
                    FileInfo myfileinfo = new FileInfo(excelfilepath);
                    createtime[ii] = myfileinfo.CreationTime.ToLongDateString() + " " + myfileinfo.CreationTime.ToLongTimeString();
                    modtime[ii] = myfileinfo.LastWriteTime.ToLongDateString() + " " + myfileinfo.LastWriteTime.ToLongTimeString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside GetSentFilesInfo an exception was caught!");
                Console.WriteLine(e.Message);
            }
        }//GetFileInfo

        static void DebugGetExcelFileCheck(string excelfilepath, ExcelFileCheck excelcheck)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(excelfilepath);

                excelcheck.sheetcount = wb.Sheets.Count;
                excelcheck.sheetnames = new string[wb.Sheets.Count];
                excelcheck.columncountpersheet = new int[wb.Sheets.Count];
                excelcheck.columnnamespersheet = new List<string>[wb.Sheets.Count];

                int sheet_idx = 0;
                foreach (Worksheet sheet in wb.Sheets)
                {
                    excelcheck.sheetnames[sheet_idx] = sheet.Name;
                    Range excelrange = sheet.UsedRange;
                    object[,] mycells = (object[,])excelrange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
                    //Console.WriteLine(excelfilepath);
                    //Console.WriteLine(string.Format("SHEET{0}: {1}", sheet_idx, sheet.Name));
                    //Console.WriteLine(string.Format("ROWS={0} COLS={1}", excelrange.Rows.Count, excelrange.Columns.Count));
                    if (mycells == null)
                    {
                        //Console.WriteLine("mycells is null");
                        excelcheck.columncountpersheet[sheet_idx] = 0;
                        excelcheck.columnnamespersheet[sheet_idx] = null;
                    }
                    else
                    {
                        //Console.WriteLine("mycells is not null");
                        excelcheck.columncountpersheet[sheet_idx] = excelrange.Columns.Count;
                        excelcheck.columnnamespersheet[sheet_idx] = new List<string>();

                        for (int col = 1; col <= excelrange.Columns.Count; col++)
                        {
                            if (mycells[1, col] == null)
                                excelcheck.columnnamespersheet[sheet_idx].Add("NULL");
                            else
                            {
                                string temp = mycells[1, col].ToString();
                                string tempupper = temp.ToUpper();
                                excelcheck.columnnamespersheet[sheet_idx].Add(tempupper);
                            }
                        }

                        for (int rr = 1; rr <= excelrange.Rows.Count; rr++)
                        {
                            for (int cc = 1; cc <= excelrange.Columns.Count; cc++)
                            {
                                //if (mycells[rr, cc] == null)
                                //    Console.Write("NULL ");
                                //else
                                //    Console.Write(mycells[rr, cc].ToString() + " ");
                            }
                            //Console.WriteLine();
                        }
                    }

                    sheet_idx++;
                }
                wb.Close();
                app.Quit();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside GetExcelFileCheckInfo an exception was caught!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }//DebugGetExcelFileCheck

        static void DebugPrintExcelFileCheck(string excelfilepath, ExcelFileCheck excelcheck)
        {
            try
            {
                Console.WriteLine("SENT = {0}", excelfilepath);
                Console.WriteLine("sheetcount = {0}", excelcheck.sheetcount);
                for (int ss = 0; ss < excelcheck.sheetcount; ss++)
                {
                    Console.WriteLine("sheetname = {0}", excelcheck.sheetnames[ss]);
                    Console.WriteLine("columncount = {0}", excelcheck.columncountpersheet[ss]);
                    for (int cc=0; cc < excelcheck.columnnamespersheet[ss].Count; cc++)
                    {
                        string tempstr = string.Format("{0}: {1}", cc, excelcheck.columnnamespersheet[ss][cc]);
                        Console.WriteLine(tempstr);
                    }
                }
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside DebugPrintExcelFileCheck an exception was caught!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }//DebugPrintExcelFileCheck

        static void ReadExcelFile(string excelfilepath)
        {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(excelfilepath);

            int sheet_ii = 0;
            foreach (Worksheet sheet in wb.Sheets)
            {
                Console.WriteLine(string.Format("sheet{0}: {1}", sheet_ii, sheet.Name));
                int rows_count = sheet.UsedRange.Rows.Count;
                int cols_count = sheet.UsedRange.Columns.Count;
                Console.WriteLine(string.Format("rows={0} cols={1}", rows_count, cols_count));
                Range cellA1 = sheet.get_Range("A1");
                if (rows_count == 1 && cols_count == 1 && cellA1.get_Value() == null)
                {
                    Console.WriteLine("EMPTY SHEET");
                }
                else
                {
                    foreach (Microsoft.Office.Interop.Excel.Range row in sheet.UsedRange.Rows)
                    {
                        int rownum = row.Row;
                        foreach (Range cell in row.Cells)
                        {
                            if (cell.get_Value() == null)
                                Console.Write(string.Format("{0}=NULL ", cell.get_Address()));
                            else
                                Console.Write(string.Format("{0} ", cell.get_Value()));
                        }
                        Console.WriteLine();
                    }
                }

                sheet_ii++;
                Console.WriteLine();
                Console.WriteLine();
            }

            wb.Close();
            app.Quit();
        }//ReadExcelFile

        static void Main(string[] args)
        {
            List<string> sentpath = new List<string>(); //contains sentfile paths
            ScanDirectory(sentpath, "C:\\Users\\leem\\TEST_DATA");

            string[] sentcreate = new string[sentpath.Count];   //contains sentfile creation date and time
            string[] sentmod = new string[sentpath.Count];      //contains sentfile modified date and time
            GetSentFilesInfo(sentpath, sentcreate, sentmod);

            //ExcelFileCheck excelcheck = new ExcelFileCheck();
            //DebugGetExcelFileCheck("C:\\Users\\leem\\TEST_DATA\\30_HAWAII_Data\\TO-EMPLOYER-ARCHIVE\\HCLASSCODEREPORT-20120416LKO.XLS.sent", excelcheck);

            ReadExcelFile("C:\\Users\\leem\\TEST_DATA\\30_HAWAII_Data\\TO-EMPLOYER-ARCHIVE\\HCLASSCODEREPORT-20120416LKO.XLS.sent");

            Console.WriteLine("DONE");
        }//Main
    }//class Program
}//namespace ClassCodeShim

No comments:

Post a Comment