View code
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using Excel = Microsoft. office. interOP. excel; using system. diagnostics; namespace Excel convert image {public partial class form1: FORM {public form1 () {initializecomponent ();} private void form1_load (Object sender, eventargs e) {kill Program (); getexcel (@ "D: \ test \ 1.xls");} public static void killprogram () {foreach (Process in process. getprocesses () {If (process. processname = "Excel") // name of the program to be terminated {process. kill () ;}} public static string getexcel (string excelfilepath) {string saveexceljpg = @ "D: \ test \ 1.jpg"; excel. application APP = new Microsoft. office. interOP. excel. application (); object objmis = type. missing; e Xcel. workbook singleexcel = app. workbooks. open (metadata, objmis, objmis); try {// wsheet. usedrange. select (); For (INT I = 1; I <= singleexcel. worksheets. count; I ++) {excel. worksheet wsheet = (Excel. worksheet) singleexcel. worksheets [I]; object ranobj = dbnull. value; // set the selected cell and copy it out. Wsheet. get_range ("A1", "aa22"). Copy (ranobj); // select all cells and copy them all. // Wsheet. usedrange. copy (objmis); // clipboard. setdataobject (objmis); idataobject idata = clipboard. getdataobject (); bitmap bits = (Bitmap) idata. getdata (dataformats. bitmap); bitmap mybitmap = new Bitmap (bits. width, Bits. height); graphics G = graphics. fromimage (mybitmap); G. drawimage (bits, 0, 0); mybitmap. save (string. format (saveexceljpg, guid. newguid (); clipboard. clear (); mybitmap. dispose (); bits. dispose () ;}} catch (exception Excel) {// throw Excel;} finally {singleexcel. close (objmis, objmis, objmis); app. quit ();} return string. empty ;}}}