What my app does is allow the user to upload a raw excel document into a GridControl where it is then cleaned to match the format of this document, which is then saved to the document on the Sharepoint site. It does save the document under the same name everytime.
I am using version 17.4.0.39.
ExcelEngine engine = new ExcelEngine();
IApplication application = engine.Excel;
application.UseFastRecordParsing = true;
var stream = await graphClient.Sites["Example.sharepoint.com,c6df194a-5585-460792-9d79-tc6ff711a050,e7f5trc1f-abc-411h-abda-ggged76102c4"].Drive.Root.ItemWithPath(templateBox.Text + ".xlsx").Content.Request().GetAsync();
stream.Position = 0;
MemoryStream file = new MemoryStream();
stream.CopyTo(file);
file.Position = 0;
//Load the stream into IWorkbook
IWorkbook doc = application.Workbooks.Open(file);
IWorksheet sheet = doc.Worksheets[0];
sheet.Clear();
sheet.ListObjects.Clear();
sheet.UsedRange.Clear();
MemoryStream outputStream = new MemoryStream();
doc.SaveAs(outputStream);
//Set the stream position as '0'
outputStream.Position = 0;
for (int n = 0; n < sheet.Names.Count; n++)
{
sheet.Names.RemoveAt(n);
}
for (int i = 0; i <= gridControl4.RowCount; i++)
{
for (int j = 1; j <= gridControl4.ColCount; j++)
{
sheet.Range[i + 1, j].Value = gridControl4[i, j].Text;
}
}
//currency formatting
try
{
doc.Styles.Remove("ColumnStyle");
}
catch { }
Syncfusion.XlsIO.IStyle columnStyle = doc.Styles.Add("ColumnStyle");
//columnStyle.IncludeNumberFormat = true;
columnStyle.NumberFormat = "$#,##0.00";
//columnStyle.Color = Color.Blue;
sheet.SetDefaultColumnStyle(19, 19, columnStyle);
sheet.SetDefaultColumnStyle(20, 20, columnStyle);
sheet.SetDefaultColumnStyle(21, 21, columnStyle);
sheet.SetDefaultColumnStyle(22, 22, columnStyle);
sheet.SetDefaultColumnStyle(23, 23, columnStyle);
sheet.SetDefaultColumnStyle(24, 24, columnStyle);
sheet.SetDefaultColumnStyle(25, 25, columnStyle);
sheet.SetDefaultColumnStyle(26, 26, columnStyle);
sheet.SetDefaultColumnStyle(27, 27, columnStyle);
sheet.SetDefaultColumnStyle(28, 28, columnStyle);
sheet.SetDefaultColumnStyle(29, 29, columnStyle);
sheet.SetDefaultColumnStyle(30, 30, columnStyle);
try
{
var rng = sheet.Range[1, 1, gridControl4.RowCount + 1, gridControl4.ColCount];
// address = rng.AddressGlobal.ToString();
Syncfusion.XlsIO.IListObject tbl = sheet.ListObjects.Create("dev", rng);
tbl.BuiltInTableStyle = TableBuiltInStyles.TableStyleLight8;
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
//save
doc.SaveAs(outputStream);
//Set the stream position as '0'
outputStream.Position = 0;
await graphClient.Sites["Example.sharepoint.com,c6df194a-5585-460792-9d79-tc6ff711a050,e7f5trc1f-abc-411h-abda-ggged76102c4"].Drive.Root.ItemWithPath(templateBox.Text + ".xlsx").Content.Request().PutAsync<Microsoft.Graph.DriveItem>(outputStream);
doc.Close(false);
engine.Dispose();