自用简单excel 读取类

/**
*
*/
package c123;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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.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.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* @author Administrator
*
*/
public class ReadExcelToMaps {

/**
* @MethodName: menglei
* @Time:2012-3-1 下午4:06:45
* @Description:文件路径 得到 excel Mpa数组
* @param:文件File 所需字段
* @return:
* @throws:
*/
public Map<String, String>[] getMapsFExcel(String filePath,String[]fileds)throws IOException{
Boolean isFile=filePath.endsWith(".xlsx");
File file = new File(filePath);
Map<String, String>[] maps=null;
if(isFile){
maps=getMapsFExcel_XLSX_2007(file, fileds); // 获取工作薄个数
}else {
maps=getMapsFExcel_XLSX_2003(file, fileds);
}
return maps;
}



/**
* @MethodName: menglei
* @Time:2012-3-1 下午4:06:45
* @Description:由excel2003文件 得到 excel Mpa数组
* @param:文件File 所需字段
* @return:
* @throws:
*/
public Map<String, String>[] getMapsFExcel_XLSX_2003 (File file,String[]fileds) throws IOException{
FileInputStream fileInputStream= new FileInputStream(file);
return getMapsFExcel_XLS_2003(fileInputStream, fileds);
}


/**
* @MethodName: menglei
* @Time:2012-3-1 下午4:06:45
* @Description:由excel2007文件 得到 excel Mpa数组
* @param:文件File 所需字段
* @return:
* @throws:
*/
public Map<String, String>[] getMapsFExcel_XLSX_2007 (File file,String[]fileds) throws IOException{
FileInputStream fileInputStream= new FileInputStream(file);
return getMapsFExcel_XLSX_2007(fileInputStream, fileds);
}


/**
* @MethodName: menglei
* @Time:2012-3-1 下午4:06:45
* @Description:由excel2007文件输入流 得到数据
* @param:文件如入流FileInputStream 所需字段
* @return:
* @throws:
*/
public Map<String, String>[] getMapsFExcel_XLSX_2007 (FileInputStream fileInputStream,String[]fileds ) throws IOException{
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Sheet sheet =null;
Workbook wb=null;
wb=new XSSFWorkbook(fileInputStream); //2007
sheet = (XSSFSheet) wb.getSheetAt(0);// 获取工作薄个数
int rowNum = sheet.getLastRowNum();// 获取工作薄行数
Row row =sheet.getRow(1);
//检测异常
try {
new Check(fileds, row);
} catch (CheckExceFiled e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
for (int i = 1; i <= rowNum; i++) {
Map<String, String> inPutlistMap= new LinkedHashMap<String, String>();
row = sheet.getRow(i);
int j = 0;
for (int k = 0; k < fileds.length; k++) {
Cell cell=row.getCell(j);
String cellValue="";
System.out.println(cell.getCellType());
if(cell.getCellType()==0){
cellValue=String.valueOf(cell.getNumericCellValue());
}else if(cell.getCellType()==1) {
cellValue= cell.getStringCellValue();
}else if(cell.getCellType()==3) {
cellValue="";
}
inPutlistMap.put(fileds[k].toString(), cellValue);
j++;
}
list.add(inPutlistMap);
}
fileInputStream.close();
Map<String,String>[] arr=list.toArray(new HashMap[list.size()]);
return arr;
}
/**
* @MethodName: menglei
* @Time:2012-3-1 下午4:06:45
* @Description:由excel2003文件输入流 得到数据
* @param:文件如入流FileInputStream 所需字段
* @return:
* @throws:
*/
public Map<String, String>[] getMapsFExcel_XLS_2003(FileInputStream fileInputStream,String[]fileds ) throws IOException{
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
POIFSFileSystem fs = new POIFSFileSystem(fileInputStream);
Sheet sheet =null;
Workbook wb=null;
wb= new HSSFWorkbook(fs);
sheet=(HSSFSheet)wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();// 获取工作薄行数
Row row =sheet.getRow(1);
//检测异常
try {
new Check(fileds, row);
} catch (CheckExceFiled e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
for (int i = 1; i <= rowNum; i++) {
Map<String, String> inPutlistMap= new LinkedHashMap<String, String>();
row = sheet.getRow(i);
int j = 0;
for (int k = 0; k < fileds.length; k++) {
row.getLastCellNum();
Cell cell=row.getCell(j);
String cellValue="";
if(cell==null){

}else if(cell.getCellType()==0){
cellValue=String.valueOf(cell.getNumericCellValue());
}else if(cell.getCellType()==1) {
cellValue= cell.getStringCellValue();
}else if(cell.getCellType()==3) {
cellValue="";
}
inPutlistMap.put(fileds[k].toString(), cellValue);
j++;
}
list.add(inPutlistMap);
}
fileInputStream.close();
Map<String,String>[] arr=list.toArray(new HashMap[list.size()]);
return arr;
}
//自定义异常
class CheckExceFiled extends Exception{
public CheckExceFiled(String msg){
super(msg);
}
}
class Check {
public Check(String[]fields,Row row) throws CheckExceFiled {
// TODO Auto-generated constructor stub
for (String filed : fields) {
if(filed.trim().equals("")){
throw new CheckExceFiled("有为空的字段");//抛出异常
}
}
if (fields.length<=0) {
throw new CheckExceFiled("必须输入有效字段");//抛出异常
}
if(fields.length>row.getLastCellNum()){
throw new CheckExceFiled("输入的字段多于Excel表中每行数据");
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Excel中制作箱线图的方法如下: 1. 首先,准备好你要制作箱线图的数据。确保你的数据包含了需要绘制的变量和对应的数值。 2. 在Excel中,选择你的数据范围,包括变量和数值。 3. 在Excel的菜单栏中,选择“插入”选项卡,然后在“图表”组中选择“统计图表”。 4. 在弹出的图表型中,选择“箱线图”。 5. Excel会自动根据你的数据创建一个箱线图。你可以根据需要对图表进行调整和格式化,例如添加标题、调整轴标签等。 6. 如果你想要进一步定制你的箱线图,可以右键单击图表中的任意部分,选择“选择数据”或“格式化数据系列”来进行更改。 请注意,这只是一种在Excel中制作箱线图的方法,具体的步骤可能会因Excel版本的不同而有所差异。 #### 引用[.reference_title] - *1* *2* [数模笔记-Excel绘图-自用](https://blog.csdn.net/weixin_47066458/article/details/119993260)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [plotly系列 | 绘制散点图组合箱线图(联合图)](https://blog.csdn.net/qq_36396757/article/details/124393312)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值