开发者

Can any find my mistake while dumping the data into excel?

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();
   开发者_StackOverflow     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;
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜