不从dataGridView正确导出列名称为excel

时间:2015-08-28 08:01:35

标签: c# sql excel datagridview

我正在尝试将数据从dataGridView导出到excel。它运行良好,但我无法正确导出列名称。问题是列名称应为:id,它是idDataGridViewTextBoxColumn。另一个问题是不会导出数据库中的第一个条目。我的意思是,而不是我在数据库中的第一个条目,它放置列名称,然后,它只放置第二个条目,然后是第三个,依此类推。谢谢!

private void button3_Click_1(object sender, EventArgs e)
        {
             var connString = (@"Data Source=" + Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + @"\Angajati.sdf");
            using (var conn = new SqlCeConnection(connString))
            {
                try
                {
                    conn.Open();
                    var query = "SELECT * FROM info ";
                    var command = new SqlCeCommand(query, conn);
                    var dataAdapter = new SqlCeDataAdapter(command);
                    var dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;

            Int16 i, j;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            for (i = 0; i <= dataGridView1.RowCount - 2; i++)
            {
                for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                {
                    xlWorkSheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Value.ToString();
                }
            }

            //adds column names to excel
            string[] colNames = new string[dataGridView1.Columns.Count];
            int col = 0;

            foreach (DataGridViewColumn dc in dataGridView1.Columns)
                colNames[col++] = dc.Name;

            char lastColumn = (char)(65 + dataGridView1.Columns.Count - 1);

            xlWorkSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
            xlWorkSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
            xlWorkSheet.get_Range("A1", lastColumn + "1").VerticalAlignment
                        = Excel.XlVAlign.xlVAlignCenter;

            xlWorkBook.SaveAs(@"C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\db.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
            MessageBox.Show("Salvat cu succes");
        }



        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

2 个答案:

答案 0 :(得分:1)

对于列名称问题,

替换这个:

foreach (DataGridViewColumn dc in dataGridView1.Columns)
    colNames[col++] = dc.Name;

用这个:

foreach (DataGridViewColumn dc in dataGridView1.Columns)
    colNames[col++] = dc.HeaderText;

更新:对于第一个缺失的行,请进行此更改:

        for (i = 0; i <= dataGridView1.RowCount - 2; i++)
        {
            for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
            {
                //xlWorkSheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Value.ToString();
                xlWorkSheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
            }
        }

答案 1 :(得分:0)

首先,您必须找出问题发生的

  1. 从GridView获取数据
  2. 将数据发送到excel
  3. 使用debuger,设置断点并检查中间值,例如dataTablecolNames