You must add the Microsoft. Office. InterOP. Excel and Microsoft. Office. Core namespaces and references.
For example, Office 2007 under Windows 7 is a reference to a COM component and A. NET component, for example:
Microsoft Excel Object Library and Microsoft Object Library are added to my computer.
CodeAs follows:
Using system; using system. data; using system. configuration; using system. collections; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. reflection; using system. io; using Microsoft. office. interOP. excel; using Microsoft. office. core; public partial class _ default: system. web. u I. page {protected void page_load (Object sender, eventargs e) {} protected void button#click (Object sender, eventargs e) {Microsoft. office. interOP. excel. application APP = new application (); app. visible = false; app. displayalerts = false; workbook = app. workbooks. open (server. mappath ("test _ excel.xls"), missing. value, missing. value, missing. value, missing. value, missing. value, missing. valu E, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value, missing. value); // It is set to operate the sheet1 worksheet = (worksheet) Workbook of the current workbook. worksheets [1]; string imgpath = server. mappath ("test_img.jpg"); byte [] bytesarr = pictobytearr (imgpath); system. drawing. image BMP = returnphoto (bytesarr); // the image data int x = 1; int y = 1; range rangetemp = Worksheet. Get_range (range) worksheet. cells [x, y], (range) worksheet. cells [x, y]); // rangetemp. select (); float picleft, pictop; picleft = convert. tosingle (rangetemp. left) + 2; pictop = convert. tosingle (rangetemp. top) + 1; worksheet. shapes. addpicture (imgpath, Microsoft. office. core. msotristate. msofalse, Microsoft. office. core. msotristate. msotrue, picleft, pictop, BMP. width * 8/10, BMP. height * 8/10); workbo OK. save (); workbook. close (null, null, null); app. workbooks. close (); app. quit (); system. runtime. interopservices. marshal. releasecomobject (rangetemp); system. runtime. interopservices. marshal. releasecomobject (worksheet); system. runtime. interopservices. marshal. releasecomobject (workbook); system. runtime. interopservices. marshal. releasecomobject (APP); system. GC. collect (); system. GC. waitforpendingfinalizers ();} /// <Summary> /// convert the image to a byte array /// </Summary> /// <Param name = "path"> path of the image </param> /// <returns> byte array </returns> Public byte [] pictobytearr (string path) {filestream FS = new filestream (path, filemode. open); // write the image to the stream. Int filelength = 0; filelength = (INT) FS. length; // obtain the object Length byte [] bytearr = new byte [filelength]; // create a byte array FS. read (bytearr, 0, filelength); // read FS by byte stream. close (); Return bytearr ;} /// <summary> /// the parameter is byte returned image /// </Summary> /// <Param name = "bytearr"> byte array </param> // /<returns> image </returns> public system. drawing. image returnphoto (byte [] bytearr) {memorystream MS = new memorystream (bytearr); system. drawing. image IMG = system. drawing. image. fromstream (MS); Ms. close (); Return IMG ;}}