Interop Excel UsedRange Rows Count incorrect

后端 未结 1 1504
借酒劲吻你
借酒劲吻你 2021-01-14 14:28

I am trying to read an excel spreadsheet into memory but when I use worksheet.UsedRange.Rows.Count, the value return is incorrect. I have 1670 rows of data in my spreadsheet

相关标签:
1条回答
  • 2021-01-14 14:49

    Try the following sample code,

    using System;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel; 
    
    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            Excel.Range range ;
    
            string str;
            int rCnt = 0;
            int cCnt = 0;
    
            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    
            range = xlWorkSheet.UsedRange;
    
            rCnt = range.Rows.Count;
            cCnt = range.Columns.Count;
    
            xlWorkBook.Close(true, null, null);
            xlApp.Quit();
    
            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }
    }
    

    Declare variable Excel.Range range and then use it.

    0 讨论(0)
提交回复
热议问题