使用以下代码找到每个匹配项时,我想将符合条件的完整Excel行添加到datagridview
通过将匹配找到的行号12传递给查询中的参数,但我收到一条错误消息
查找工作正常,但不会向private void B_General_Search_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oWB;
Microsoft.Office.Interop.Excel.Range currentFind = null;
Microsoft.Office.Interop.Excel.Range firstFind = null;
string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DefaultDirectory + @"\new29.xlsx" + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
if (string.IsNullOrEmpty(TB_Search_Text.Text))
{
MessageBox.Show("enter text for search");
return;
}
object missing = System.Reflection.Missing.Value;
oWB = oXL.Workbooks.Open(DefaultDirectory + @"\new29.xlsx", //---Filename OR FilePath
0, //---object UpdateLinks
true, //---object ReadOnly
missing, //5//---object Format
"", //---object Password
"", //---object WriteResPassword
false, //---object ReadOnlyRecommend
Excel.XlPlatform.xlWindows, //---object Origin
"", //---object Delimiter
true, //---object Editable
false, //---object Notify
0, //---object Converter
true, //---object AddToMru
false, //---object Local
false); //---object CorruptLoad;
//loop to search witin all excel sheets (workbook)
foreach (Excel.Worksheet SheetID in oWB.Worksheets)
{
Excel.Range oRng = oXL.get_Range("A1", "XFD1048576");
currentFind = oRng.Find(TB_Search_Text.Text,
missing,
Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows,
Excel.XlSearchDirection.xlNext,
false,
missing,
missing);
while (currentFind != null)
{
//Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
else if (currentFind.get_Address(true, true, Excel.XlReferenceStyle.xlR1C1) == firstFind.get_Address(true, true, Excel.XlReferenceStyle.xlR1C1))
{
break;
}
currentFind = oRng.FindNext(currentFind);
string cmdtxt = @"SELECT * FROM [" + SheetID.Name + "$] WHERE R=R12";
MessageBox.Show(cmdtxt);
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbDataAdapter DA = new OleDbDataAdapter(cmdtxt, conn);
conn.Open();
//da.Parameters.Add(new OleDbParameter("@rowid", OleDbType.VarChar)).Value = "R12";
MessageBox.Show(cmdtxt);
DA.Fill(dt);
DGV_Data.DataSource = dt;
conn.Close();
}
}
}
oWB.Close(false, missing, missing);
oWB = null;
oXL.Quit();
}
没有给出一个或多个必需参数的值。
string cmdtxt = @"SELECT * FROM [" + SheetID.Name + "$] WHERE R=@rowid";
MessageBox.Show(cmdtxt);
using (OleDbConnection conn = new OleDbConnection(ConnStr))
{
OleDbDataAdapter DA = new OleDbDataAdapter();
OleDbCommand cmd = new OleDbCommand(cmdtxt,conn);
cmd.Parameters.Add("@rowid", OleDbType.VarChar).Value = "R12";
DA.SelectCommand = cmd;
//conn.Open();
MessageBox.Show(cmdtxt);
DA.Fill(dt);
DGV_Data.DataSource = dt;
conn.Close();
}
我也试过这种方式
import { Component, OnInit } from '@angular/core';
import { BarComponent } from '../bar/bar.component';
@Component({
selector: 'app-foo',
templateUrl: './foo.component.html'
})
export class FooComponent implements OnInit {
barComponentPath;
constructor() { }
ngOnInit() {
this.barComponentPath = ""; // ?
}
}