;
帖子
3919 
精华
积分
11710 
鱼币
0 ¥ 
来自
大连 
在线时间
1858 小时 
注册时间
2007-6-11 
最后登录
2009-10-20 

贴图大师勋章新人进步勋章宣传大使勋章论坛元老勋章

3
发表于 2009-1-5 19:48 | 只看该作者
Reading an Excel file in C# - 缂栫▼搴 - Powered by SupeSite

浣犵殑浣嶇疆锛缂栫▼搴 >> 璧勮 >> CSharp >> CSharp鏂囨。 >> 璇︾粏鍐呭 鍦ㄧ嚎鎶曠ǹ

Reading an Excel file in C#

鍙戝竷: 2008-7-04 11:43 |  浣滆: webmaster |   鏉ユ簮: 鏈珯鍘熷垱 |  鏌ョ湅: 200娆


鎴戞兂瑕侀氳繃C#浠g爜璋冪敤test.xls鐨勫唴瀹癸紝鏇存柊閲岄潰鐨勬暟鎹
鎴戠殑浠g爜濡備笅锛

public class BankParserClass

Excel.Application excelApp = new Excel.ApplicationClass();

Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
( this line of code gets an error    here on excelApp : denotes a fileld where a class is expected)                                                 

string workbookPath = "c:/test.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
      0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
      true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
      string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
Excel.Range excelCell =(Excel.Range)excelWorksheet.get_Range("A1", "A1");

娌℃湁瀹炵幇


瑙e喅鏂规硶
This is application is kind of test application which tells how to use Microsoft Excel 10.0 Object Library, by using this library, how to load/read the excel contents.

Note: Here, I have created test.xls file which will be copied to c:\ before run the application.

Application developed using sample console application under VC# projects.

Steps:

 1. Include the following reference into the project :

  Microsoft Excel 10.0 Object Library

  Microsoft Office 10.0 Object Library

 

2. Include the name space i.e. using Excel.

3. Creating the ExcelApplicationClass,WorkBook and Range.

Find the complete code below:

using System;
using Excel;
namespace TestExcel
{
 /// <summary>
 /// Summary description for ExcelApplication.
 /// </summary>
 class ExcelApplication
 {
      /// <summary>
  /// The main entry point for the application.
  /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
        
         string Path = @"c:\test.xls";
         // initialize the Excel Application class
         Excel.ApplicationClass app = new ApplicationClass();
         // create the workbook object by opening the excel file.
         Excel.Workbook workBook = app.Workbooks.Open(Path,
                                                      0,
                                                      true,
                                                      5,
                                                      "",
                                                      "",
                                                      true,
                                                      Excel.XlPlatform.xlWindows,
                                                      "\t",
                                                      false,
                                                      false,
                                                      0,
                                                      true,
                                                      1,
                                                      0);
         // get the active worksheet using sheet name or active sheet
         Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
         int index = 0;
         // This row,column index should be changed as per your need.
         // i.e. which cell in the excel you are interesting to read.
         object rowIndex = 2;
         object colIndex1 = 1;
         object colIndex2 = 2;
         try
         {
            while ( ((Excel.Range)workSheet.Cells[rowIndex,colIndex1]).Value2 != null )
            {
               rowIndex = 2+index;
               string firstName = ((Excel.Range)workSheet.Cells[rowIndex,colIndex1]).Value2.ToString();
               string lastName = ((Excel.Range)workSheet.Cells[rowIndex,colIndex2]).Value2.ToString();
               Console.WriteLine("Name : {0},{1} ",firstName,lastName);
               index++;
            }
         }
         catch(Exception ex)
         {
            app.Quit();
            Console.WriteLine(ex.Message);
         }
      }
    
 }
}

TAG: Reading Excel file
鎵撳嵃 | 鏀惰棌姝ら〉 |  鎺ㄨ崘缁欏ソ鍙 | 涓炬姤
涓婁竴绡 涓嬩竴绡
 

璇勫垎锛0

鍙戣〃璇勮
鏌ョ湅鍏ㄩ儴鍥炲銆愬凡鏈0浣嶇綉鍙嬪彂琛ㄤ簡鐪嬫硶銆