Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8443945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:16:33+00:00 2026-06-10T09:16:33+00:00

I am trying to normalize the data in an excel spreadsheet using Microsoft Interop

  • 0

I am trying to normalize the data in an excel spreadsheet using Microsoft Interop Excel objects. Basically, I need to convert the columns into rows starting from a certain column offset.

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

My function works as expected. However, the running time is very slow. So, I am wondering if I use any other framework like OpenXML, will I see any increase in efficiency

Here is my code using Interop objects:

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);
        }
    }

I am willing to try any other open source frameworks, if there is a possibility to improve performance.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:16:34+00:00Added an answer on June 10, 2026 at 9:16 am

    I was able to come up with a better implementation. Instead of looping through every single cell, leverage the Excel transpose function to do bulk copy.

    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);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get Excel data, which was mapped using a grid/matrix mapping
I am trying to convert a document with content like the following into another
Just wondering whether it is possible to convert highly normalized data into binary and
I've run into a bit of a hiccup trying to plot some data in
Need to load data from a single file with a 100,000+ records into multiple
Need to load data from a single file with a 100,000+ records into multiple
I am trying to make the jump from data-centric design and development into DDD
I'm trying to normalize some data which I have in a data frame. I
I'm trying to figure out the best way to normalize the following data set,
I am trying to import various pipe delimited files using php 5.2 into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.