【NCRE】——Excel之如何获取行高和列宽

       准备开始就目前做的项目开始写系列博客了,希望大家喜欢~

       在操作Excel的时候,如何设置列宽和行高大家还记得吗?还是很简单的对不对?那么你知道Excel的行高和裂宽度的属性Microsoft是怎么命名的吗?用用大家熟悉的英语啦~Yes!RowHeight and ColumnWidth。那么这样就简单了,获取一个Excel的固定位置上的单元格行高和列宽的代码如下:

       首先打开Excel:

	/// 
        /// 打开Excel工作表
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            string mytype = "A";
            m_workbook = m_excel.Workbooks.Open(
               @"D:\NCRE考生文件\Excel\Excel15" + mytype +".xlsx",
               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);
            m_excel.Visible = true;
        }


    上边的代码简单的理解一下,就是找到需要的Excel的路径,然后打开就OK啦~

        那么现在就是获取Excel的行高和列宽的代码了:

	 private void button8_Click(object sender, EventArgs e)
        {
            //获取工作表
            MSExcel.Worksheet sheet1 = m_workbook.ActiveSheet as MSExcel.Worksheet;
            double rowheight = m_excel.Range["A2"].RowHeight;  //单元格行高
            double cellwidth = m_excel.Range["A1"].ColumnWidth;  //单元格列宽
            MessageBox.Show("单元格A1的行高为:" + rowheight);
            MessageBox.Show("单元格A1的列宽为:" + cellwidth );
        }
         从上述代码我们可以知道A2的行高,A1的列宽,看成果:

【NCRE】——Excel之如何获取行高和列宽_第1张图片【NCRE】——Excel之如何获取行高和列宽_第2张图片

           Demo实现的:

【NCRE】——Excel之如何获取行高和列宽_第3张图片【NCRE】——Excel之如何获取行高和列宽_第4张图片

        

         嘻嘻,出来了~期待下一个例子吧~



你可能感兴趣的:(●,项目管理,--------【NCRE】)