Reputation: 5818
I am using MVC3. I am showing some content and chart(chart using jqplot) in a view. Now I want to export the content and chart to excel. I've already done exporting content to excel. Now I want to add image to excel. I've already converted chart to image and assigned as a source to one image in a view. Now is it possible to add that image to excel content from controller by taking that image source and something like that?
Upvotes: 1
Views: 11025
Reputation: 2545
this is how I place image in excel sheet:
Excel.Range picPosition = xlSShhh.Cells[2, 15];
Excel.Pictures p = xlSShhh.Pictures(System.Type.Missing) as Excel.Pictures;
Excel.Picture pic = null;
try
{
pic = p.Insert(path + pic_name + ".png", System.Type.Missing);
pic.ShapeRange.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoCTrue;
pic.ShapeRange.Width = 180;
pic.ShapeRange.Height = 180;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
hope it helps
Upvotes: 0
Reputation: 3063
You can try to use EPPlus library http://epplus.codeplex.com/.
You do not need to convert chart to image and insert it into Excel, you can create chart with this library same as one in your app.
Example of adding image to Excel using EPPlus (this is only example, not full code):
using (System.Drawing.Image img = /*...Load image here...*/)
{
if (img != null)
{
//set row height to accommodate the picture
ws.Row(currentRow).Height = ExcelHelper.Pixel2RowHeight(pictureHeight + 1);
//add picture to cell
ExcelPicture pic = ws.Drawings.AddPicture("PictureUniqueName", img);
//position picture on desired column
pic.From.Column = pictureCol - 1;
pic.From.Row = currentRow - 1;
pic.From.ColumnOff = ExcelHelper.Pixel2MTU(1);
pic.From.RowOff = ExcelHelper.Pixel2MTU(1);
//set picture size to fit inside the cell
pic.SetSize(pictureWidth, pictureHeight);
}
}
Upvotes: 1