**我已经完成了将数据从数据库导出到文件的部分,但是我发现有时等待这个动作完成需要很长时间。所以我想在我的程序中添加一个进度条,但这是我的问题:
我不知道 xx.xls 文件有多大
我不知道如何获取已导出文件大小的阶段
所以我不知道如何计算进度条的百分比。我应该怎么做才能获得我需要的所有信息,或者还有其他解决方案吗?非常感谢这是我的导出数据代码:**
SaveFileDialog savefiledialog = new SaveFileDialog();
savefiledialog.FileName = @"data.xlsx";
if (!(bool)savefiledialog.ShowDialog())
return;
DbHelper dh = new DbHelper("data.mdb");
ApplicationClass excel = new ApplicationClass();
Workbooks workbooks = excel.Workbooks;
Workbook workbook = workbooks.Add();
Worksheet sheet1 = (Worksheet)excel.ActiveSheet;
ArrayList itemList = dh.setTable("Item").where("1=1").select();
sheet1.Cells[1, "A"] = "col1";
sheet1.Cells[1, "B"] = "col2";
sheet1.Cells[1, "C"] = "col3";
int i = 2;
foreach (Item item in itemList)
{
sheet1.Cells[i, "A"] = item.Website;
sheet1.Cells[i, "B"] = item.Shop_id;
sheet1.Cells[i, "C"] = item.Title;
i++;
}
sheet1.Range["A1"].AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic1);
string fileName = string.Format(savefiledialog.FileName, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
// Save this data as a file
System.Action<string, string, string, string, string, string, string, string, string, string> action = sheet1.SaveAs;
BackgroundWorker b = new BackgroundWorker();
sheet1.SaveAs(fileName);
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(sheet1);
workbook.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(workbooks);
excel.Quit();