import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class PoiRead {
// public static void main(String[] args) throws Exception{
public static List<List<String>> getResult() throws Exception {
File filepath = new File("F:\\电子对抗.xlsx");
InputStream is = new FileInputStream(filepath);
//利用POI 把Excel表遍历生成List类型
Workbook wb = new XSSFWorkbook(is);//对应一个Excel文件
List<List<String>> sheetlist = new ArrayList<List<String>>();//对应一页sheet
//对应第几张sheet,从0开始
Sheet sheet = wb.getSheetAt(7);
//获得行数
int rowSize = sheet.getLastRowNum() + 1;
for (int j = 0; 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();//内容转化字符串
}
rowList.add(value);
}
sheetlist.add(rowList);
}
return sheetlist;
}
//把原来长度为2的list拆分成成都为5的list,其中把第二个单元格的内容拆分成:title,author,cbs,ny.其中author.cbs,ny可能没有,title肯定有
public static List<List<String>> newsheetlist(List<List<String>> sheetlist) {
List<List<String>> newsheetlist = new ArrayList<List<String>>
POI读取Excel存入数据库
最新推荐文章于 2024-07-05 14:30:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)