This project has moved. For the latest updates, please go here.

LoadFromDataTable bug introduced in 5892cd3f330e

Oct 11, 2012 at 10:05 AM

Returning ExcelBaseRange the toColumn is computed incorrectly.

Structuring the code like LoadFromArrays and LoadFromCollection it should probably look like:

public ExcelRangeBase LoadFromDataTable(DataTable Table, bool PrintHeaders)
{
	if (Table == null)
	{
		throw (new ArgumentNullException("Table can't be null"));
	}

	int col = _fromCol, row = _fromRow;
	if (PrintHeaders)
	{
		foreach (DataColumn dc in Table.Columns)
		{
			// If no caption is set, the ColumnName property is called implicitly.
			_worksheet.Cell(row, col++).Value = dc.Caption;
		}
		row++;
	}
	foreach (DataRow dr in Table.Rows)
	{
		col = _fromCol;
		foreach (object value in dr.ItemArray)
		{
			_worksheet.Cell(row, col++).Value = value;
		}
		row++;
	}
	return _worksheet.Cells[_fromRow, _fromCol, row - 1, col - 1];
}

Cheers,

Andreas