导出excel文件后加载xml失败

时间:2015-09-30 15:59:02

标签: c#

我有一个具有以下功能的c#应用程序:

  • 从配置文件中读取密码。
  • 导出Excel文件。

当我第一次读密码时:没关系。但是一旦我导出Excel文件,我就不能再从配置文件中读取密码了。以下说明失败。

xmlDoc.Load("Cfg.xml");

此问题仅出现在Windows XP上。在Windows 7上没关系。

从配置文件中读取密码的代码:

private void OK_Click(object sender, EventArgs e)
        {
            try
            {
                // Check password
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("Cfg.xml");
                XmlNode node = xmlDoc.SelectSingleNode("Config/ManagerPW");

                if (node != null)
                {
                    string MangerPW = node.Attributes[0].Value;

                    PCCrypto PWCrypto = new PCCrypto();
                    if (PWCrypto.verifyMd5(this.Password.Text, MangerPW) == true)
                    {
                        isCorrectPassWord = true;
                        this.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("Incorrect Password!", "PW Authentication", MessageBoxButtons.OK,
                           MessageBoxIcon.Error);
                        this.Password.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("You tried to perform an unauthorized operation!", "PW Authentication", MessageBoxButtons.OK,
                               MessageBoxIcon.Error);
                    this.Password.Text = "";
                }
            }
            catch
            {
                MessageBox.Show("Error in loading configuration file", "Configuration Error", MessageBoxButtons.OK,
                               MessageBoxIcon.Error);
                this.Dispose(); 
            }

        }

导出Excel文件的代码

public bool exportToExcel(string path)
{
    bool bRet = true;
    int columnsNum = resultList.Columns.Count - 1;
    int rowsNum = resultList.Items.Count;
    object[,] array = new object[rowsNum + 1, columnsNum];

    //Change Current System Time to US
    System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

    Excel.Application xlApp = new Excel.ApplicationClass();
    Excel.Workbooks xlWorkBooks = xlApp.Workbooks;
    Excel.Workbook xlWorkBook = xlWorkBooks.Add(Type.Missing);
    Excel.Sheets xlWorkSheets = xlWorkBook.Sheets;
    Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkSheets[1];

    //disable alerts
    xlApp.DisplayAlerts = false;

    //Add Header to array            
    for (int i = 0; i < columnsNum; i++)
    {
        array[0, i] = resultList.Columns[i + 1].Text;
    }

    //Add Listview data to array
    for (int r = 0; r < rowsNum; r++)
    {
        for (int c = 0; c < columnsNum; c++)
        {
            this.Invoke(new MethodInvoker(delegate
            {
                array[r + 1, c] = resultList.Items[r].SubItems[c+1].Text;
            }));
        }
    }

    //Save array data into excel
    Excel.Range c1 = (Excel.Range)xlWorkSheet.Cells[1, 1];
    Excel.Range c2 = (Excel.Range)xlWorkSheet.Cells[rowsNum + 1, columnsNum];
    Excel.Range xlRange = xlWorkSheet.get_Range(c1, c2);
    xlRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;       
    xlRange.EntireColumn.NumberFormat = "@";
    xlRange.Value2 = array;
    xlRange.EntireColumn.AutoFit();

    //Add Header color
    xlWorkSheet.get_Range("A1", "I1").Interior.Color = ColorTranslator.ToOle(Color.Aquamarine);

    //Save Excel file
    try
    {
        xlWorkBook.SaveAs(@path, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        xlWorkBook.Close(true, Type.Missing, Type.Missing);
    }
    catch
    {
        bRet = false;
    }

    xlWorkBooks.Close();
    xlApp.Application.Quit();
    xlApp.Quit();
    releaseObject(c1);
    releaseObject(c2);
    releaseObject(xlRange);
    releaseObject(xlWorkSheet);
    releaseObject(xlWorkSheets);
    releaseObject(xlWorkBook);
    releaseObject(xlWorkBooks);
    releaseObject(xlApp);

    return bRet;
}

1 个答案:

答案 0 :(得分:0)

尝试在

中指定完整路径
xmlDoc.Load("Cfg.xml"); 

e.g。

xmlDoc.Load(@"C:\myfolder\Cfg.xml"); 

导出Excel时,当前文件夹正在更改。