繁体   English   中英

导出Excel文件后XML加载失败

[英]xml load after exporting excel file failed

我的ac#应用程序具有以下功能:

  • 从配置文件读取密码。
  • 导出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;
}

尝试在中指定完整路径

xmlDoc.Load("Cfg.xml"); 

例如

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

导出excel时,当前文件夹已更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM