为什么无法检索datagridview中的第一行C#我的代码结果除了第一行以外的所有数据? [英] Why can't retrieve first row in datagridview C# the results of my code all data except the first row ?

查看:96
本文介绍了为什么无法检索datagridview中的第一行C#我的代码结果除了第一行以外的所有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么无法检索datagridview中的第一行c#我的代码结果除了第一行以外的所有数据?

i希望将DataSet或grideview的当前数据复制到C:\根目录下的文件:使用excel文件



我尝试过:



Why can't retrieve first row in datagridview c# the results of my code all data except the first row ?
i want copy the current data of the DataSet or grideview to files on C:\ root directory:using excel file

What I have tried:

private void ExportToExcel()
    {
        // Creating a Excel object.
        Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
        Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

        try
        {

            worksheet = workbook.ActiveSheet;

            worksheet.Name = "ExportedFromDatGrid";


            int cellRowIndex = 1;
            int cellColumnIndex = 1;

            //Loop through each row and read value from each column.
            for (int i = 0; i < dg1.Rows.Count  ; i++)
            {
                for (int j =0; j < dg1.Columns.Count -1; j++)
                {
                    // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                    if (cellRowIndex == 1)
                    {
                        worksheet.Cells[cellRowIndex, cellColumnIndex] = dg1.Columns[j].HeaderText;
                    }
                    else
                    {
                        worksheet.Cells[cellRowIndex, cellColumnIndex] = dg1.Rows[i].Cells[j].Value.ToString();
                    }
                    cellColumnIndex++;
                }
                cellColumnIndex = 1;
                cellRowIndex++;
            }


            //Getting the location and file name of the excel to save from user.
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
            saveDialog.FilterIndex = 2;

            if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                workbook.SaveAs(saveDialog.FileName);
                MessageBox.Show("Export Successful");
            }
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            excel.Quit();
            workbook = null;
            excel = null;
        }
    }



谢谢


thank

推荐答案

引用:

为什么无法检索datagridview中的第一行c#我的代码的结果除了第一行以外的所有数据?

Why can't retrieve first row in datagridview c# the results of my code all data except the first row ?



因为这是您在代码中请求的内容。而不是打印第一行,而是打印标题。

您需要在处理行之前打印标题。



尝试这种方式:


Because that is what you requested in your code. Instead of printing the first row, you print the headers.
You need to print the headers before handling the rows.

Try this way:

private void ExportToExcel()
{
  // Creating a Excel object.
  Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
  Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
  Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

  try
  {

    worksheet = workbook.ActiveSheet;

    worksheet.Name = "ExportedFromDatGrid";


    int cellRowIndex = 1;
    int cellColumnIndex = 1;

    for (int j =0; j < dg1.Columns.Count -1; j++)
    {
      // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
      worksheet.Cells[cellRowIndex, cellColumnIndex] = dg1.Columns[j].HeaderText;
      cellColumnIndex++;
    }
    cellColumnIndex = 1;
    cellRowIndex++;
    //Loop through each row and read value from each column.
    for (int i = 0; i < dg1.Rows.Count  ; i++)
    {
      for (int j =0; j < dg1.Columns.Count -1; j++)
      {
        // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
        worksheet.Cells[cellRowIndex, cellColumnIndex] = dg1.Rows[i].Cells[j].Value.ToString();
        cellColumnIndex++;
      }
      cellColumnIndex = 1;
      cellRowIndex++;
    }


    //Getting the location and file name of the excel to save from user.
    SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
    saveDialog.FilterIndex = 2;

    if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
      workbook.SaveAs(saveDialog.FileName);
      MessageBox.Show("Export Successful");
    }
  }
  catch (System.Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
  finally
  {
    excel.Quit();
    workbook = null;
    excel = null;
  }
}


这篇关于为什么无法检索datagridview中的第一行C#我的代码结果除了第一行以外的所有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆