简体   繁体   English

使用 GemBox 将数据从 DataGridView 导入现有 Excel 文件

[英]Importing data from DataGridView to existing Excel file using GemBox

I am fairly new to programming and GemBox.我对编程和 GemBox 还很陌生。 I found this code that inserts a data from a DataGridView to an existing Excel sheet with headers and footers.我发现这段代码将数据从 DataGridView 插入到带有页眉和页脚的现有 Excel 工作表中。 What happens with the code is it replaces the exisiting excel file totally and removes all the headers and footers.代码发生的情况是它完全替换了现有的 excel 文件并删除了所有页眉和页脚。 What I want to do is just insert the data starting from cell A:9 without removing the pre-existing data from other excel cells.我想要做的只是从单元格 A:9 开始插入数据,而不从其他 excel 单元格中删除预先存在的数据。 Is there anyway to do this using GemBox?反正有没有使用 GemBox 来做到这一点?

private void replace_Click(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "XLSX files (*.xlsx)|*.xlsx";
            saveFileDialog.FilterIndex = 3;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                var workbook = new ExcelFile();
                var worksheet = workbook.Worksheets.Add("Sheet1");

                var options = new ImportFromDataGridViewOptions();
                options.ColumnHeaders = false;
                options.StartRow = 8;       
                options.StartColumn = 0;

                DataGridViewConverter.ImportFromDataGridView(worksheet, this.dataGridView1, options);

                workbook.Save(saveFileDialog.FileName);
            }
        }

Any help would greatly be appreciated.任何帮助将不胜感激。

Use this to import the data from DataGridView to an existing Excel sheet:使用它将数据从DataGridView导入现有的 Excel 表:

private void replace_Click(object sender, EventArgs e)
{
    var saveFileDialog = new SaveFileDialog();
    saveFileDialog.Filter = "XLSX files (*.xlsx)|*.xlsx";

    if (saveFileDialog.ShowDialog() == DialogResult.OK)
    {
        var workbook = ExcelFile.Load("Path to your existing Excel file.");
        var worksheet = workbook.Worksheets.ActiveWorksheet;
        var options = new ImportFromDataGridViewOptions("A9");

        DataGridViewConverter.ImportFromDataGridView(worksheet, this.dataGridView1, options);
        workbook.Save(saveFileDialog.FileName);
    }
}

Note, this will import the data starting from cell A9 and replace any existing data that is inside the range where the import was done.请注意,这将从单元格 A9 开始导入数据,并替换导入完成范围内的任何现有数据。

In other words, if you have an Excel file that already has some data in cells A9, B9, etc. they will end up being overridden.换句话说,如果您有一个 Excel 文件,该文件已经在单元格 A9、B9 等中包含一些数据,那么它们最终将被覆盖。

If you don't want that, then insert empty rows to make room for DataGridView data, like this:如果您不希望这样,则插入空行以为DataGridView数据腾出空间,如下所示:

worksheet.Rows.InsertEmpty(8, this.dataGridView1.Rows.Count);

var options = new ImportFromDataGridViewOptions("A9");
DataGridViewConverter.ImportFromDataGridView(worksheet, this.dataGridView1, options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM