[英]Excel Data Reader Issues, column Names, and Sheet Selection
I am using Excel Data Reader to read some data in to an Entity Framework Database 我正在使用Excel Data Reader将一些数据读入实体框架数据库
The code below is working but i need some further refinements 下面的代码正在运行,但我需要进一步改进
First of all IsFirstRowAsColumnNames does not seem to be working as intended and I have to use .Read instead. 首先,IsFirstRowAsColumnNames似乎没有按预期工作,我必须使用.Read。
The fudge i had in originally to select a particular sheet was has scuppered plans, can anyone help with this excelReader.Name at the moment is pointless unless i can specifically loop through or select a sheet, which I originally used .Read to achieve hence the conflict. 我最初选择特定纸张时的软糖是已经破坏的计划,任何人都可以帮助这个excelReader.Name目前是没有意义的,除非我可以专门循环或选择我最初使用的工作表。阅读因此实现冲突。
It would also be nice to refer to the actual column header names to retrieve the data rather than indexes such as var name = reader["applicationname"].ToString() in SQL client; 引用实际的列标题名称来检索数据而不是索引,例如var name = reader [“applicationname”]。SQL客户端中的ToString();
Is there perhaps a better Extension i could use to read in excel data if i can't achieve the above. 如果我无法实现上述目标,是否可以使用更好的扩展我可以用于读取Excel数据。
public static void DataLoadAliases(WsiContext context)
{
const string filePath = @"Alias Master.xlsx";
var stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
var excelReader = filePath.Contains(".xlsx")
? ExcelReaderFactory.CreateOpenXmlReader(stream)
: ExcelReaderFactory.CreateBinaryReader(stream);
excelReader.IsFirstRowAsColumnNames = true;
excelReader.Read(); //skip first row
while (excelReader.Read())
{
if (excelReader.Name == "Alias Master")
{
var aliasId = excelReader.GetInt16(0);
var aliasName = excelReader.GetString(1);
//Prevent blank lines coming in from excel;
if (String.IsNullOrEmpty(aliasName)) continue;
context.Aliases.Add(new ApplicationAlias
{
AliasId = aliasId,
Name = aliasName,
});
}
else
{
excelReader.NextResult();
}
}
excelReader.Close();
context.SaveChanges();
}
for .XLSX file i use OpenXML SDK : http://www.microsoft.com/en-us/download/details.aspx?id=30425 对于.XLSX文件我使用的是OpenXML SDK: http : //www.microsoft.com/en-us/download/details.aspx?id = 30425
for XLS file i use a OleDbConnection as see below : 对于XLS文件,我使用OleDbConnection,如下所示:
OleDbConnection oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath+ ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1;';");
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt = oledbConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string workSheetName = (string)dt.Rows[0]["TABLE_NAME"];
cmd.Connection = oledbConn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [" + workSheetName + "]";
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds, "Donnees");
oledbConn.Close();
return ds.Tables[0];
DataTable DT = new DataTable();
FileStream stream = File.Open(Filepath, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
DataSet result = excelReader.AsDataSet();
excelReader.Close();
DT = result.Tables[0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.