如何从存储在内存中的excel表中读取值c#

测试员

我正在尝试从 Excel 电子表格中获取单元格的值。该值存储在 sheet1 的单元格 A2 中,该单元格位于名为 Item 的列标题下。下面的代码总是将值返回为 null。请帮我获取存储在 A2 中的值。

using Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

 namespace Test
 {
     public class ExcelUtil
     {
         public static DataTable ExcelToDataTable(string fileName)
         {
             FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
             IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //.xlsx
                        excelReader.IsFirstRowAsColumnNames = true;
              DataSet result = excelReader.AsDataSet();
              DataTableCollection table = result.Tables;
              DataTable resultTable = table["Sheet1"];

              return resultTable;
        }

        public static List<Datacollection> dataCol = new List<Datacollection>();

         public static void PopulateInCollection(string fileName)
         {
             DataTable table = ExcelToDataTable(fileName);
             //Iterate through all the rows and columns of the Table
             for (int row = 1; row <= table.Rows.Count; row++)
             {
                 for (int col = 0; col < table.Columns.Count; col++)
                 {
                     Datacollection dtTable = new Datacollection()
                     {
                         rowNumber = row,
                         colName = table.Columns[col].ColumnName,
                         colValue = table.Rows[row - 1][col].ToString()
                      };
                      //Add all the details for each row
                      dataCol.Add(dtTable);
                   }
               }
           }

       public static string ReadData(int rowNumber, string columnName)
       {
           try
           {
                //Retrieving Data using LINQ
                string data = (from colData in dataCol
                                           where colData.colName == columnName && colData.rowNumber == rowNumber
                                           select colData.colValue).SingleOrDefault();

                //var data = dataCol.Where(x => x.colName == columnName && x.rowNumber == rowNumber).SingleOrDefault().colValue;
                            return data.ToString();
           }
           catch (Exception e)
           {
               e.Message.ToString();
               return null;
            }
        }

        private static void mytest()
        {
            string itemNo = ExcelUtil.ReadData(1, "Item");
         }
    }

public class Datacollection
{
    public int rowNumber { get; set; }
    public string colName { get; set; }
    public string colValue { get; set; }
}
}
测试员

我找到了答案!我需要在上面的代码中添加以下 2 行:

            private static void mytest()
            {
                ExcelUtil util = new ExcelUtil();
                ExcelUtil.PopulateInCollection(@"c:\datalocation\Data.xlsx");

                string itemNo = ExcelUtil.ReadData(1, "Item");
            }

我还必须修改以下内容:

                    public static string ReadData(int rowNumber, string columnName)
            {
                try
                {
                    ////Retrieving Data using LINQ
                    var data = (from colData in dataCol
                                where colData.colName == columnName && colData.rowNumber == rowNumber
                                select colData.colValue).First().ToString();

                    //var data = dataCol.Where(x => x.colName == columnName && x.rowNumber == rowNumber).SingleOrDefault().colValue;
                    return data;
                }
                catch (Exception e)
                {
                    e.Message.ToString();
                    return null;
                }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何分块读取要存储在内存中的文件

来自分类Dev

IEnumerable如何在c#中存储在内存中?

来自分类Dev

c中的整数如何存储在内存中?

来自分类Dev

BASIC,行如何存储在内存中?

来自分类Dev

循环数组如何存储在内存中

来自分类Dev

函数参数如何存储在内存中?

来自分类Dev

循环数组如何存储在内存中

来自分类Dev

函数参数如何存储在内存中?

来自分类Dev

逐行读取文件还是存储在内存中?

来自分类Dev

逐行读取文件还是存储在内存中?

来自分类Dev

如何只访问一行而不将整个表存储在内存中?

来自分类Dev

如何将长度由C中的变量定义的数组存储在内存中?

来自分类Dev

查找存储在内存C / C ++中的值

来自分类Dev

二维数组如何存储在内存中?

来自分类Dev

函数如何在内存中编码/存储?

来自分类Dev

不同类型如何存储在内存中

来自分类Dev

Perl如何在内存中存储整数?

来自分类Dev

双精度浮点数如何存储在内存中?

来自分类Dev

指令和数据如何存储在内存中?

来自分类Dev

如何将DocValues存储在内存/磁盘中?

来自分类Dev

浮点数如何存储在内存中

来自分类Dev

GCC如何在内存中存储成员函数?

来自分类Dev

如何将类变量存储在内存中?

来自分类Dev

C ++异常参数存储在内存中的哪个位置?

来自分类Dev

C ++异常参数存储在内存中的什么位置?

来自分类Dev

如何使用C有选择地将变量存储在内存段中?

来自分类Dev

如何从Firebase数据库读取值并将其存储到HTML表中

来自分类Dev

Lua表如何在内存中处理?

来自分类Dev

如何在内存中容纳大表?