Rin*_*lar 1 c# excel asynchronous winforms
我有一个excel表,上面有各种工具箱项目.我有一个下拉菜单,允许用户选择一个值并点击"运行"按钮(我的按钮1),并且一切正常,假设数据在Excel工作表内并且当前是手动完成的.但是,如果我想添加第二个按钮(button2),它将自动从SQL Server"刷新"数据,它会运行......但不是异步的.这是我的button2:
private async void button2_Click(object sender, EventArgs e)
{
var Excel = Globals.ThisWorkbook.Application;
var activebook = Excel.ActiveWorkbook;
var ws = Excel.ActiveSheet;
SQLServer server = new SQLServer();
int t = await Task.Run(() => server.getInfo(activebook, ws, Excel));
}
Run Code Online (Sandbox Code Playgroud)
代码运行正常,但我不想让用户等待在Excel窗口响应之前填充6000行Excel工作表.我在这里做错了吗?
我为这个混乱道歉,我对这个网站还很新,并不打算引起太多混乱.我正在尝试运行此方法:
public void getInfo(Workbook book, Worksheet activesheet, Microsoft.Office.Interop.Excel.Application app)
{
SqlConnection cnn;
string connectionstring = "#########";
string sql = null;
////***Opening SQL Database
connectionstring = "Data Source=########;Initial Catalog=BrightreeData;Persist Security Info=True;User ID=######;Password=######";
cnn = new SqlConnection(connectionstring);
cnn.Open();
activesheet.Range["K" + 1].Value = "Connected";
////** Write your Sql Query here
sql = "SELECT [StopReason], [SOKey], [NickName] FROM [BrightreeData].[dbo].[SalesOrder] Where [StopReason] = 'Ineligible Policy' ORDER BY [SOKey]";
///*** Preparing to retrieve value from the database
SQL.DataTable dtable = new SQL.DataTable();
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
SQL.DataSet ds = new SQL.DataSet();
dscmd.Fill(dtable);
int count = dtable.Rows.Count;
for (int i = 0; i < dtable.Rows.Count; i++)
{
try
{
check: activesheet.Range["C" + 1].Value = (i + 1) + " out of " + count + " rows";
soService.DataFetchServiceResponseUsingSalesOrder salesorder = soAction.SalesOrderFetchByBrightreeID(dtable.Rows[i]["SOKey"].ToString());
activesheet.Range["J" + (8 + i)].Value = dtable.Rows[i]["NickName"];
activesheet.Range["A" + (8 + i)].Value = salesorder.Items[0].SalesOrderInsuranceInfo.Payors[1].payorPolicyInfo.Name.ToString();
activesheet.Range["D" + (8 + i)].Value = salesorder.Items[0].SalesOrderInsuranceInfo.Payors[1].payorPolicyInfo.PolicyNumber;
//If the current row has the same policy as the last row, it is a repeat and nothing worth grabbing that data.
//
// Eventually add something at the end of the program that deletes rows that are blank?...for now I will manually delete empty rows.
if(activesheet.Range["D" + (8 + i)].Value == activesheet.Range["D" + (7 + i)].Value & activesheet.Range["D" + (8 + i)].Text != "")
{
activesheet.Range["C" + (8 + i)].Value = "Repeat Patient";
count--;
i++;
goto check;
}
activesheet.Range["J" + (8 + i)].Value = "St Lukes";
activesheet.Range["K" + (8 + i)].Value = System.DateTime.Today.Date;
string ptID = Convert.ToString(salesorder.Items[0].SalesOrderClinicalInfo.Patient.BrightreeID);
ptService.DataFetchServiceResponseUsingPatient ptSearch = ptAction.PatientFetchByBrightreeID(ptID);
activesheet.Range["B" + (8 + i)].Value = ptSearch.Items[0].PatientGeneralInfo.Name.First;
activesheet.Range["C" + (8 + i)].Value = ptSearch.Items[0].PatientGeneralInfo.Name.Last;
activesheet.Range["E" + (8 + i)].Value = ptSearch.Items[0].PatientGeneralInfo.BirthDate;
activesheet.Range["L" + (8 + i)].Value = ptSearch.Items[0].PatientGeneralInfo.PtID;
}catch
{
activesheet.Range["B" + (8 + i)].Value = "Error getting patient information";
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用此电话:
private async void button2_Click(object sender, EventArgs e)
{
var Excel = Globals.ThisWorkbook.Application;
var activebook = Excel.ActiveWorkbook;
var ws = Excel.ActiveSheet;
SQLServer server = new SQLServer();
await Task.Run(() => server.getInfo(activebook, ws, Excel));
}
Run Code Online (Sandbox Code Playgroud)
在我的button2_Click()方法的最后一行抛出一个错误.
An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'
occurred in System.Core.dll but was not handled in user code
Additional information: Cannot implicitly convert type 'type' to 'object'
Run Code Online (Sandbox Code Playgroud)
试试这个:
await Task.Run((Action)(() => server.getInfo(activebook, ws, Excel)));
Run Code Online (Sandbox Code Playgroud)
由于某种原因(可能是因为您传递的参数是COM对象),系统会将参数处理为动态,因此会错误地认为您传递的委托将返回一个值.
如果您Task.Run在visual studio中移动鼠标,您应该看到系统正在尝试调用此重载:
Task<TResult> Run<TResult>(Func<TResult> function)
Run Code Online (Sandbox Code Playgroud)
哪里TResult是dynamic.
| 归档时间: |
|
| 查看次数: |
306 次 |
| 最近记录: |