将数据网格视图导出为C#窗体中的Excel工作表行和标题

时间:2018-04-11 17:36:26

标签: excel winforms c#-4.0 datagridview

我需要帮助从datagridview列和标题导出数据,下面我将添加我用于从datagridview导出到excel的代码,它可以将所有数据从datagridview发送到excel表单问题是标题没有发送到工作表。你们可以帮助我在这里添加的那篇文章,所以标题也显示在表格中。

<!doctype html>
<html>
    <head>
        <title>Test</title>
         <meta charset="utf-8"/>
         <script src="jquery-3.3.1.min.js"></script>
         <script src="js/testjs.js"></script>
   </head>
   <body>
       <h1>first test</h1>
       <p>second test</p>

   <!-- this works only here but not in external js file
   <script>
    $("h1").hide();
   </script>
   -->
   </body>
</html>

1 个答案:

答案 0 :(得分:1)

 private void ImportDataGridViewDataToExcelSheet()
        {

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);   
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


            for (int x = 1; x < dataGridView_ShowAllData.Columns.Count + 1; x++)
            {
                xlWorkSheet.Cells[1, x] = dataGridView_ShowAllData.Columns[x - 1].HeaderText;
            }

            for (int i = 0; i < dataGridView_ShowAllData.Rows.Count; i++)
            {
                for (int j = 0; j < dataGridView_ShowAllData.Columns.Count; j++)
                {                  
                    xlWorkSheet.Cells[i + 2, j + 1] = dataGridView_ShowAllData.Rows[i].Cells[j].Value.ToString();                                 
                }
            }          

            var saveFileDialoge = new SaveFileDialog();
            saveFileDialoge.FileName = "ReportView";
            saveFileDialoge.DefaultExt = ".xlsx";
            if (saveFileDialoge.ShowDialog() == DialogResult.OK)
            {
                xlWorkBook.SaveAs(saveFileDialoge.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }


            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        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();
            }
        }

        private void toolStripMenuItem_ImportDGVToExcel_Click(object sender, EventArgs e)
        {
            ImportDataGridViewDataToExcelSheet();
        }