我试图在将数据写入Excel工作表时显示进度条。但无法表现出来。
try
{
ShowProgressBarForExcelSheet();
i18NText = I18N.Xlate("PREPARING_EXCEL_SHEET", "Excel-Tabelle ist immer vorbereitet...", null, Assembly.GetExecutingAssembly());
oProgressBarFO.UpdateLogSearchStatus(i18NText);
workSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkBook.Sheets[1];
excel.ActiveSheet.Cells[headerRow, 1] = "Datum/Zeit";
excel.ActiveSheet.Cells[headerRow, 3] = "Arbeitsplatz";
excel.ActiveSheet.Cells[headerRow, 5] = "Benutzername";
excel.ActiveSheet.Cells[headerRow, 7] = "Aktion";
excel.ActiveSheet.Cells[headerRow, 9] = "Zugriffstype";
excel.ActiveSheet.Cells[headerRow, 11] = "Klartext";
excel.ActiveSheet.Cells[headerRow, 13] = "Objekt Type";
excel.ActiveSheet.Cells[headerRow, 15] = "Patienten-Nr.";
excel.ActiveSheet.Cells[headerRow, 17] = "Fall-Nr.";
excel.ActiveSheet.Cells[headerRow, 19] = "Vorgangs-Nr.";
excel.ActiveSheet.Cells[headerRow, 21] = "Objekt Value";
}
有什么方法可以在将数据写入Excel工作表时显示进度条吗?用户无法编辑Excel工作表或excel工作表必须为用户禁用,直到所有数据都填入excel
答案 0 :(得分:3)
您可以使用BackgroundWorker有效地实现此目的。这里附有一个示例,说明如何在C#中执行此操作:
private void button1_Click(object sender, EventArgs e)
{
int ProgressToBeupdated = 100;
BackgroundWorker WorkerThread = new BackgroundWorker();
WorkerThread.WorkerReportsProgress = true;
WorkerThread.DoWork += WorkerThread_DoWork;
WorkerThread.ProgressChanged += WorkerThread_ProgressChanged;
WorkerThread.RunWorkerAsync(new object());
}
void WorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
void WorkerThread_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(500);
(sender as BackgroundWorker).ReportProgress(i);
}
}
在这个示例中,我使用的是一个按钮和一个进度条控件,它显示每半秒后的进度(基本上是让线程进入睡眠状态,直到这只是一个样本)。
因此,您可以为DoWork设置需要将数据写入Excel工作表的事件,在那里您可以计算任务后的百分比,然后使用ReportProgress(int percentage)更新要显示的百分比进度条。
您需要定义ProgressChanged事件,以便在dowork事件处理程序中调用ReportProgress函数时更新进度条中的值。
答案 1 :(得分:0)
**Displaying progress bar while inserting multiple records to database.**
progressBar1.Maximum = dtTable.Rows.Count;
progressBar1.Value = progressBar1.Minimum;
foreach (DataRow vRow in dtTable.Rows)
{
//TODO : Your INSERT, UPDATE, DELETE Or any Operation/Execution
progressBar1.Value++;
}