One. Imported into excel
NOTE: POI herein is ssm frame base prior to the introduction of the
1. dependence introduction
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency>
2. Write POIUtil class
com.exp.utils Package;
Import com.exp.model.User;
Import the org.apache.poi.hssf.usermodel *;.
Import org.apache.poi.ss.util.CellRangeAddress;
Import java.io.FileOutputStream;
Import a java.io.OutputStream;
Import java.util.List;
public class POIUtil {
public static void Export (List <the User> userList) throws Exception {
position designating data storage //
the OutputStream the outputStream a FileOutputStream new new = ( "D: \\ Test .xls ");
// create a workbook 1.
HSSFWorkbook workbook = new new HSSFWorkbook ();
. 2 // create a worksheet sheet
HSSFSheet sheet = workbook.createSheet (" the Test ");
// List <the User> = userList userService.selectAll ();
// constructor parameter in turn represents the starting line up to the line, start column, as of column
Region = new new CellRangeAddress CellRangeAddress (0, 0, 0,. 3);
sheet.addMergedRegion (Region);
HSSFCellStyle style = workbook.createCellStyle ();
// horizontally centered
style.setAlignment (HSSFCellStyle.ALIGN_CENTER);
// vertically centered
style. setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);
HSSFRow ROW1 = sheet.createRow (0);
HSSFCell cell row1.createCell = (0);
// set value, the cell corresponding to the title incorporated herein
cell.setCellValue ( "personnel information table") ;
// Add style commencement
cell.setCellStyle (style);
for (int I = 0; I <userList.size (); I ++) {
// row
HSSFRow sheet.createRow row = (I +. 1);
// for assignment column
row.createCell(0).setCellValue(userList.get(i).getId());
row.createCell(1).setCellValue(userList.get(i).getName());
row.createCell(2).setCellValue(userList.get(i).getPassword());
row.createCell(3).setCellValue(userList.get(i).getRemark());
}
workbook.write(outputStream);
outputStream.close();
}
}
In the TestController
3. Test
Visit: http: // localhost: 8080 / ssm2 / test
In the disc D will generate a file open as follows test.xls
two. Exported from the database to excel
1. Preparation POIUtil Tools
public static List<User> importExcel() throws Exception{ HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File("D:\\t1.xls"))); HSSFSheet sheet = null; for(int i = 0;i < workbook.getNumberOfSheets();i++){ //获取每个sheet sheet = workbook.getSheetAt(i); List<User> list = new ArrayList<User>(); //getPhysicalNumberOfRows获取有记录的行数 for(int j = 0;j < sheet.getPhysicalNumberOfRows();j++){ Row row = sheet.getRow(j); if(null!=row){ //getLastCellNum获取最后一列 User user = new User(); for(int k = 0;k < row.getLastCellNum();k++){ if(null!=row.getCell(k)){ if(k==0){ Cell cell = row.getCell(0); //cell->double Double d = cell.getNumericCellValue(); //double->int int id = new Double(d).intValue(); user.setId(id); } if(k==1){ Cell cell = row.getCell(1); //cell->string user.setName(cell.getStringCellValue().toString()); } if(k==2){ Cell cell = row.getCell(2); user.setPassword(cell.getStringCellValue().toString()); } if(k==3){ Cell cell = row.getCell(3); user.setRemark(cell.getStringCellValue().toString()); } } } list.add(user); } } System.out.println("读取sheet表:"+ workbook.getSheetName(i) + "完成"); return list; } return null; }
At the time of writing tools of the problems encountered Summary:
(1)getLastRowNum
If the sheet does not have a row of data is -1, only the first line of data is returned to 0, is the last data row n-1 n-th row is returned;
getPhysicalNumberOfRows
Get the number of recorded lines, namely: the last data line has n-th row, m-th row in front of the line is not blank data, then returns nm;
Prior written getLastRowNum () to perform row.getLastCellNum () = - 1, to obtain the value of less than excel
(2) How to assign a value to an object in the loop, if I wrote a judgment
(3)cell->int和string
2.UserMapper.xml
<insert id="insertData" parameterType="list"> insert into user(id,name,password,remark) VALUES <foreach collection="list" index="index" item="user" separator=","> (#{user.id},#{user.name},#{user.password},#{user.remark}) </foreach> </insert>
3.UserMapper and interfaces as UserService
int insertData(List<User> users);
4.UserServiceImpl
@Override public int insertData(List<User> users) { int num = userMapper.insertData(users); return num; }
5.TestController
@RequestMapping("/insertData") public void insertData() throws Exception{ List<User> users = POIUtil.importExcel(); int num = userService.insertData(users); if(num > 0){ System.out.println("插入数据成功"); } }
excel data D: /t1.xls, the following data
6. Test
访问:http://localhost:8080/ssm2/insertData
That view in the database as success:
Note: This article simply wrote under POI import and export functions, of course, there are many uses yet to be learning the POI.
Reproduced in: https: //www.cnblogs.com/xinyijiu/p/10969690.html