C# 如何从存储在内存c中的excel表中读取值#
我试图从Excel电子表格中获取单元格的值。该值存储在sheet1单元格A2中,该单元格位于名为Item的列标题下。下面的代码总是将值返回为null。请帮我获取A2中存储的值C# 如何从存储在内存c中的excel表中读取值#,c#,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.Task
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; }
}
}
使用Excel;
使用制度;
使用System.Collections.Generic;
使用系统数据;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间测试
{
公共类ExcelUtil
{
公共静态数据表ExcelToDataTable(字符串文件名)
{
FileStream stream=File.Open(文件名,FileMode.Open,FileAccess.Read);
IExcelDataReader excelReader=ExcelReaderFactory.CreateOpenXmlReader(流);/.xlsx
excelReader.IsFirstRowAsColumnNames=true;
数据集结果=excelReader.AsDataSet();
DataTableCollection table=result.Tables;
DataTable Resultable=表[“Sheet1”];
返回结果;
}
公共静态列表数据列=新列表();
公共静态void PopulateInCollection(字符串文件名)
{
DataTable=ExcelToDataTable(文件名);
//遍历表中的所有行和列
对于(int row=1;row x.colName==columnName&&x.rowNumber==rowNumber);
返回data.ToString();
}
捕获(例外e)
{
e、 Message.ToString();
返回null;
}
}
私有静态void mytest()
{
字符串itemNo=ExcelUtil.ReadData(1,“Item”);
}
}
公共类数据收集
{
公共整数行号{get;set;}
公共字符串colName{get;set;}
公共字符串colValue{get;set;}
}
}
该代码将在这方面对您有很大帮助
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
namespace EXEL_DB
{
public partial class Form1 : Form
{
OleDbConnection coon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=""C:\A.xls""; Extended properties=""Excel 8.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""");
OleDbCommand com;
DataTable dt = new DataTable();
OleDbDataAdapter da;
public Form1()
{
coon.Open();
fillgrilll("");
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataView dv = new System.Data.DataView(dt);
dv.RowFilter = "group by [langue com] order by [langue com]";
dataGridView1.DataSource = dv;
}
void fillgrilll(string re)
{
try
{
dt.Clear();
// My sheet i name it A but you have to write [A$]
da = new OleDbDataAdapter("select * from [A$] where datepart(month,[Shipment Date]) = datepart(month,'06/04/2016')", coon);
da.Fill(dt);
//dataGridView1.DataSource = dt;
}
catch {
MessageBox.Show("Errr");
}
}
}
}
`我找到了答案!我需要在上面的代码中添加以下两行:
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;
}
哪个方法返回null?您是否尝试过调试代码并了解它返回null的原因?我应该如何在我的机器上运行此代码?我应该调用哪种方法?@ChetanRanpariya您需要在PC上本地保存一个excel工作表,其中有几行,第一行有标题,您可以在第二行中放入任何内容。是的,我尝试过调试,但得到了相同的错误:可结果性错误CS0103:名称“可结果性”在当前上下文中不存在。我忘了将该文件名添加到初始化类中:ExcelUtil.PopulateInCollection(@“Data.xlsx”);应用程序无法获取itemno字段的字符串(它是excel电子表格中的A2),您的代码是否编译?您首先说它返回null。您是在运行时还是在编译时遇到此错误?哪一行显示错误?@ChetanRanpariya是的,它会编译。但是返回的值为null。