poi读取Excel总结
来源:互联网 发布:淘宝整点秒杀怎么设置 编辑:程序博客网 时间:2024/04/30 20:28
package eiis.controller.app.sbz;import eiis.app.sbz.entity.AppWatStationEntity;import eiis.app.sbz.service.IAppWatStationService;import eiis.app.sbz.util.ReadAndWriteExcelUtil;import jxl.read.biff.BiffException;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.*;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;import java.io.*;import java.math.BigDecimal;import java.time.Instant;import java.util.*;/** * Created by Administrator on 2017/3/8. */@RequestMapping("/sbz/")@Controller("/controller/app/sbz/AppWatStationController")public class AppWatStationController { @Autowired private IAppWatStationService iAppWatStationService; public List<AppWatStationEntity> appWatStationEntity = new ArrayList(); @ResponseBody @RequestMapping("appWatStation_readExcel") public String readFile(AppWatStationEntity appWatStationEntity, String data) throws IOException, BiffException { return "SUCCESS"; } /* @RequestMapping(value = "readExcel", method = RequestMethod.POST, params = {"filename", "suffix"}) @ResponseBody*/ public String readexcel(HttpServletRequest request) throws Exception { Workbook wb = null; String filename = request.getParameter("filename"); String suffix = request.getParameter("suffix"); String filepath = "D://"+filename; InputStream is = new FileInputStream(filepath); if(".xls".equals(suffix)){ wb = new HSSFWorkbook(is); System.out.println("文件格式为.xls"); }else if(".xlsx".equals(suffix)){ wb = new XSSFWorkbook(is); System.out.println("文件格式为.xlsx"); }else { System.out.println("文件格式错误!"); } List<List<Map<String, String>>> result = new ArrayList<List<Map<String,String>>>();//对应excel文件 int sheetSize = wb.getNumberOfSheets(); System.out.println(sheetSize); for (int i = 0; i < sheetSize; i++) {//遍历sheet页 Sheet sheet = wb.getSheetAt(i); List<Map<String, String>> sheetList = new ArrayList<Map<String, String>>();//对应sheet页 List<String> titles = new ArrayList<String>();//放置所有的标题 int rowSize = sheet.getLastRowNum() + 1; for (int j = 1; j < rowSize; j++) {//遍历行 Row row = sheet.getRow(j); if (row == null) {//略过空行 continue; } int cellSize = row.getLastCellNum();//行中有多少个单元格,也就是有多少列 if (j == 0 || j==1) {//第一行是标题行 for (int k = 0; k < cellSize; k++) { Cell cell = row.getCell(k); titles.add(cell.toString()); } } else {//其他行是数据行 Map<String, String> rowMap = new HashMap<String, String>();//对应一个数据行 for (int k = 0; k < titles.size(); k++) { Cell cell = row.getCell(k); String key = titles.get(k); String value = null; if (cell != null) { value = cell.toString(); } rowMap.put(key, value); } sheetList.add(rowMap); } } System.out.println(sheetList); result.add(sheetList); } return null; } @RequestMapping(value = "readExcel", method = RequestMethod.POST, params = {"filename", "suffix"}) @ResponseBody public String readExcelWithoutTitle(HttpServletRequest request) throws Exception{ Workbook wb = null; String filename = request.getParameter("filename"); String suffix = request.getParameter("suffix"); String filepath = "D://"+filename; InputStream is = new FileInputStream(filepath); if(".xls".equals(suffix)){ wb = new HSSFWorkbook(is); System.out.println("文件格式为.xls"); }else if(".xlsx".equals(suffix)){ wb = new XSSFWorkbook(is); System.out.println("文件格式为.xlsx"); }else { System.out.println("文件格式错误!"); } List<List<List<String>>> result = new ArrayList<List<List<String>>>();//对应excel文件 int sheetSize = wb.getNumberOfSheets(); for (int i = 0; i < sheetSize; i++) {//遍历sheet页 Sheet sheet = wb.getSheetAt(i); List<List<String>> sheetList = new ArrayList<List<String>>();//对应sheet页 int rowSize = sheet.getLastRowNum() + 1; for (int j = 3; j < rowSize; j++) {//遍历行 Row row = sheet.getRow(j); if (row == null) {//略过空行 continue; } int cellSize = row.getLastCellNum();//行中有多少个单元格,也就是有多少列 List<String> rowList = new ArrayList<String>();//对应一个数据行 for (int k = 0; k < cellSize; k++) { Cell cell = row.getCell(k); String value = null; if (cell != null) { value = cell.toString(); if (k==2 && value!=null){ String string = row.getCell(2).getCellComment().toString(); } } rowList.add(value); } sheetList.add(rowList); } System.out.println(sheetList); result.add(sheetList); } return null; }}
0 0
- poi读取Excel总结
- POI-----POI读取Excel实例
- POI读取EXCEL
- POI读取EXCEL教程
- POI读取EXCEL
- poi 读取excel
- 通过POI读取Excel
- POI读取Excel
- POI 读取 Excel
- POI读取Excel浅谈
- POI 读取EXcel
- POI读取EXCEL教程
- POI读取Excel
- POI读取Excel
- POI读取Excel浅谈
- POI读取EXCEL教程
- Java POI 读取Excel
- java poi读取excel
- javac 与类名冲突
- 数据结构(一) 抽象数据类型的表示与实现 代码示例
- 邓仰东专栏|机器学习的那些事儿(二):机器学习简史
- sqoop使用手册
- 如何避免RecycleView复杂多条目布局时的加载不流畅
- poi读取Excel总结
- java 二分查找算法
- int const *p与int *const p区别
- 使用渲染纹理的制作摄像头
- java设计模式—分类
- JavaSE程序分析004 JVM常量池的小事情
- 并查集的使用及其实现
- 微信支付集成简介
- linux怎样使用top命令查看系统状态