原創(chuàng)|其它|編輯:郝浩|2012-10-12 15:22:04.000|閱讀 6546 次
概述:Aspose.Cells控件是一個功能非常強大的控件。由于Aspose.Cells控件的功能很多和我個人的時間關系,這都是我抽下班時間連困帶餓寫的,哎!...... 所以在此我只寫了指定讀一個單元格的數(shù)據(jù)(此代碼指定讀B3單元格的數(shù)據(jù)),要學習的同志請到慧都控件網(wǎng)上找相關的資料。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Cells控件是一個功能非常強大的控件。由于Aspose.Cells控件的功能很多和我個人的時間關系,這都是我抽下班時間連困帶餓寫的,哎!...... 所以在此我只寫了指定讀一個單元格的數(shù)據(jù)(此代碼指定讀B3單元格的數(shù)據(jù)),要學習的同志請到慧都控件網(wǎng)上找相關的資料。
要想使用Aspose.Cells控件,必須先購買此控件,然后還要在工程中引用此控件和使用的文件中using namespace,因為C#中的方法在同一個命名空間中才有權訪問。接下來編寫代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using Aspose.Cells;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 默認構造函數(shù).<br></br>
        /// 2009-04-13 YJ 定義函數(shù).<br></br>
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            //此處的students.xls文件在工程下Bin的程序集目錄下
            string opnFileName = Application.StartupPath.Trim() + "\\students.xls";
            if(!string.IsNullOrEmpty(opnFileName))
            {
                Workbook tcWorkBook = new Workbook();
                tcWorkBook.Open(opnFileName);
                Worksheets tcWorkSheets = tcWorkBook.Worksheets;
                Worksheet tcWorkSheet;
                Cells tcCells;
                //索引行號
                //int tcRow = 0;
                //索引列號
                //int tcColumn = 0;
                Range tcRange;
                string sExcelValue = "";
                Cell tcCell;
                for (int i = 0; i < tcWorkSheets.Count; i++)
                {
                    tcWorkSheet = tcWorkSheets[i];
                    tcCells = tcWorkSheet.Cells;
                    //以索引的方式遍歷工作表
                    //tcCell = tcCells[tcRow, tcColumn];
                    tcCell = tcCells["a2"];
                    string tcSheetName = tcWorkSheets[i].Name;
                    try
                    {
                        // sExcelValue = tcCell.Value.ToString();
                        sExcelValue = tcCell.StringValue;    //可以獲取空值
                        MessageBox.Show("工作表 \"" + tcSheetName + "\" a2的數(shù)據(jù):" + sExcelValue);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("工作表 \"" + tcSheetName + "\" a2單元格不存在或者已經(jīng)被合并:" + ex.Message);
                    }
                    if (tcCell.IsMerged)
                    {
                        tcRange = tcCell.GetMergedRange();
                        MessageBox.Show("工作表 \"" + tcSheetName + "\" a2單元格合并了 " + tcRange.RowCount.ToString() + " 行。");
                        MessageBox.Show("工作表 \"" + tcSheetName + "\" a2單元格合并了 " + tcRange.ColumnCount.ToString() + " 列。");
                    }
                    else
                    {
                        MessageBox.Show("工作表 \"" + tcSheetName + "\" a2單元格沒有被合并。");
                    }
                }
            }
        }
    }
}
					本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:網(wǎng)絡轉(zhuǎn)載