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

  • 相关阅读:
    财务系统重复付款case分析及解决方案
    MySQL体系结构
    安装篇九:安装wordpress(5.4版本)
    安装篇八:配置 Nginx 使其支持 MySQL 应用
    安装篇七:配置 Nginx 使其支持 PHP 应用
    安装篇六:安装PHP(7.2.29版本)
    安装篇五:安装MySQL(5.6.38版本)
    安装篇四:安装NGINX(1.4.0版本)
    安装篇三:系统初始化设置
    安装篇二:CentOS 6.9系统安装
  • 原文地址:https://www.cnblogs.com/zhanghaichang/p/1967271.html
Copyright © 2011-2022 走看看