我正在使用下面的代码从excel文件中选择所有数据,并想知道我是否可以从第三行开始并读取文件中的其余数据..
excelConnectionString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFileName + ";Extended Properties=Excel 12.0";
// Create Connection to Excel Workbook
using (OleDbConnection excelConnection =
new OleDbConnection(excelConnectionString))
{
excelConnection.Open();
System.Data.DataTable dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string[] excelSheet = new String[dt.Rows.Count];
int sheet = 0;
foreach (DataRow row in dt.Rows)
{
excelSheet[sheet] = row["Table_Name"].ToString();
sheet++;
}
excelDataTable.Clear();
for (int i = 0; i < excelSheet.Length; i++)
{
OleDbCommand command = new OleDbCommand
("Select * FROM [" + excelSheet[i] + "]", excelConnection);
excelAdapter.SelectCommand = command;
excelAdapter.Fill(excelDataTable);
}
excelConnection.Close();
}
return excelDataTable;
答案 0 :(得分:0)
在使用Oledbconnection阅读之前,您可以使用类似的内容修改您的文件: -
private string customiseTemplate(string filename)
{
try
{
//Create Instance of Excel Appllication
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
//Create a WorkBook Object.
Workbooks wbks = app.Workbooks;
//Populate Workbook from Excel File.
_Workbook _wbk = wbks.Add(filename);
//Create Sheet Object.
Sheets shs = _wbk.Sheets;
//Determine which sheet to work with. In this instance there should only ever be one.
_Worksheet _wsh = (_Worksheet)shs.get_Item(1);
//Delete Unwanted Rows 1 to 3.
Microsoft.Office.Interop.Excel.Range
range1 = (Range)_wsh.get_Range("A1:A3", Missing.Value);
range1.EntireRow.Delete(Missing.Value);
// Release Range
System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);
//Save WorkBook.
app.AlertBeforeOverwriting = false;
string newFile = filename.Insert(3, "Ammended_");
_wbk.SaveAs(newFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Missing.Value,
Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
// CLose Excel App.
app.Quit();
// Release unnecessary excel processes
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
return newFile;
}
catch(Exception x)
{
MessageBox.Show(x.Message);
}
return null;
}
答案 1 :(得分:0)
var w = sqlData.AsEnumerable().Where(data => data.Field<String>("slideNo") == "5")
.Select(data=> data.Field<String>("QuestionStartText"));
这是编码的答案......
答案 2 :(得分:0)