0

我想使用 GemBox 将 excel 文件导入 datagridview。我对 GemBox 相当陌生,我似乎无法弄清楚如何。我只想从单元格“A9”导入到单元格“H32”。

var workbook = ExcelFile.Load(openFileDialog.FileName);

                DataGridViewConverter.ExportToDataGridView(workbook.Worksheets.ActiveWorksheet, this.dgvMain, new ExportToDataGridViewOptions() { ColumnHeaders = true });

谢谢!

4

1 回答 1

0

以下是导入特定单元格的方法:

var workbook = ExcelFile.Load(openFileDialog.FileName);
var worksheet = workbook.Worksheets.ActiveWorksheet;

var options = new ExportToDataGridViewOptions();
options.ColumnHeaders = true;
options.StartRow = 8;        // Start from row 9, index 8.
options.NumberOfRows = 24;   // End with row 32.
options.StartColumn = 0;     // start with column A, index 0.
options.NumberOfColumns = 8; // End with column H.

DataGridViewConverter.ExportToDataGridView(worksheet, this.dgvMain, options);

这里还有另一种方式,你可以通过使用CellRange对象的属性来指定相同的内容:

var workbook = ExcelFile.Load(openFileDialog.FileName);
var worksheet = workbook.Worksheets.ActiveWorksheet;
var range = worksheet.Cells.GetSubrange("A9:H32");

var options = new ExportToDataGridViewOptions();
options.ColumnHeaders = true;
options.StartRow = range.FirstRowIndex;
options.NumberOfRows = range.Height;
options.StartColumn = range.FirstColumnIndex;
options.NumberOfColumns = range.Width;

DataGridViewConverter.ExportToDataGridView(worksheet, this.dgvMain, options);
于 2021-03-15T04:41:46.280 回答