以下是导入特定单元格的方法:
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);