Spring boot POI操作Excel入门案例( 二 )


执行代码:
public static void main(String[] args) throws Exception {poiUtils poiUtils = new poiUtils();//写出文件poiUtils.poiWrite();//读取文件//List read = poiUtils.getRead();//System.out.println("读取桌面文件的内容为");//System.out.println(read);}
执行结果:
在桌面生成 .xls 文件
打开生成的 .xls文件
3、Demo2:导入EXCEL表格

Spring boot  POI操作Excel入门案例

文章插图
从本地导入EXCEL表格中的内容,并将结果封装到List集合中,为了方便阅读,这里把异常都抛出,实际情况下,应该对异常进行捕获,并进行处理;
import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.ss.usermodel.Cell;import javax.swing.filechooser.FileSystemView;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class poiUtils {public List getRead() throws Exception {//1、获取文件的路径//1.1、从桌面获取文件FileSystemView fsv = FileSystemView.getFileSystemView();String desktop = fsv.getHomeDirectory().getPath();String filePath = desktop + "/testexcel.xls";//1.2、从绝对路径获取文件//String filePath = "D:\\testexcel.xls";//2、通过流获取本地文件FileInputStream fileInputStream = new FileInputStream(filePath);BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);POIFSFileSystem fileSystem = new POIFSFileSystem(bufferedInputStream);//3、创建工作簿对象,并获取工作表1HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);HSSFSheet sheet = workbook.getSheet("Sheet1");//4、从工作表中获取行数,并遍历int lastRowIndex = sheet.getLastRowNum();System.out.println("总行数为:" + lastRowIndex);ArrayList list = new ArrayList<>();for (int i = 1; i <= lastRowIndex; i++) {//4.1 获取每行的数据HSSFRow row = sheet.getRow(i);if (row == null) {break;}//5、从每一列中获取参数HashMap map = new HashMap<>();short lastCellNum = row.getLastCellNum();System.out.println("总列数为:" + lastRowIndex);for (int j = 0; j < lastCellNum; j++) {//设置返回值的类型row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);//获取每列的数据String cellValue = http://www.kingceram.com/post/row.getCell(j).getStringCellValue();System.out.println(cellValue);switch (j) {case 0:map.put("english", cellValue);break;case 1:map.put("chinese", cellValue);break;case 2:map.put("codechinese", cellValue);break;default:break;}}//将最后的结果封装到List集合中list.add(map);}//6、关闭资源、输出封装数据bufferedInputStream.close();System.out.println(list.toString());return list;}}
执行代码:
读取桌面生成的 .xls文件
public static void main(String[] args) throws Exception {poiUtils poiUtils = new poiUtils();//写出文件//poiUtils.poiWrite();//读取文件List read = poiUtils.getRead();System.out.println("读取桌面文件的内容为");System.out.println(read);}
执行结果:
4、Demo3:通过web接口传递EXCEL表格
这个是集成在 boot里面的,直接放在web就可以直接运行调用了 。
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.ss.usermodel.Cell;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import java.io.BufferedInputStream;import java.io.InputStream;@RestController@RequestMapping("/excel")public class TestController {private static final Logger logger = LogManager.getLogger(TestController.class);/*** 接受POSTman发送的EXCEL数据** @param* @return*/@RequestMapping(value = "http://www.kingceram.com/upload", method = {RequestMethod.POST})public void selectOne(@RequestParam("file") MultipartFile file) throws Exception {//1、通过流操作,将传入的数据转为EXCEL对象InputStream inputStream = file.getInputStream();BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);POIFSFileSystem fileSystem = new POIFSFileSystem(bufferedInputStream);//2、获取工作表0HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);//HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());HSSFSheet sheet = workbook.getSheetAt(0);//3、获取工作表0的行数,并遍历每一行int lastRowNum = sheet.getLastRowNum();for (int i = 1; i