Harsh Jani

Harsh Jani

  • NA
  • 34
  • 53.4k

Rad an Excel Chart using NPOI Library

Jul 23 2015 3:29 AM
I am reading an excel chart using interop and exporting in to a word file. Now, I want to achieve the same purpose using NPOI library. The code which I have used to read an excel chart is as follows: -
 
  
 
Microsoft.Office.Interop.Word.Table myWordable;
Microsoft.Office.Interop.Word.Range myWordRange;
FileStream myfile = Path.Combine(filepath, "Test_Excel.xlsx");
using (ExcelPackage package = new ExcelPackage(myfile))
{
ExcelWorksheet myws = package.Workbook.Worksheets[1];
myWordTable = new_mach.myWordApplication.ActiveDocument.Tables[5];
myWordRange = new_mach.myWordTable.Cell(2, 1).Range;
Microsoft.Office.Interop.Word.InlineShape objshape = new_mach.myWordDoc.InlineShapes.AddChart(Microsoft.Office.Core.XlChartType.xlXYScatterSmoothNoMarkers, new_mach.myWordRange);
Microsoft.Office.Interop.Word.Chart mychrt = objshape.Chart;
Workbook wb = mychrt.ChartData.Workbook;
Worksheet datasheet = wb.Worksheets["Sheet1"];
Microsoft.Office.Interop.Excel.Range myrng = datasheet.get_Range("A1","D10");
Microsoft.Office.Interop.Excel.ListObject tbl = datasheet.ListObjects["Table1"];
tbl.Resize(myrng);
mychrt.HasLegend = true;
mychrt.Legend.Position = Microsoft.Office.Interop.Word.XlLegendPosition.xlLegendPositionBottom;
mychrt.Legend.Format.Line.Style = Microsoft.Office.Core.MsoLineStyle.msoLineSingle;
mychrt.Legend.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue;
Microsoft.Office.Interop.Word.Series sr = mychrt.SeriesCollection(1);
sr.Border.Color = Color.Red;
sr.Name = "Charging";
sr = mychrt.SeriesCollection(2);
sr.Name = "Discharging";
sr.Border.Color = Color.Navy;
Microsoft.Office.Interop.Word.Axis vertaxis = (Microsoft.Office.Interop.Word.Axis)mychrt.Axes(Microsoft.Office.Interop.Word.XlAxisType.xlValue, Microsoft.Office.Interop.Word.XlAxisGroup.xlPrimary);
vertaxis.HasMajorGridlines = true;
vertaxis.HasTitle = true;
vertaxis.AxisTitle.Text = "Pol-Depol Currents (microAmp)";
vertaxis.HasMinorGridlines = true;
vertaxis.LogBase = 10;
Microsoft.Office.Interop.Word.Axis Horiaxis = (Microsoft.Office.Interop.Word.Axis)mychrt.Axes(Microsoft.Office.Interop.Word.XlAxisType.xlCategory, Microsoft.Office.Interop.Word.XlAxisGroup.xlPrimary);
Horiaxis.HasMajorGridlines = true;
Horiaxis.HasTitle = true;
Horiaxis.AxisTitle.Text = "time (sec)";
Horiaxis.HasMinorGridlines = true;
Horiaxis.LogBase = 10;
Horiaxis.MinimumScale = 1;
Horiaxis.MaximumScale = 1000;
Horiaxis.TickLabelPosition = Microsoft.Office.Interop.Word.XlTickLabelPosition.xlTickLabelPositionLow;
}
 
I want to achieve the same purpose, using NPOI library. I did go through a few threads, but could not find a relevant solution to my problem. Can anyone help me write equivalent code for this using NPOI library?