View code
/** Made by anby * 2012-10.31 * good luck !~ * Convert JPG files in Excel * Remember to reference Microsoft. office. interOP. excel; */using system. drawing; using system. LINQ; using system. windows. forms; using Excel = Microsoft. office. interOP. excel; using system. diagnostics; using system. io; namespace excelconverthelper {class exceltoimage {// <summary> // open an Excel file /// </Summary> /// <returns> </returns> Public static string openexcelfile () {openfiledialog OPF = new Openfiledialog (); string excelfilepath = ""; OPF. filter = "Excel file (*. XLS) | *. xls "; OPF. filterindex = 1; if (OPF. showdialog () = dialogresult. OK) {excelfilepath = OPF. filename;} else {excelfilepath = "";} return excelfilepath;} public static string saveexcelfile () {folderbrowserdialog FBD = new folderbrowserdialog (); string excelsavepath = ""; if (FBD. showdialog () = dialogresult. OK) {excelsa Vepath = FBD. selectedpath;} else {excelsavepath = "";} return excelsavepath ;} /// <summary> /// end the opened Excel process /// </Summary> Public static void killprogram () {foreach (Process in process. getprocesses () {If (process. processname = "Excel") // name of the program to be terminated {process. kill () ;}} public static void filecreate (string path) {directoryinfo dirinfo = new directoryinfo (PATH); If (dirinfo. exists = = False) {dirinfo. create ();} else {dirinfo. delete (); dirinfo. create ();}} /// <summary> /// convert the specified range cell EXCEL to JPG // </Summary> /// <Param name = "excelfilepath"> Excel file path, ex: @ "C: \ 1.xls" </param> // <Param name =" saveexceljpg "> Save image path, EX: @" C: \ test "</param> // <Param name =" cell1 "> </param> // <Param name =" cell2 "> </param> Public static void exceltoimages (string excelfilepath, string saveexceljpg, STR Ing cell1, string cell2) {killprogram (); filecreate (saveexceljpg); excel. application APP = new Microsoft. office. interOP. excel. application (); object objmis = system. type. missing; excel. workbook singleexcel = app. workbooks. open (metadata, objmis, objmis); try {// wsheet. usedrange. select (); For (INT I = 1; I <= singleexcel. worksheets. count; I ++) {application. doevents (); excel. worksheet wsheet = (Excel. worksheet) singleexcel. worksheets [I]; object ranobj = dbnull. value; // set the selected cell and copy it out. Wsheet. get_range (cell1, cell2). Copy (ranobj); // select all cells and copy them all. // Wsheet. usedrange. copy (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); string savepath = saveexceljpg + wsheet. name + ". jpg "; mybitmap. save (savepath, system. drawing. imaging. imageformat. JPEG); clipboard. clear (); Mybitmap. Dispose (); bits. Dispose ();} MessageBox. Show ("conversion successful! ");} Catch (exception e) {MessageBox. Show (" Conversion failed! ");} Finally {killprogram (); // singleexcel. close (objmis, objmis, objmis); // app. quit ();}} /// <summary> /// Save the Excel cells you have used as JPG /// </Summary> /// <Param name = "excelfilepath"> source Excel path </param> /// <Param name = "saveexceljpg"> path of the target image (excluding the image name) </param> // <returns> </returns> Public static void exceltojpg (string excelfilepath, string saveexceljpg) {killprogram (); excel. application APP = new Micros Oft. office. interOP. excel. application (); object objmis = type. missing; excel. 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. workshe ETS [I]; object ranobj = dbnull. value; // You can select a cell and copy it out. // Wsheet. get_range ("A1", "aa22"). Copy (ranobj); wsheet. usedrange. Copy (ranobj); // select all cells and copy them. // 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 (saveexceljpg, system. drawing. imaging. imageformat. JPEG); clipboard. clear (); mybitmap. dispose (); Bits. Dispose ();} MessageBox. Show ("conversion successful! ");} Catch (exception Excel) {MessageBox. Show (" Conversion failed! ") ;}Finally {killprogram (); // singleexcel. Close (objmis, objmis, objmis); // app. Quit ();}}}}