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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:03:48+00:00 2026-05-17T01:03:48+00:00

I have a two table’s ,where i have to dump the info into the

  • 0

I have a two table’s ,where i have to dump the info into the Excel.I have used the follwing code.But am getting the Error “Index out of Bound”.

 public void ConvertToTable()
    {
        DataSet ds = new DataSet();
        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();
        dt1 = ExportToExcel.ToDataTable<CMInfo>(CMInfo);
        dt2 = ExportToExcel.ToDataTable<RefMInfo>(REFMINFO);
        dt1.TableName = "Changed Method Information";
        dt2.TableName = "Referenced Method Information";
        ds.Tables.Add(dt1);
        ds.Tables.Add(dt2);
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet[] xlWorkSheet = new Excel.Worksheet[ds.Tables.Count];
        int Sheet_Count = 0;
        object misValue = System.Reflection.Missing.Value;
        xlApp = new Excel.Application();
        xlApp.SheetsInNewWorkbook = ds.Tables.Count;
        xlWorkBook = xlApp.Workbooks.Add(misValue);

        foreach (DataTable dt in ds.Tables)
        {
            if (Sheet_Count < ds.Tables.Count)
            {
                xlWorkSheet[Sheet_Count] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(Sheet_Count + 1);
                xlWorkSheet[Sheet_Count].Name = dt.TableName;
                int iCol = 0;
                foreach (DataColumn c in dt.Columns)
                {
                    iCol++;
                    xlWorkSheet[Sheet_Count].Cells[1, iCol] = c.ColumnName;
                    xlWorkSheet[Sheet_Count].Cells[1, iCol].Font.Bold = true;
                }
                // For each row of data
                int iRow = 0;
                foreach (DataRow r in dt.Rows)
                {
                    iRow++;
                    // Add each row's cell data...
                    iCol = 0;
                    foreach (DataColumn c in dt.Columns)
                    {
                        iCol++;
                        xlWorkSheet[Sheet_Count].Cells[iRow + 1, iCol] = r[c.ColumnName];
                        xlWorkSheet[Sheet_Count].Cells.ColumnWidth = (xlWorkSheet[Sheet_Count].Cells[iRow + 1, iCol].Count() * 50);
                    }
                }
                Sheet_Count++;
            }

        }
        xlWorkBook.SaveAs("ReferenceMethodInfo.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();
        Marshal.ReleaseComObject(xlWorkSheet[Sheet_Count]);
        Marshal.ReleaseComObject(xlWorkBook);
        Marshal.ReleaseComObject(xlApp);
        ds.Clear();
        System.Windows.Forms.MessageBox.Show("Exported Completed");
    }

//This Code for Converting the List into DataTable

  public static class ExportToExcel
{
    // remove "this" if not on C# 3.0 / .NET 3.5
    public static DataTable ToDataTable<T>(this IList<T> data)
    {
        PropertyDescriptorCollection props =
            TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();

        for (int i = 0; i < props.Count; i++)
        {
            PropertyDescriptor prop = props[i];
            table.Columns.Add(prop.Name, prop.PropertyType);
        }
        object[] values = new object[props.Count];
        foreach (T item in data)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = props[i].GetValue(item);
            }
            table.Rows.Add(values);
        }
        return table;
    }
}
  • 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-17T01:03:48+00:00Added an answer on May 17, 2026 at 1:03 am

    Difficult to say wexactly what causes the error without knowing which function or indeed which line causes the error.

    I’ll admit that I’ve never exported data to excel, and so don’t know if cells are indexed on zero or from 1.

    I am concerned that you initialise iRow and iCol to zero (0) and yet you increment them (to 1) before use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two table TABLE_EXAM and TABLE_QUESTION. I fetch record using following code but
If I have two table of data. One has a clustered index CINDEX, the
I have two table views, when user clicks one cell of the first, second
I have two table cells, with dynamic content. Table cell one works fine, when
I have two table. One table hold key column and xml column. Other table
I have two table masterTbl and detailTbl which structure is given below... --PRIMARY TABLE
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have two table emptable1(empid,status) emptable2(empid,week) i want to select all the empid whose
i have two table: declare @t1 table (id int) declare @t2 table (id int)
I have two table first is TABLE_SUBJECT and second is TABLE_CHAPTER.Now i want to

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.