/// <summary>
        /// 导出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tlbImport_Click(object sender, EventArgs e)
        {
            //实例一个数据集对象
            DataSet pset = new DataSet();
            DataTable PTable = new DataTable();
            PTable.Columns.Add("抄表计划编号");
            PTable.Columns.Add("抄表区间");
            PTable.Columns.Add("抄表人");
            PTable.Columns.Add("小区");
            PTable.Columns.Add("抄表开始日期");
            PTable.Columns.Add("抄表结束日期");
            PTable.Columns.Add("计划是否提交");
            for (int i = 0; i < this.gvDenWriteTabPlan.Rows.Count; i++)
            {
                if (gvDenWriteTabPlan.Rows[i].Cells[0].Value != null && (bool)gvDenWriteTabPlan.Rows[i].Cells[0].Value == true)
                {
                    DataRow pRow = PTable.NewRow();
                    pRow[0] = gvDenWriteTabPlan.Rows[i].Cells[1].Value;
                    pRow[1] = gvDenWriteTabPlan.Rows[i].Cells[2].Value;
                    pRow[2] = gvDenWriteTabPlan.Rows[i].Cells[3].Value;
                    pRow[3] = gvDenWriteTabPlan.Rows[i].Cells[5].Value;
                    pRow[4] = gvDenWriteTabPlan.Rows[i].Cells[9].Value;
                    pRow[5] = gvDenWriteTabPlan.Rows[i].Cells[10].Value;
                    pRow[6] = gvDenWriteTabPlan.Rows[i].Cells[12].Value;
                   PTable.Rows.Add(pRow);
                   
                }
            }
            DenWriteTabPlanBiz biz = new DenWriteTabPlanBiz();
            pset.Tables.Add(PTable);
            Import(pset);
        }        /// <summary>
        /// 导出方法
        /// </summary>
        /// <param name="ds"></param>
        private void Import(DataSet ds)
        {
            //实例一个Excel类
            Excel.Application excel = new Excel.ApplicationClass();
            //添加到工作簿
            excel.Application.Workbooks.Add(true);
            //导出的文件名
            string fileName = this.Text + ".xls";
            //将DS转化成DataTable
            DataTable dt = ds.Tables[0];
            //初始化行和列
            int rowIndex = 1;
            int colIndex = 0;
            //遍历列
            foreach (DataColumn col in dt.Columns)
            {
                colIndex++;
                excel.Cells[1, colIndex] = col.ColumnName;
            }
            //遍历行
            foreach (DataRow row in dt.Rows)
            {
                rowIndex++;
                colIndex = 0;
                for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
                }
            }
            //为可显示
            excel.Visible = true;
            //保存
            //excel.ActiveWorkbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel5, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
        }

解决方案 »

  1.   

     //遍历行
                foreach (DataRow row in dt.Rows)
                {
                                    colIndex = 0;
                    for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                    {
                        excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
                    }
            rowIndex++;
                }
      

  2.   

    那条记录是最后选择的就不导,只导最后一条之前的记录!
    我调试过了,是下面这句的判断条件有错误,帮忙改一下,谢谢了!
    if (gvDenWriteTabPlan.Rows[i].Cells[0].Value != null && (bool)gvDenWriteTabPlan.Rows[i].Cells[0].Value == true)
    这是界面:
      

  3.   

    (gvDenWriteTabPlan.Rows[i].Cells[0].Value != null && gvDenWriteTabPlan.Rows[i].Cells[0].Selected)
      

  4.   


    你最后那个空行,在DataTable里面是没有的。问题不在这里。
    是1楼指出的遍历行的时候,你先执行了 rowIndex++; 
    这里就漏掉了row[0]
    excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString(); 
      

  5.   

    楼主你那个GridView控件设置出现左右滚动条是那个属性。