0

MicrosoftInteropExcelオブジェクトを使用してExcelスプレッドシートのデータを正規化しようとしています。基本的に、特定の列オフセットから始まる行に列を変換する必要があります。

Original Data:
ColumnA ColumnB ColumnC ColumnD ColumnE ColumnF
   X       Y      10      20      30      40

Normalized Data:
ColumnA ColumnB NewColumn Value
  X        Y     ColumnC   10
  X        Y     ColumnD   20
  X        Y     ColumnE   30
  X        Y     ColumnF   40

私の関数は期待どおりに機能します。ただし、実行時間は非常に遅いです。したがって、OpenXMLのような他のフレームワークを使用するかどうか疑問に思っていますが、効率が向上するのでしょうか。

相互運用オブジェクトを使用したコードは次のとおりです。

public static void Normalize(string aFilePathName, string aSheetName, int aColOffSet, string aPivotColName, string aValueColName)
{
  LOG.DebugFormat("Normaling data in file: {0}", aFilePathName);
  LOG.DebugFormat("Sheet Name:{0} ColOffset:{1}", aSheetName, aColOffSet);

   Excel.Application vExcel = new Excel.Application();
   Excel.Workbook vWorkbook = null;
   Excel.Worksheet vWsOriginal = null;
   Excel.Worksheet vWsNormalized = null;
   try
     {
        vExcel.Visible = false;
        vWorkbook = vExcel.Workbooks.Open(aFilePathName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        vWsOriginal = vWorkbook.Worksheets[aSheetName];
        string vNormalizedSheetName = string.Format("Normalized {0}", aSheetName);
        bool vNormalizedSheetExists = (vWorkbook.Sheets.Cast<object>()
                                            .Select(sheetValue => sheetValue as Excel.Worksheet))
                                            .Any(wbSheet => wbSheet != null && wbSheet.Name == vNormalizedSheetName);
            if (!vNormalizedSheetExists)
            {
                vWsNormalized = vWorkbook.Worksheets.Add(vWsOriginal, Type.Missing, Type.Missing, Type.Missing);
                vWsNormalized.Name = vNormalizedSheetName;
            }
            else
            {
                vWsNormalized = vWorkbook.Worksheets[vNormalizedSheetName];
            }
            vWsNormalized.UsedRange.ClearContents();

            long vTotalColumns = 1;
            long vRowCounter = 1;
            Excel.Range vWsRange = vWsOriginal.Cells[vRowCounter, vTotalColumns];

            List<string> vHeaders = new List<string>();
            while (vWsRange.Value2 != null)
            {
                vHeaders.Add(vWsRange.Value2.ToString());
                vTotalColumns = vTotalColumns + 1;
                vWsRange = vWsOriginal.Cells[vRowCounter, vTotalColumns];
            }

            // Insert the headers
            for (int vHeaderCol = 1; vHeaderCol < aColOffSet; vHeaderCol++)
            {
                vWsNormalized.Cells[1, vHeaderCol].Value = vHeaders[vHeaderCol - 1];
            }
            vWsNormalized.Cells[1, aColOffSet].Value = aPivotColName;
            vWsNormalized.Cells[1, aColOffSet + 1].Value = aValueColName;

            long vNewRow = 2;

            for (int vCol = aColOffSet; vCol < vTotalColumns; vCol++)
            {
                vRowCounter = 2;
                while (((Excel.Range)vWsOriginal.Cells[vRowCounter, 1]).Value2 != null)
                {
                    for (int j = 1; j < aColOffSet; j++)
                    {
                        vWsNormalized.Cells[vNewRow, j] = vWsOriginal.Cells[vRowCounter, j];
                    }

                    vWsNormalized.Cells[vNewRow, aColOffSet] = vWsOriginal.Cells[1, vCol];
                    vWsNormalized.Cells[vNewRow, aColOffSet + 1] = vWsOriginal.Cells[vRowCounter, vCol];

                    vRowCounter = vRowCounter + 1;

                    vNewRow = vNewRow + 1;
                }
            }
        }
        finally
        {
            vWorkbook.Close(Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
            Marshal.FinalReleaseComObject(vWsNormalized);
            Marshal.FinalReleaseComObject(vWsOriginal);
            Marshal.FinalReleaseComObject(vWorkbook);
            vExcel.Quit();
            Marshal.FinalReleaseComObject(vExcel);
        }
    }

パフォーマンスを改善する可能性がある場合は、他のオープンソースフレームワークを試してみます。

ありがとう

4

2 に答える 2

0

私はより良い実装を思い付くことができました。すべてのセルをループする代わりに、Excelの転置機能を利用して一括コピーを実行します。

public static void Normalize2(string aFilePathName, string aSheetName, int aColOffSet, string aPivotColName, string aValueColName)
    {
        LOG.DebugFormat("Normaling data in file: {0}", aFilePathName);
        LOG.DebugFormat("Sheet Name:{0} ColOffset:{1}", aSheetName, aColOffSet);

        Excel.Application vExcel = new Excel.Application();
        Excel.Workbook vWorkbook = null;
        Excel.Worksheet vWsOriginal = null;
        Excel.Worksheet vWsNormalized = null;
        try
        {
            vExcel.Visible = false;
            vWorkbook = vExcel.Workbooks.Open(aFilePathName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            vWsOriginal = vWorkbook.Worksheets[aSheetName];
            //vWsOriginal.Name = string.Format("Original_{0}", aSheetName);
            string vNormalizedSheetName = string.Format("Normalized {0}", aSheetName);
            bool vNormalizedSheetExists = (vWorkbook.Sheets.Cast<object>()
                                            .Select(sheetValue => sheetValue as Excel.Worksheet))
                                            .Any(wbSheet => wbSheet != null && wbSheet.Name == vNormalizedSheetName);
            if (!vNormalizedSheetExists)
            {
                vWsNormalized = vWorkbook.Worksheets.Add(vWsOriginal, Type.Missing, Type.Missing, Type.Missing);
                vWsNormalized.Name = vNormalizedSheetName;
            }
            else
            {
                vWsNormalized = vWorkbook.Worksheets[vNormalizedSheetName];
            }
            vWsNormalized.UsedRange.ClearContents();

            long vTotalColumns = 1;
            long vRowCounter = 1;
            Excel.Range vWsRange = vWsOriginal.Cells[vRowCounter, vTotalColumns];

            List<string> vHeaders = new List<string>();
            while (vWsRange.Value2 != null)
            {
                vHeaders.Add(vWsRange.Value2.ToString());
                vTotalColumns = vTotalColumns + 1;
                vWsRange = vWsOriginal.Cells[vRowCounter, vTotalColumns];
            }

            // Insert the headers
            for (int vHeaderCol = 1; vHeaderCol < aColOffSet; vHeaderCol++)
            {
                vWsNormalized.Cells[1, vHeaderCol].Value = vHeaders[vHeaderCol - 1];
            }
            vWsNormalized.Cells[1, aColOffSet].Value = aPivotColName;
            vWsNormalized.Cells[1, aColOffSet + 1].Value = aValueColName;

            long vNewRow = 2;
            long vValueColumns = vTotalColumns - aColOffSet;
            vRowCounter = 2;

            Excel.Range vHeaderData = vWsOriginal.Range[vWsOriginal.Cells[1, aColOffSet],
                                                        vWsOriginal.Cells[1, vTotalColumns - 1]];
            string[] vPivotValueNames = new string[vTotalColumns - aColOffSet];
            vHeaders.CopyTo(aColOffSet - 1, vPivotValueNames, 0, (int) (vTotalColumns - aColOffSet));
            while (((Excel.Range)vWsOriginal.Cells[vNewRow, 1]).Value2 != null)
            {
                Excel.Range vStaticRowData = vWsOriginal.Range[vWsOriginal.Cells[vNewRow, 1],
                                                                   vWsOriginal.Cells[vNewRow, aColOffSet - 1]];

                Excel.Range vDynamicRowData = vWsOriginal.Range[vWsOriginal.Cells[vNewRow, aColOffSet],
                                                                   vWsOriginal.Cells[vNewRow, vTotalColumns - 1]];

                long vDestRowStart = vRowCounter;
                long vDestRowEnd = (vRowCounter + vValueColumns) - 1;
                Excel.Range vNormalizedStaticRowData = vWsNormalized.Range[vWsNormalized.Cells[vDestRowStart, 1],
                                                                    vWsNormalized.Cells[vDestRowEnd, aColOffSet - 1]];
                Excel.Range vNormalizedPivotValueRowData = vWsNormalized.Range[vWsNormalized.Cells[vDestRowStart, aColOffSet],
                                                                    vWsNormalized.Cells[vDestRowEnd, aColOffSet]];

                Excel.Range vNormalizedValueRowData = vWsNormalized.Range[vWsNormalized.Cells[vDestRowStart, aColOffSet + 1],
                                                                    vWsNormalized.Cells[vDestRowEnd, aColOffSet + 1]];
                vNormalizedStaticRowData.Value = vStaticRowData.Value;
                vNormalizedPivotValueRowData.Value = vExcel.WorksheetFunction.Transpose(vHeaderData.Value);
                vNormalizedValueRowData.Value = vExcel.WorksheetFunction.Transpose(vDynamicRowData.Value);

                vNewRow = vNewRow + 1;
                vRowCounter = vRowCounter + vValueColumns;
            }
        }
        finally
        {
            vWorkbook.Close(Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
            Marshal.FinalReleaseComObject(vWsNormalized);
            Marshal.FinalReleaseComObject(vWsOriginal);
            Marshal.FinalReleaseComObject(vWorkbook);
            vExcel.Quit();
            Marshal.FinalReleaseComObject(vExcel);
        }
    }
于 2012-08-27T18:52:40.390 に答える
0

最近、似たようなことをしなければならず、ピボットテーブルウィザードを使用してこのトリックを見つけました:http ://www.launchexcel.com/pivot-table-flatten-crosstab/

于 2013-10-31T23:27:34.440 に答える