Insert a Chart in Excel Spreadsheet using EPPlus Library.

EPPlus is .NET Library that makes it very easy to manipulate Excel programmatically. It is based on Open XML.

https://epplus.codeplex.com/

Below is the sample code we can use to insert a chart in a excel spreadsheet.


public void UpdateExcelUsingEPPlus(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);

ExcelPackage p = new ExcelPackage(fileInfo);

// access the first sheet named Sheet1
ExcelWorksheet myWorksheet = p.Workbook.Worksheets["Sheet1"];

// specify cell values to be used for generating chart.
myWorksheet.Cells["C2"].Value = 10;
myWorksheet.Cells["C3"].Value = 40;
myWorksheet.Cells["C4"].Value = 30;

myWorksheet.Cells["B2"].Value = "Yes";
myWorksheet.Cells["B3"].Value = "No";
myWorksheet.Cells["B4"].Value = "NA";

// add chart of type Pie.
var myChart = myWorksheet.Drawings.AddChart("chart", eChartType.Pie);

// Define series for the chart
var series = myChart.Series.Add("C2: C4", "B2: B4");
myChart.Border.Fill.Color = System.Drawing.Color.Green;
myChart.Title.Text = "My Chart";
myChart.SetSize(400, 400);

// Add to 6th row and to the 6th column
myChart.SetPosition(6, 0, 6, 0);

p.Save();

}

The output –

If we want to do it using Open XML SDK without using EPPlus Library we can refer to the below article.

https://msdn.microsoft.com/en-us/library/office/cc820055.aspx

Hope it helps..

Insert Formula in Cell using C# (OpenXML and EPPlus Library)

Sharing the sample code, we can use to insert forumala in a particular cell in Excel Spreadsheet.

First using Open XML SDK


public static void UpdateExcelUsingOpenXMLSDK(string fileName)
{
// Open the document for editing.
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(fileName, true))
{
// Access the main Workbook part, which contains all references.
WorkbookPart workbookPart = spreadSheet.WorkbookPart;
// get sheet by name
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").FirstOrDefault();

// get worksheetpart by sheet id
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id.Value) as WorksheetPart;

// The SheetData object will contain all the data.
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild();

Cell formulaCell = InsertCellInWorksheet("B", 10, worksheetPart);
formulaCell.DataType = new EnumValue<CellValues>(CellValues.Number);
formulaCell.CellFormula = new CellFormula("SUM(B2:B7)");

// Save the worksheet.
worksheetPart.Worksheet.Save();

// for recacluation of formula
spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;

}
}

private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
{
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
string cellReference = columnName + rowIndex;

// If the worksheet does not contain a row with the specified row index, insert one.
Row row;
if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
{
row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
}
else
{
row = new Row() { RowIndex = rowIndex };
sheetData.Append(row);
}

// If there is not a cell with the specified column name, insert one.
if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
{
return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
}
else
{
// Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
Cell refCell = null;
foreach (Cell cell in row.Elements<Cell>())
{
if (cell.CellReference.Value.Length == cellReference.Length)
{
if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
{
refCell = cell;
break;
}
}
}

Cell newCell = new Cell() { CellReference = cellReference };
row.InsertBefore(newCell, refCell);

worksheet.Save();
return newCell;
}
}

Using EPPlus

https://www.nuget.org/packages/EPPlus/


public void UpdateExcelUsingEPPlus(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);
ExcelPackage p = new ExcelPackage(fileInfo);
ExcelWorksheet myWorksheet = p.Workbook.Worksheets["Sheet1"];

myWorksheet.Cells["B15"].Formula = "SUM(B2:B7)";

p.Save();

}

Hope it helps..

Configuring Live Assist (Preview) for Dynamics 365.

Let us see how we can configure Live Assist step by step.

First, go to Applications tab of the Dynamics 365 Administration Center.

Select Live Assist for Dynamics 365 and click on Manage.

Click on Accept to give permission to the app.

Select the Dynamics 365 Instance and provide your email id, accept the terms and conditions and click on Submit.

This configures the Live Assist on the specified Dynamics 365 Instance and also sends the email to the email id specified.

Once the configuration is done, we can see the new Live Assist section added in our Settings area.

There we can click on Admin URL of Live Assist for further configuration.

Clicking on it opens the Admin Center.

Click on Confirm and Authorize.

The Dashboard of the admin center.

The Get Started page.

Code Snippet that we can use to enable Live Assist in web site.

Live Assist adds a new panel on the right inside CRM.

To test it we can make use of Demo Site provided along with Live Assist.

The agent within CRM can click on Conversation Icon and “Grab a chat” to start conversation.

Now suppose we want to configure our CRM Portal to use it. For this we need to copy the code snippet provided earlier. Go to the Header web template of our Web Site and paste the JavaScript code to our Header’s source code.

This enables Live Assist in the portal.

Communication between the CRM user and the Portal user.

The helpful links and video

https://neilparkhurst.com/2017/04/09/cafex-live-assist-my-initial-install-with-usd/

https://blogs.technet.microsoft.com/lystavlen/2017/03/16/live-assist-for-dynamics-365-first-look/

Hope it helps..

Update Cell value in Excel Spreadsheet using C# (Open XML and EPPlus library)

Sharing a sample code that updates a particular cell’s value in Excel Spreadsheet.

Here we are updating cell B4

First using Open XML SDK


public static void UpdateExcelUsingOpenXMLSDK(string fileName)
{
// Open the document for editing.
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(fileName, true))
{
// Access the main Workbook part, which contains all references.
WorkbookPart workbookPart = spreadSheet.WorkbookPart;
// get sheet by name
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").FirstOrDefault();

// get worksheetpart by sheet id
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id.Value) as WorksheetPart;

// The SheetData object will contain all the data.
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild();

Cell cell = GetCell(worksheetPart.Worksheet, "B", 4);

cell.CellValue = new CellValue("10");
cell.DataType = new EnumValue<CellValues>(CellValues.Number);

// Save the worksheet.
worksheetPart.Worksheet.Save();

// for recacluation of formula
spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;

}
}

private static Cell GetCell(Worksheet worksheet,
string columnName, uint rowIndex)
{
Row row = GetRow(worksheet, rowIndex);

if (row == null) return null;

var FirstRow = row.Elements<Cell>().Where(c => string.Compare
(c.CellReference.Value, columnName +
rowIndex, true) == 0).FirstOrDefault();

if (FirstRow == null) return null;

return FirstRow;
}

private static Row GetRow(Worksheet worksheet , uint rowIndex)
{
Row row = worksheet.GetFirstChild<SheetData>().
Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);
if (row == null)
{
throw new ArgumentException(String.Format("No row with index {0} found in spreadsheet", rowIndex));
}
return row;
}

Now the same code using EPPlus

https://www.nuget.org/packages/EPPlus/


public void UpdateExcelUsingEPPlus(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);
ExcelPackage p = new ExcelPackage(fileInfo);
ExcelWorksheet myWorksheet = p.Workbook.Worksheets["Sheet1"];
myWorksheet.Cells[4, 2].Value = 10;
p.Save();

}

Hope it helps..