#region 測試EPPlus插入圖片 public static void Createsheel2() { WebClient client = new WebClient(); var downloadUrl = FileOperater.GetDownloadUrl("f736cf1950 ...
#region 測試EPPlus插入圖片
public static void Createsheel2()
{
WebClient client = new WebClient();
var downloadUrl = FileOperater.GetDownloadUrl("f736cf1950e64400b3ba69001b0b5a1b");
var excelData = client.DownloadData(downloadUrl);
var bytes = client.DownloadData("http://img1.soufunimg.com/message/images/card/tuanproj/201511/2015112703584458_s.jpg");
using (Stream ms = new MemoryStream(excelData))
{
ExcelPackage excel;
try
{
excel = new ExcelPackage(ms);
ExcelWorksheet ws = excel.Workbook.Worksheets[1];
ExcelWorkbook wb = excel.Workbook;
for (int i = 1; i < 7; i++)
{
excel.Workbook.Worksheets.Copy(ws.Name, ws.Name+"_"+i);//複製模板sheet
var drawings = excel.Workbook.Worksheets[i].Drawings;
foreach (ExcelDrawing excelDrawing in drawings)
{
ExcelPicture excelPicture = excelDrawing as ExcelPicture;
var name = excelPicture.Name;
//var aa = excelPicture.Image;
//var cc = excelPicture.ImageFormat;
//excelPicture.Image.Save(imgms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
int vSheetCount = excel.Workbook.Worksheets.Count; //獲取總Sheet頁
for (int i = 1; i <= vSheetCount; i++)
{
ws = excel.Workbook.Worksheets[i];
using (Stream imgms = new MemoryStream(bytes))
{
System.Drawing.Bitmap myimge = new System.Drawing.Bitmap(imgms);
ExcelPicture picture = null;
for (int j = 0; j <4; j++)
{
picture = ws.Drawings.AddPicture("a" + j, myimge);//插入圖片
if (j >1)
{
picture.SetPosition(560, 250 * (j - 2) + (j - 1) * 40);//設置圖片的位置
}
else
{
picture.SetPosition(330, 250 * j +(j+1)*40);//設置圖片的位置
}
// picture.SetPosition(100*j, 100*j);//設置圖片的位置
picture.SetSize(250, 220);//設置圖片的大小
}
}
}
FileOperater.FileUpdate("f736cf1950e64400b3ba69001b0b5a1b", excel.GetAsByteArray());
}
catch (Exception ex)
{
throw;
}
}
}
#endregion