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单元格没有被合并。");
                        }
                    }
                }
            }
        }
    }

  • 相关阅读:
    【剑指offer】3-数组中重复的数字
    自定义管道遇到问题解决方案
    vue的双向绑定示例
    子组件传递给父组件数据
    vue里的共享对象示例
    mysql里的explain介绍
    mysql索引
    修改MySQL字符集
    v-model修饰符示例
    vue下拉列表示例
  • 原文地址:https://www.cnblogs.com/zhanghaichang/p/1967271.html
Copyright © 2011-2022 走看看