zoukankan      html  css  js  c++  java
  • Aspose.Cells 使用整理:读取Excel文件里的数据

    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>
            /// 默认构造函数.<br></br>
            /// 2009-04-13 YJ 定义函数.<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的数据:" + sExcelValue);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("工作表 \"" + tcSheetName + "\" a2单元格不存在或者已经被合并:" + 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单元格没有被合并。");
                        }
                    }
                }
            }
        }
    }

  • 相关阅读:
    【Android 工具类】经常使用工具类(方法)大全
    driver: Linux设备模型之input子系统具体解释
    ural 1057 Amount of degrees 【数位dp】
    Java8 Lambda表达式教程
    Java线程池
    NodeJS实战——创建基础应用并应用模板引擎
    【网络】代理服务器
    【HTTP】Wireshark过滤规则
    【HTTP】WireShark中获取Content-Encoding: gzip时的响应内容
    【python】判断字符串日期是否有效
  • 原文地址:https://www.cnblogs.com/zhanghaichang/p/1967271.html
Copyright © 2011-2022 走看看