The idea is simple and no matter how much I have searched I couldn't find any similar example.
Using the OleDbDataAdapter class, I am able to achieve the first step:
Read a specific range in a specific tab from an Excel document.
I have to change the Dataset contents as I please and then move to the last step.
The last step is where I struggle: how to write the Dataset back to Excel into the same range and same tab? Basically perform an update of what was read initially with my changes.
My code so far for the first step - the changes in the dataset are not relevant for this example.
static void Main(string[] args)
{
string dq = "\"";
string dataSheetName = "Sheet1";
string fileName = @"C:\Myfile.xlsm";
string excelRange = "A1:E7";
string connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={fileName}; Extended Properties={dq}Excel 12.0;HDR=NO;IMEX=1{dq}";
var adapter = new OleDbDataAdapter($"SELECT * FROM [{dataSheetName}${excelRange}]", connectionString);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "myData");
DataTable dataTable = dataSet.Tables["myData"];
foreach (DataRow row in dataTable.Rows)
{
// change rows contents as I please before writing back to Excel
}
// ------------------------------------------------------------------------------------
// GOAL: To write back to Excel the dataset changed above into the same tab and range
// ------------------------------------------------------------------------------------
}
I am using Office 365.
Can anyone help?
Much appreciated