Operation Excel using the Apache POI

Recently as interface, there is a function of the desired information to the online system part using Excel import automobile engine. A brief review of previously learned with java operations Excel.

1, maven configure Apache POI

pom.xml configuration packet coordinates POIjar

 1 <!-- 配置Apache POI -->
 2         <dependency>
 3             <groupId>org.apache.poi</groupId>
 4             <artifactId>poi</artifactId>
 5             <version>4.1.0</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.apache.poi</groupId>
 9             <artifactId>poi-ooxml</artifactId>
10             <version>4.1.0</version>
11         </dependency>
12         <dependency>
13             <groupId>org.apache.poi</groupId>
14             <artifactId>poi-ooxml-schemas</artifactId>
15             <version>4.1.0</version>
16         </dependency>

2, the test

 1 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 2 import org.apache.poi.ss.usermodel.*;
 3 import org.junit.Test;
 4 
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.io.OutputStream;
 8 
 9 public class POITest {
10 
11     @Test
12     public void run() throws IOException {
13         // 1、创建一个工作簿
14         Workbook wb = newHSSFWorkbook ();
 15          // 2, creates a Sheet 
16          Sheet Sheet = wb.createSheet ();
 . 17          // . 3, lines create an object 
18 is          Row Row = sheet.createRow (2 );
 . 19          // . 4, creating cell 
20          cell = row.createCell the cell (. 3 );
 21 is          // . 5, set the contents of the cell 
22 is          cell.setCellValue ( "Excel test operation the Apache a POI" );
 23          // cell style 
24          the CellStyle CellStyle = wb.createCellStyle ();
 25          // font 
26          the Font font = wb.createFont ();
 27         font.setFontName ( "Chinese official script" );
 28          font.setFontHeightInPoints (( Short ) 20 is );
 29          cellStyle.setFont (font);
 30          // . 6, sets the font style 
31 is          cell.setCellStyle (CellStyle);
 32          // . 7, save, close the stream 
33 is          the OutputStream OS = new new a FileOutputStream ( "E: \\ POITest.xls" );
 34 is          wb.write (OS);
 35          os.close ();
 36      }
 37 [ }

3. Results

 

This operation is relatively simple, the work needs to be done: first to verify whether the Excel file, followed verify the contents of Excel, then Excel reads the contents of the file (the title and the first line of each line) to upload, and finally read taken insert relevant content database table.

Guess you like

Origin www.cnblogs.com/alphajuns/p/11373251.html