下面代码网上看来的,实在看不出哪里有错,可调试时确实出错,出错行我标示出来了,各位大虾务必帮个忙,先谢一个:
private bool ExportExcel(string FilePath, string p_ReportName)
        {
            if (this.dataGridView1.Rows.Count == 0)
            {
                return false;
            }
            Excel.Application xlApp = new Excel.ApplicationClass();
            if(xlApp==null)
            {
                MessageBox.Show("Excel无法启动");
                return false;
            }            Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
            Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
            Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, this.dataGridView1.Columns.Count]);
            range.MergeCells = true;         //合并单元格
            xlApp.ActiveCell.FormulaR1C1 = p_ReportName;  //设置标题
            xlApp.ActiveCell.Font.Size = 20;
            xlApp.ActiveCell.Font.Bold = true;
            xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter; //字体对齐方式            int colIndex = 0; 
            int rowIndex = 0;
            int colCount = this.dataGridView1.Columns.Count;
            int rowCount = this.dataGridView1.Rows.Count;            object[,] objData = new object[rowCount + 1, colCount];
             // 获取列标题
            foreach (DataGridViewColumn dgvc in this.dataGridView1.Columns)
            {
                objData[rowIndex, colIndex++] = dgvc.HeaderText;
            }
            // 获取数据
            for (rowIndex = 1; rowIndex <= rowCount; rowIndex++)
            {
                for (colIndex = 0; colIndex < colCount; colIndex++)
                {
                    objData[rowIndex, colIndex] = this.dataGridView1[rowIndex - 1, colIndex].ToString(); //出错行
                }
                Application.DoEvents();
            }
            // 写入Excel
            xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Italic = true;
            range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowCount + 2, colCount]);
            range.Value2 = objData;            // 保存
            try
            {
                xlApp.Cells.EntireColumn.AutoFit();
                xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter;
                xlBook.Saved = true;
                xlBook.SaveCopyAs(FilePath + ".xls");
                MessageBox.Show("导出成功!");
            }
            catch
            {
                MessageBox.Show("保存出错!");
                return false;
            }
            finally
            {
                xlApp.Quit();
                GC.Collect();
                KillProcess("excel");
            }
            return true;
        }
出错提示:
索引超出范围。必须为非负值并小于集合大小。
参数名: index

解决方案 »

  1.   

    for (rowIndex = 1; rowIndex  <= rowCount; rowIndex++) 
    有问题,改成
    for (rowIndex = 0; rowIndex  < rowCount; rowIndex++) 
      

  2.   

    this.dataGridView1[rowIndex - 1, colIndex].ToString()第一次循环rowIndex -1 等于0 
    而dataGridview第0行为表头,肯定会报错
      

  3.   


    /*   数组和控件行的下标都是从0开始的    */
    int colCount = this.dataGridView1.Columns.Count; 
    int rowCount = this.dataGridView1.Rows.Count; 
    object[,] objData = new object[rowCount, colCount]; 
    // 获取列标题 
    foreach (DataGridViewColumn dgvc in this.dataGridView1.Columns) 

      objData[rowIndex, colIndex++] = dgvc.HeaderText; 

    // 获取数据 
    for (rowIndex = 0; rowIndex  < rowCount; rowIndex++) 

      for (colIndex = 0; colIndex  < colCount; colIndex++) 
      { 
        objData[rowIndex, colIndex] = this.dataGridView1[rowIndex , colIndex].ToString(); 
      } 
      Application.DoEvents(); 

      

  4.   

    楼上说的并不正确,请在仔细看一下,rowIndex=0 这一行是用来存放DataGridView的标题行的
      

  5.   

    你那个for循环写错了,dataGridView[前面这个参数为列的,后面这个参数为行的],楼主写反了。。
    ^_^,给分吧。刚才我在vs上写的时候发现的。
      

  6.   

    楼上错了。objData[rowIndex, colIndex] = this.dataGridView1[rowIndex - 1, colIndex].ToString(); //出错行 
    是这句dataGridView1[,]这里面参数写反了。
      

  7.   

    不对,现在能运行成功了,但为什么导出来的值是DataGridViewTextBoxCell { ColumnIndex=0, RowIndex=0 } 这个样子的,没哟具体的内容?