0

Interop.Excel を使用して Excel からデータを読み取るコードがあります。問題は、write メソッドを 2 回目に呼び出したときに、別のコピーに保存されているように見え、最初の呼び出しだけで実際に元の Excel ファイルにデータが書き込まれることです。相互運用オブジェクトが適切にクリーンアップされないようです。

これは読み取り/書き込みメソッドです

public class ExcelHelper
{
    Microsoft.Office.Interop.Excel.Application excelApp;
    static AppSettingsReader settingsReader = new AppSettingsReader();


    public string[,] getExcelInfo(string excelFileName)
    {

        string[,] dataArray;

        string logfileName = settingsReader.GetValue("LogFileName", typeof(string)).ToString();
        string logDirectory = settingsReader.GetValue("LogDirectory", typeof(string)).ToString();
        string logLevel = settingsReader.GetValue("LogLevel", typeof(string)).ToString();

        Logger.Log(logfileName, logDirectory, logLevel, "Getting File " + excelFileName);

        Workbook wb = excelApp.Workbooks.Open(excelFileName,null,true);
        Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = (Microsoft.Office.Interop.Excel._Worksheet)wb.Sheets[1];

        Worksheet ws = wb.Worksheets[1];

        Range range = ws.UsedRange;

       dataArray = new string[range.Rows.Count, range.Columns.Count];

        string str = string.Empty;
        int rCnt = 0;
        int cCnt = 0;

        for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
        {
            for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            {



                dataArray[rCnt - 1, cCnt - 1] = System.Convert.ToString((range.Cells[rCnt, cCnt] as Range).Value2);




            }
        }

        wb.Close();


        releaseObject(range);
        releaseObject(ws);
        releaseObject(wb);



       return dataArray;


    }


    public static int WritePRSIdToExcel(int PRSInt, int line)
    {


      string excelApp = ConfigurationManager.AppSettings["excelFileName"];


      //  Application excel = (Application)Marshal.GetActiveObject("Excel.Application");
        _Application docExcel = (Application)Marshal.GetActiveObject("Excel.Application");
        docExcel.Visible = false;
        docExcel.DisplayAlerts = false;

        _Workbook workbooksExcel = docExcel.Workbooks.Open(excelApp, null, true);
        _Worksheet worksheetExcel = (_Worksheet)workbooksExcel.ActiveSheet;


        Range range = worksheetExcel.UsedRange;


        string str;

        str = (string)(range.Cells[line, 3] as Microsoft.Office.Interop.Excel.Range).Value2;



        if (!(str == null))
        {

            ((Range)worksheetExcel.Cells[line + 1, 5]).Value2 = PRSInt;

            workbooksExcel.Save();
            GC.Collect();   
            GC.WaitForPendingFinalizers();
            workbooksExcel.Close(false, Type.Missing, Type.Missing);
            docExcel.Quit();
            docExcel.Application.DisplayAlerts = true;
            Marshal.ReleaseComObject(workbooksExcel);
            Marshal.ReleaseComObject(docExcel);
            Marshal.ReleaseComObject(worksheetExcel);
            Marshal.ReleaseComObject(range);

        }



        return PRSInt;


    }

これは、セルに書き込まれたデータを読み取る方法です。

public class GetIDNumber
{
    static AppSettingsReader settingsReader = new AppSettingsReader();



    static void Main(string[] args)
    {
        ExcelHelper excelHelper = new ExcelHelper();



        /// <summary>
        /// Log settings
        /// </summary>
        string logfileName = settingsReader.GetValue("LogFileName", typeof(string)).ToString();
        string logDirectory = settingsReader.GetValue("LogDirectory", typeof(string)).ToString();
        string logLevel = settingsReader.GetValue("LogLevel", typeof(string)).ToString();

        string excelFileName = settingsReader.GetValue("ExcelFileName", typeof(string)).ToString();
        string TeamProjectName = settingsReader.GetValue("TeamProject", typeof(string)).ToString();

        excelHelper.InitExcel();

        tfsHelper.Connect();

        string[,] dataArray = excelHelper.getExcelInfo(excelFileName);



        //handle data


        const int START_ROW = 1;
        const int PRODUCT_REQUIRMENT_TITLE_COL = 2;
        const int REQ_DESC = 3;
        const int WORKITEM_ID_COL = 4;
        const int REQUIRMENT_ACV_ID_COL = 5;
        const int RISK_COL = 6;

        int TFSId = 0;     
        int PRSId = 0;



        while (line < dataArray.GetLength(0))
        {

            line++;



                  if (!string.IsNullOrEmpty(dataArray[line, REQ_DESC]))
                  {

                                    string TfsId = string.Empty;

                                     TfsId = ((dataArray[line, 4]));

                  } 

          }






enter code here
4

1 に答える 1