[英]c# IExcelDataReadReader and a specific Sheet
I have a program that needs to read an excel file and write it to sql server.I am using IExcelDataReader and my code works as long as I have one worksheet. 我有一个程序需要读取一个Excel文件并将其写入sql server.I正在使用IExcelDataReader,只要我有一个工作表,我的代码就可以工作。 If I have another that for an example has some information that I don't need to insert,I can't specific a sheet that I need.
例如,如果我有另一个不需要插入的信息,则无法指定所需的工作表。 I know that I should probably use NextRecord() somehow,but I can't make it work.
我知道我可能应该以某种方式使用NextRecord(),但是我无法使其工作。
This is my code so far: 到目前为止,这是我的代码:
OpenFileDialog ope = new OpenFileDialog();
ope.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
if (ope.ShowDialog() == false)
return;
FileStream stream = new FileStream(ope.FileName, FileMode.Open);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
DataClasses1DataContext conn = new DataClasses1DataContext();
foreach (DataTable table in result.Tables)
{
foreach (DataRow dr in dd.Rows)
{
Excel addTable = new Excel()
{
id = Convert.ToInt32(dr[0]),
ime = Convert.ToString(dr[1])
};
conn.ExecuteCommand("TRUNCATE TABLE Excel");
conn.Excels.InsertOnSubmit(addTable);
}
}
conn.SubmitChanges();
excelReader.Close();
stream.Close();
MessageBox.Show("success!");
I found the aswer.. in the foreach there should be only: 我发现aswer ..在foreach中应该只有:
foreach (DataRow dr in result.Tables[0].Rows)
{
Excel addTable = new Excel()
{
ime = Convert.ToString(dr[1])
};
// conn.ExecuteCommand("TRUNCATE TABLE Excel");
conn.ExecuteCommand("DELETE FROM Excel where ime='a'");
conn.Excels.InsertOnSubmit(addTable);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.