Wednesday, June 25, 2014

example C# and EXCEL code

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

namespace ClassCodeShim
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open("C:\\Users\\leem\\TEST_DATA\\17_HHSC_Data\\TO-EMPLOYER-ARCHIVE\\HHSC_PIF_CLS_CD_RPT_20121130.xls.sent");

            int sheet_ii = 0;
            foreach (Worksheet sheet in wb.Sheets)
            {
                Console.WriteLine(string.Format("sheet{0}: {1}", sheet_ii, sheet.Name));

                foreach (Microsoft.Office.Interop.Excel.Range row in sheet.UsedRange.Rows)
                {
                    int rownum = row.Row;
                    foreach (Range cell in row.Cells)
                    {
                        Console.Write(string.Format("{0}={1} ", cell.get_Address(), cell.get_Value()));
                    }
                    Console.WriteLine();
                }

                sheet_ii++;
                Console.WriteLine();
                Console.WriteLine();
            }
        }//Main
    }//class Program
}//namespace ClassCodeShim

No comments:

Post a Comment