将Datagridview导出到现有的Excel文件C#

时间:2017-01-22 15:29:20

标签: c# excel gridview datagridview export

我的软件有问题。我想将Datagridview导出到现有的excel文件。我的软件将excel文件导入到datagridview,现在我想对gridview进行更改,然后将其安全保存。我已经尝试了很多选项,但是我想将它添加到现有文件中,大多数教程都是关于如何将它导出到新文件中。有人能解决我的问题吗?我还想到将gridview导出到一个新的并且在那之后复制,将新的一个页面复制到现有的一个。但同样的问题,我找不到任何东西。我对这整个编程事物都很陌生,而且只是我公司的一名学员。如果有人可以帮助我,那将是非常好的!如果我的英语不好,我就住在德国。谢谢:)

2 个答案:

答案 0 :(得分:1)

记得添加您的参考(右键单击您的项目并选择 CON 参考并查找 Microsoft Excel 15.0 对象库)

private void btnExport_Click(object sender, EventArgs e)
{ 
    Microsoft.Office.Interop.Excel.Application xlworksheet = 
        new Microsoft.Office.Interop.Excel.Application();
           
    xlworksheet.Application.Workbooks.Open(
        @"C:\Users\BLANKSOASHEET.xlsx", Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing);
           
    try
    {
       for(int i=1; i<DgvSOA.Columns.Count+1; i++)
       {
           worksheet.Cells[1,i] = datagridView1.Columns[i - 1].HeaderText;
       }       

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

答案 1 :(得分:0)

这将保存到特定位置的特定Excel文件。

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";
            string sql = "SELECT * FROM Authors";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
            DataSet ds = new DataSet();
            connection.Open();
            dataadapter.Fill(ds, "Authors_table");
            connection.Close();
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "Authors_table";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            Int16 i, j;

            xlApp = new Excel.ApplicationClass();
            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();
                }
            }

            xlWorkBook.SaveAs(@"c:\csharp.net-informations.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);
        }

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