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 4114432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:27:17+00:00 2026-05-20T22:27:17+00:00

I have an accounting program that generates reports and saves them in excel file.

  • 0

I have an accounting program that generates reports and saves them in excel file. But I don’t need most of the columns in this report. So I created a program that deletes the columns that I don’t need. In the converted file I use 3 empty columns to write notes, data etc. I generate this report every week, so when the new report is generated I need to convert him and transfer the 3 columns, that I wrote in the old report, to the new one. The first column of the report is UserID, so I need to transfer the notes and data that I wrote for the ID’s. Some ID’s from the old report may be missing in the new one.
I created a Windows Forms application that should do this, but it doesn’t work good. The new report is converted, but the data from the old one is not transfered. Any ideas?? This is my code:

                OpenExcelWorkbook(OldFilePath);
                _sheet = (Excel.Worksheet)_sheets[1];
                _sheet.Select(Type.Missing);

                var oldData = new DataTable();
                oldData.Columns.Add("Id", typeof(string));
                oldData.Columns.Add("Comment1", typeof(string));
                oldData.Columns.Add("Comment2", typeof(string));
                oldData.Columns.Add("Comment3", typeof(string));

                var range = _sheet.UsedRange;

                for (var i = 1; i <= range.Rows.Count; i++)
                {
                    oldData.Rows.Add(_sheet.Range["A" + i, Type.Missing].Value2, _sheet.Range["J" + i, Type.Missing].Value2, _sheet.Range["K" + i, Type.Missing].Value2,
                        _sheet.Range["L" + i, Type.Missing].Value2);
                }

                _book.Save();
                _book.Close(false, Type.Missing, Type.Missing);
                _app.Quit();
                releaseObject(_sheet);
                releaseObject(_book);
                releaseObject(_app);


                OpenExcelWorkbook(NewFilePath);
                _sheet = (Excel.Worksheet)_sheets[1];
                _sheet.Select(Type.Missing);

                range = _sheet.Range["D:E", Type.Missing];
                range.EntireColumn.Delete(Type.Missing);
                range = _sheet.Range["E:F", Type.Missing];
                range.EntireColumn.Delete(Type.Missing);
                range = _sheet.Range["F:I", Type.Missing];
                range.EntireColumn.Delete(Type.Missing);

                _sheet.Cells[4, 3] = "Invoice";
                _sheet.Cells[4, 4] = "Transfer";
                _sheet.Cells[4, 5] = "Receipt";


                range = _sheet.UsedRange;

                var convDataTable = new DataTable();
                convDataTable.Columns.Add("Id", typeof(string));
                convDataTable.Columns.Add("Comment1", typeof(string));
                convDataTable.Columns.Add("Comment2", typeof(string));
                convDataTable.Columns.Add("Comment3", typeof(string));


                for (var i = 1; i < range.Rows.Count; i++)
                    convDataTable.Rows.Add(_sheet.Range["A" + i, Type.Missing].Value2, "", "", "");

                foreach (DataRow row in convDataTable.Rows)
                {
                    foreach (DataRow row1 in oldData.Rows)
                    {
                        if (row["Id"] == row1["Id"])
                        {
                            row["Comment1"] = row1["Comment1"];
                            row["Comment2"] = row1["Comment2"];
                            row["Comment3"] = row1["Comment3"];
                        }
                    }
                }


                for (var i = 1; i <= convDataTable.Rows.Count; i++)
                {
                    _sheet.Cells[i, 10] = convDataTable.Rows[i - 1]["Comment1"];
                    _sheet.Cells[i, 11] = convDataTable.Rows[i - 1]["Comment2"];
                    _sheet.Cells[i, 12] = convDataTable.Rows[i - 1]["Comment3"];
                }


                _book.Save();
                _book.Close(false, Type.Missing, Type.Missing);
                _app.Quit();
                releaseObject(_sheet);
                releaseObject(_book);
                releaseObject(_app);

These are the functions that I use.

    private static void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }
    protected void OpenExcelWorkbook(string filePath)
    {
        _app = new Application();

        _books = _app.Workbooks;
        _book = _books.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        _sheets = _book.Worksheets;

    }
  • 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-05-20T22:27:18+00:00Added an answer on May 20, 2026 at 10:27 pm

    I solved the problem. This is the solution:

     OpenExcelWorkbook(OldFilePath);
                    _sheet = (Excel.Worksheet)_sheets[1];
                    _sheet.Select(Type.Missing);
    
                    var oldData = new DataTable();
                    oldData.Columns.Add("Id", typeof(string));
                    oldData.Columns.Add("Comment1", typeof(string));
                    oldData.Columns.Add("Comment2", typeof(string));
                    oldData.Columns.Add("Comment3", typeof(string));
    
                    var range = _sheet.UsedRange;
    
                    for (var i = 1; i <= range.Rows.Count; i++)
                    {
                        var id = "";
                        var comment1 = "";
                        var comment2 = "";
                        var comment3 = "";
    
                        if (((Excel.Range)_sheet.Cells[i, 1]).Value2 != null)
                            id = ((Excel.Range)_sheet.Cells[i, 1]).Value2.ToString();
    
                        if (((Excel.Range)_sheet.Cells[i, 10]).Value2 != null)
                            comment1 = ((Excel.Range)_sheet.Cells[i, 10]).Value2.ToString();
    
                        if (((Excel.Range)_sheet.Cells[i, 11]).Value2 != null)
                            comment2 = ((Excel.Range)_sheet.Cells[i, 11]).Value2.ToString();
    
                        if (((Excel.Range)_sheet.Cells[i, 12]).Value2 != null)
                            comment3 = ((Excel.Range)_sheet.Cells[i, 12]).Value2.ToString();
                        if (comment1 != "" || comment2 != "" || comment3 != "")
                            oldData.Rows.Add(id, comment1, comment2, comment3);
                    }
    
                    _book.Save();
                    _book.Close(false, Type.Missing, Type.Missing);
                    _app.Quit();
                    releaseObject(_sheet);
                    releaseObject(_book);
                    releaseObject(_app);
    
    
                    OpenExcelWorkbook(NewFilePath);
                    _sheet = (Excel.Worksheet)_sheets[1];
                    _sheet.Select(Type.Missing);
    
                    range = _sheet.Range["D:E", Type.Missing];
                    range.EntireColumn.Delete(Type.Missing);
                    range = _sheet.Range["E:F", Type.Missing];
                    range.EntireColumn.Delete(Type.Missing);
                    range = _sheet.Range["F:I", Type.Missing];
                    range.EntireColumn.Delete(Type.Missing);
    
                    _sheet.Cells[4, 3] = "Invoice";
                    _sheet.Cells[4, 4] = "Transaction";
                    _sheet.Cells[4, 5] = "Receipt";
    
    
                    range = _sheet.UsedRange;
    
                    var convDataTable = new DataTable();
                    convDataTable.Columns.Add("Id", typeof(string));
                    convDataTable.Columns.Add("Comment1", typeof(string));
                    convDataTable.Columns.Add("Comment2", typeof(string));
                    convDataTable.Columns.Add("Comment3", typeof(string));
    
    
                    for (var i = 1; i < range.Rows.Count; i++)
                    {
                        var id = "";
                        if (((Excel.Range)_sheet.Cells[i, 1]).Value2 != null)
                            id = ((Excel.Range)_sheet.Cells[i, 1]).Value2.ToString();
                        convDataTable.Rows.Add(id, "", "", "");
                    }
    
                    foreach (DataRow row in convDataTable.Rows)
                    {
                        foreach (DataRow row1 in oldData.Rows)
                        {
                            var oldId = row1[0].ToString();
                            var newId = row[0].ToString();
                            if (newId != "")
                            {
                                if (newId == oldId)
                                {
                                    var comment1 = row1[1].ToString();
                                    var comment2 = row1[2].ToString();
                                    var comment3 = row1[3].ToString();
                                    row[1] = comment1;
                                    row[2] = comment2;
                                    row[3] = comment3;
                                    break;
                                }
                            }
                        }
                    }
    
    
                    for (var i = 1; i <= convDataTable.Rows.Count; i++)
                    {
                        _app.Cells[i, 10] = convDataTable.Rows[i - 1]["Comment1"].ToString();
                        _app.Cells[i, 11] = convDataTable.Rows[i - 1]["Comment2"].ToString();
                        _app.Cells[i, 12] = convDataTable.Rows[i - 1]["Comment3"].ToString();
                    }
    
    
                    _book.Save();
                    _book.Close(false, Type.Missing, Type.Missing);
                    _app.Quit();
                    releaseObject(_sheet);
                    releaseObject(_book);
                    releaseObject(_app);
                    MessageBox.Show("The converting is finished");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been building a Rails application that performs accounting functionality. As part of
This issue stems from an accounting package that spits out text-based reports with rows
I need to know how does svcutil and Visual Studio decide which types can
I googled a bit with no luck. I'm wondering if a tool exists that
I have the below SQL which works just fine: SELECT Message, CreateDate, AccountId, AlertTypeId
I have the following function signuture: public JsonResult PopulateGrid(int page, Guid? accountId, Guid? systemUserId,
I have a table of chalets where a chalet is referenced by an account...
Can some recommend a regex to return the value when an item is selected
Good morning guys Is there a good way to use regular expression in C#
first of all, I am new to silverlight(play around with it for one month)

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.