poi解析excel简单小例子
程序员文章站
2022-04-08 17:09:41
...
package com.test;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ParseExcel {
public static void main(String[] args) {
Map<String, List<List<String>>> sheetsMap = getExcelData("D:/aaa.xlsx");
System.out.println("sheet total:"+sheetsMap.size());
Set<String> keys = sheetsMap.keySet();
//查看解析记录
for(String key : keys){
List<List<String>> sheets = sheetsMap.get(key);
System.out.println("====================="+key+"=======================");
for (int j = 0; j < sheets.size(); j++) {
List<String> cellList = sheets.get(j);
for (int k = 0; k < cellList.size(); k++) {
System.out.print(cellList.get(k)+" ");
}
System.out.println();
}
System.out.println();
}
}
/**
* 根据指定sheet名获取数据
* @param sheetName sheet名称
* @param path 指定excel文件
* @return 返回指定sheetName的sheet数据
*/
public static List<List<String>> getSheetByName(String sheetName,String path){
// 获取所有数据
Map<String, List<List<String>>> sheetsMap = getExcelData(path);
// 获取指定的数据
List<List<String>> list = sheetsMap.get(sheetsMap);
return list;
}
/**
* 获取excel数据
* @param path excel文件的路径
* @return 返回整个excel的数据,可根据sheet名在map中获取每个sheet的数据,
* 可根据map中的list获取每行的列表数据
*/
public static Map<String,List<List<String>>> getExcelData(String path) {
// 总数据
Map<String,List<List<String>>> sheetsMap = new HashMap<String,List<List<String>>>();
// 页数据
List<List<String>> rowsList = null;
// 行数据
List<String> cellList = null;
XSSFWorkbook workbook = null;
File file = new File(path);
try {
workbook = new XSSFWorkbook(file);
int sheetTotal = workbook.getNumberOfSheets();
// 1.循环遍历每一个sheet
for (int i = 0; i < sheetTotal; i++) {
XSSFSheet sheet = workbook.getSheetAt(i);
int rowTotal = sheet.getLastRowNum();
rowsList = new ArrayList<List<String>>();
// 2.循环遍历每一行row
for (int j = 0; j < rowTotal; j++) {
XSSFRow row = sheet.getRow(j);
if (row != null) {
int cellTotal = row.getLastCellNum();
cellList = new ArrayList<String>();
// 3.循环遍历每一列cell
for (int k = 0; k < cellTotal; k++) {
if (row.getCell(k) != null) {
// 4.判断日期,也可以用HSSFDataFormat.getBuiltinFormat("m/d/yy")替换14
if(row.getCell(k).getCellStyle().getDataFormat()==14){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = row.getCell(k).getDateCellValue();
if(date!=null){
cellList.add(format.format(date).toString());
}
}else{
cellList.add(row.getCell(k).toString());
}
}
}
rowsList.add(cellList);
}
}
// 可很据sheet名获得数据
sheetsMap.put(sheet.getSheetName(), rowsList);
}
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭工作簿
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sheetsMap;
}
}
日期格式源码
static
{
List m = new ArrayList();
putFormat(m, 0, "General");
putFormat(m, 1, "0");
putFormat(m, 2, "0.00");
putFormat(m, 3, "#,##0");
putFormat(m, 4, "#,##0.00");
putFormat(m, 5, "\"$\"#,##0_);(\"$\"#,##0)");
putFormat(m, 6, "\"$\"#,##0_);[Red](\"$\"#,##0)");
putFormat(m, 7, "\"$\"#,##0.00_);(\"$\"#,##0.00)");
putFormat(m, 8, "\"$\"#,##0.00_);[Red](\"$\"#,##0.00)");
putFormat(m, 9, "0%");
putFormat(m, 10, "0.00%");
putFormat(m, 11, "0.00E+00");
putFormat(m, 12, "# ?/?");
putFormat(m, 13, "# ??/??");
putFormat(m, 14, "m/d/yy");
putFormat(m, 15, "d-mmm-yy");
putFormat(m, 16, "d-mmm");
putFormat(m, 17, "mmm-yy");
putFormat(m, 18, "h:mm AM/PM");
putFormat(m, 19, "h:mm:ss AM/PM");
putFormat(m, 20, "h:mm");
putFormat(m, 21, "h:mm:ss");
putFormat(m, 22, "m/d/yy h:mm");
for (int i = 23; i <= 36; i++)
{
putFormat(m, i, "reserved-0x" + Integer.toHexString(i));
}
putFormat(m, 37, "#,##0_);(#,##0)");
putFormat(m, 38, "#,##0_);[Red](#,##0)");
putFormat(m, 39, "#,##0.00_);(#,##0.00)");
putFormat(m, 40, "#,##0.00_);[Red](#,##0.00)");
putFormat(m, 41, "_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)");
putFormat(m, 42, "_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)");
putFormat(m, 43, "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)");
putFormat(m, 44, "_(\"$\"* #,##0.00_);_(\"$\"* (#,##0.00);_(\"$\"* \"-\"??_);_(@_)");
putFormat(m, 45, "mm:ss");
putFormat(m, 46, "[h]:mm:ss");
putFormat(m, 47, "mm:ss.0");
putFormat(m, 48, "##0.0E+0");
putFormat(m, 49, "@");
String[] ss = new String[m.size()];
m.toArray(ss);
_formats = ss;
}
上一篇: BAT读取文件
下一篇: getline函数读文件