public static void Main() { //ExStart:1 // The path to the documents directory. string dataDir = Aspose.Cells.Examples.Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); //Define memory stream object System.IO.MemoryStream objImage; //Define web client object System.Net.WebClient objwebClient; //Define a string which will hold the web image url string sURL = "http://www.aspose.com/Images/aspose-logo.jpg"; try { //Instantiate the web client object objwebClient = new System.Net.WebClient(); //Now, extract data into memory stream downloading the image data into the array of bytes objImage = new System.IO.MemoryStream(objwebClient.DownloadData(sURL)); //Create a new workbook Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(); //Get the first worksheet in the book Aspose.Cells.Worksheet sheet = wb.Worksheets[0]; //Get the first worksheet pictures collection Aspose.Cells.Drawing.PictureCollection pictures = sheet.Pictures; //Insert the picture from the stream to B2 cell pictures.Add(1, 1, objImage); //Save the excel file wb.Save(dataDir + "webimagebook.out.xlsx"); } catch (Exception ex) { //Write the error message on the console Console.WriteLine(ex.Message); } //ExEnd:1 }
public static void Run() { //Output directory string outputDir = RunExamples.Get_OutputDirectory(); try { // Define a string which will hold the web image url string sURL = "http://www.aspose.com/Images/aspose-logo.jpg"; // Instantiate the web client object System.Net.WebClient objwebClient = new System.Net.WebClient(); // Now, extract data into memory stream downloading the image data into the array of bytes MemoryStream objImage = new System.IO.MemoryStream(objwebClient.DownloadData(sURL)); // Create a new workbook Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(); // Get the first worksheet in the book Aspose.Cells.Worksheet sheet = wb.Worksheets[0]; // Get the first worksheet pictures collection Aspose.Cells.Drawing.PictureCollection pictures = sheet.Pictures; // Insert the picture from the stream to B2 cell pictures.Add(1, 1, objImage); // Save the excel file wb.Save(outputDir + "outputLoadWebImage.xlsx"); } catch (Exception ex) { // Write the error message on the console Console.WriteLine(ex.Message); } Console.WriteLine("LoadWebImage executed successfully."); }
/// <summary> /// 处理 /// </summary> public void Process() { designer.Process(); //处理图片/超链接 foreach (Worksheet ws in designer.Workbook.Worksheets) //工作表 { foreach (Cell c in ws.Cells) //行 { if (!c.IsMerged) { string s = c.StringValue; if (s.StartsWith(imgTrim)) //图片的处理 { string url = c.StringValue.Replace(imgTrim, ""); try { if (!string.IsNullOrWhiteSpace(url)) { System.IO.Stream objImage; System.Net.WebClient objwebClient; objwebClient = new System.Net.WebClient(); objImage = new System.IO.MemoryStream(objwebClient.DownloadData(url)); double cw = ws.Cells.GetColumnWidth(c.Column); double rh = ws.Cells.GetRowHeight(c.Row); Aspose.Cells.Drawing.PictureCollection pictures = ws.Pictures; pictures.Add(c.Row, c.Column, c.Row + 1, c.Column + 1, objImage); } c.PutValue(""); } catch { c.PutValue("图片" + url + "下载失败"); } } if (s.StartsWith(LinkTrim)) //超链接的处理 { string str = c.StringValue.Replace(LinkTrim, ""); string title, url; string[] arr = str.Split(new string[] { LinkTrim_Title }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length == 2) { title = arr[0]; url = arr[1]; } else { title = url = str; } try { if (!string.IsNullOrWhiteSpace(url)) { int intIndex = ws.Hyperlinks.Add(c.Row, c.Column, c.Row + 1, c.Column + 1, url); Hyperlink hlink = ws.Hyperlinks[intIndex]; hlink.TextToDisplay = title; } } catch { c.PutValue(title); } } } } } //额外处理 if (otherFuns != null && otherFuns.Count > 0) { otherFuns.ForEach(a => a(designer)); } designer.Workbook.Save(_targetpath); isOpen = false; }