JAVA exports reports in Excel to the image format (2) implementation idea, javaexcel
Next, an Excel report similar to the one designed below. How can I export a jpg image with the same style in the specified area?
There is no ready-made solution to implement this function, and Google duniang hasn't been there for a long time. Finally, I thought about several ideas:
Idea 1: Use the background and border in the report as the model image. The data that needs to be updated regularly is read from Oracle through JDBC and drawn to the model image.
Reason for rejection: it is not feasible. The coordinate points of all data must have rules to facilitate cyclic plotting. The amount of work is huge, the coupling is huge, and table data is moving across the body, which is not conducive to expansion.
Idea 2: No report prototype is required. After an Excel report is generated, use jxl or poi to read all cells in the report, including the data and format of cells, the border, width, height, font foreground color, and background color must be read, and then drawn in JAVA to generate a report in JPG format.
Reason for rejection: it is feasible, but the code volume is huge, which is time-consuming and laborious to read and draw. However, it has some advantages. You can write a general program in a busy time, all export problems are solved once and for all.
Idea 3: it is a strange trick for the blogger to think about it. Open Excel via WPS or Office and write a Robot to move the mouse to the specified area covered by two specified coordinates, robot simulates the press Ctrl + C, and then exports the data drawing on the clipboard to the JPG file.
Finally, I implemented Idea 3, which has some advantages and disadvantages. The advantages are simplicity and convenience, high operability, low code volume, and high scalability, for other reports, you only need to provide another set of coordinate parameters.
The disadvantage is that it completely sacrifices JAVA's cross-platform nature. Even if it runs on a server with lower resolution, it may cause the replication operation to fail.
Another drawback is that the thread becomes insecure. If the server load is heavy, you cannot predict the time for the Office to open the report correctly, or the copy operation may fail.
Or when multiple report generation tasks are executed simultaneously, it is clear that using Robot will cause a conflict.
The following provides the implementation code of Idea 3. I will use idea 2 to write a general program in the future.
Package com. newflypig. excel; import java. awt. graphics; import java. awt. image; import java. awt. robot; import java. awt. toolkit; import java. awt. datatransfer. dataFlavor; import java. awt. datatransfer. transferable; import java. awt. event. inputEvent; import java. awt. event. keyEvent; import java. awt. image. bufferedImage; import java. io. file; import java. text. simpleDateFormat; import java. util. date; import javax. imageio. I MageIO; public class OpenExcelDemo {public static void main (String [] args) throws Exception {openExcel ("d: \ New Period Monthly Report .xlsx"); copyRectByPix (37,207,121 5, 665 ); // specify the data of two coordinate points. The value range is createImageFileFromClip ("d:/" + getTimeStr () + ". jpg "); closeExcel (int) Toolkit. getdefatooltoolkit (). getScreenSize (). getWidth ()-15, 12); // You Need to simulate the Close event and close the Excel file so that the report can be correctly opened next time} private static void closeExcel (int I, int j) throw S Exception {Robot robot = new Robot (); robot. delay (500); robot. mouseMove (I, j); robot. delay (500); robot. mousePress (InputEvent. button#mask); robot. delay (500); robot. mouseRelease (InputEvent. button#mask); robot. delay (500); robot. keyPress (KeyEvent. VK_ENTER);} public static void openExcel (String dir) throws Exception {runtime.getruntime(.exe c ("cmd/k" + dir + "");} public static void copyRectBy Pix (int fromX, int fromY, int toX, int toY) throws Exception {Robot robot = new Robot (); robot. delay (3000); // The latency is 3000 milliseconds. mouseMove (fromX, fromY); robot. delay (500); robot. mousePress (InputEvent. button#mask); robot. delay (500); robot. mouseMove (toX, toY); robot. delay (500); robot. mouseRelease (InputEvent. button#mask); robot. setAutoDelay (200); robot. keyPress (KeyEvent. VK_CONTROL); robot. keyPress (Ke YEvent. VK_C); robot. keyRelease (KeyEvent. VK_CONTROL); robot. keyRelease (KeyEvent. VK_C);} public static void createImageFileFromClip (String dir) throws Exception {Transferable t = Toolkit. getdefatooltoolkit (). getSystemClipboard (). getContents (null); if (null! = T & t. isDataFlavorSupported (DataFlavor. imageFlavor) {Image image = (Image) t. getTransferData (DataFlavor. imageFlavor); savePic (image, dir) ;}} public static String savePic (Image iamge, String dir) throws Exception {int w = iamge. getWidth (null); int h = iamge. getHeight (null); // first create a BufferedImage variable, because the BufferedImage variable is used for image write in ImageIO. BufferedImage bi = new BufferedImage (w, h, BufferedImage. TYPE_3BYTE_BGR); // create another Graphics variable to draw the Image to be preserved and the Image variable Graphics g = bi. getGraphics (); g. drawImage (iamge, 0, 0, null); // write the BufferedImage variable to the file. ImageIO. write (bi, "jpg", new File (dir); return dir;} public static String getTimeStr () {String time = new SimpleDateFormat ("yyyyMMddHHmmss "). format (new Date (); return time ;}}