2016/06/15

C#.Net 取得Excel行列


程式碼:
class Ex{
    private EXCEL.Application _app;
    private EXCEL.Worksheet _sheet;
    private EXCEL.Workbook _workbook;
    
    public Ex(String fileName){
        //調用EXCEL
        this._app = new EXCEL.Application();

        //隱藏EXCEL
        this._app.Visible = false;

        //開啟檔案
        this._workbook = this._app.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing);
        this._sheet = (EXCEL.Worksheet)_workbook.Worksheets[1];
    }
    
    public void Show(){
        //列
        Console.WriteLine(this._sheet.UsedRange.Cells.Columns.Count);
        //行
        Console.WriteLine(this._sheet.UsedRange.Cells.Rows.Count);
    }
}