Method of generating excel file and inserting picture using Java in Windows

Generating simple Excel files in real office, we often have such a requirement: to open the report directly in Excel. There is such a need in the internship. Query all the information of the attachments provided by the user according to the selected resources and generate Excel for download. However, when querying, we need to check whether the information in the attachment provided by the user is wrong (ID card). If there are errors, generate error information excel. The POI project of Apache is a relatively mature HSSF interface for processing excel objects. In fact, POI can handle not only excel, but also word, PowerPoint, Visio and even outlook. Here I will first introduce how to generate excel using poi. First, before generating excel, we need to understand the organization of Excel files. In POI, it is understood that an excel file corresponds to a workbook, and a workbook is composed of several sheets. A sheet has multiple rows, and a row generally has multiple cells. For the above four nouns, we can understand them in the figure below

For generating excel, POI provides the following basic objects:

From the above picture and the organization structure of Excel, we can understand the steps of creating excel.

1. Generate the document object hsshworkbook. 2. Generate the form hssfsheet through the hssfworkbook. 3. Generate row hssfrow 4 through hssfsheet and cell hssfcell through hssfrow. The following is the display code: ID card error bean (errorcondition. Java)

Processing class (exporterrorcexcel. Java)

Through the above six steps, you can generate Excel files at the specified location.

Java POI implements inserting pictures into Excel for web development. It is inevitable to deal with Excel. Today, the boss gave me a task - export excel. At first, it was quite simple. It was just to find, build excel and download the response. But there is one difference, that is to add pictures, which has been adding pictures for a long time. At the same time, no better information is found on the network, so write this blog record for yourself and bloggers to query and refer to. In POI, there is an hssfpatriarch object, which is the top-level manager of drawing. Its createpicture (anchor, pictureindex) method can insert a picture in Excel. So to insert a picture in Excel, you can do it in three steps. 1、 Get the hssfpatriarch object, 2. New hssfclientanchor object, 3. Call the createpicture method. It's very easy to implement, and it's still a little difficult if you want to do it well. Here we insert a picture:

The results after implementation are as follows:

As for why this result is caused mainly by the constructor hssfclientanchor (0,8), let me explain this constructor: hssfclientanchor (int dx1, int DY1, int DX2, int dy2, short col1, int row1, short col2, int row2); The meanings of each parameter are as follows:

Here, dx1 and DY1 define the starting position of the picture in the start cell, and DX2 and dy2 define the ending position in the end cell. Col1 and row1 define the start cell, and col2 and row2 define the end cell.

The following is created by two different constructors. From this figure, we can clearly see the meaning and differences of the above eight parameters.

The above is to insert a picture. What about inserting multiple pictures? In fact, it is very simple to construct multiple different hssfclientanchor objects and control the eight parameters as follows:

Like other codes, the following results are obtained:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>