2012-11-01 67 views
1

我正在使用MVC3。我在視圖中顯示了一些內容和圖表(使用jqplot的圖表)。現在我想將內容和圖表導出爲ex​​cel。我已經完成了將內容導出爲ex​​cel的工作。現在我想添加圖像到Excel。我已經將圖表轉換爲圖像,並將其作爲源視圖分配給一個圖像。現在是否有可能通過拍攝該圖像源以及類似的東西來添加該圖像以優化控制器的內容?在Excel文件中放置圖像

回答

1

您可以嘗試使用EPPlus庫http://epplus.codeplex.com/

您不需要將圖表轉換爲圖像並將其插入到Excel中,您可以使用此庫創建圖表,使其與您應用中的圖表相同。使用EPPlus添加圖像到Excel的

實施例(這僅是示例,而不是全碼):

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); 
} 
} 
+0

謝謝,我已經用jqplot jquery插件創建了圖表。現在我不想再次從頭開始。現有的圖表可以處理嗎? – DON

+2

任何人都在乎Excel助手是什麼?這裏是http://epplus.codeplex.com/discussions/229134 – Esen

+0

在GDI +錯誤中產生泛型 –

0

這就是我把圖像中的excel表:

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); 
} 

希望它有助於