等待第1行以在起始行2之前完成执行

加入
2014年11月6日
消息
8
编程经验
Beginner
我正在打开一个Excel工作簿并刷新一些查询,然后保存工作簿。我的问题是它在完全刷新之前保存工作簿。我可以在我的语法中做些什么,以确保在保存之前完全刷新工作簿?

C#:
namespace RubarbJones
{
    public partial class Form1 : Form
    {
        
        public BackgroundWorker backgroundWorker1 = new BackgroundWorker();
        public static Excel.Application oXL;
        public static Excel._Workbook oWB;
        public static Excel._Worksheet oWS;
        public static string filename = "R:\\Excel\\Workbooks\\BaseTemplate.xlsx";
        
        public Form1()
        {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }


        private void btn1_Click()
        {
             backgroundWorker1.RunWorkerAsync();
        }
        
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            if (oXL == null)
            {
                oXL = new Excel.Application();
                oXL.Visible = true;
                oXL.DisplayAlerts = false;
                oWB = oXL.ActiveWorkbook;
            }
            oWB = (Excel._Workbook)(oXL.Workbooks.Open(filename));
            oWB = oXL.ActiveWorkbook;
            oWS = (Excel._Worksheet)(oWB.ActiveSheet);
            oWB.RefreshAll();
            oWB.SaveAs("C:\\Final\\" + oXL.ActiveWorkbook.Name);
        }
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {


        }        
}
 
Last edited:
最佳 底部