java读取excel指定的位置
来源:互联网 发布:ubuntu 字体文件夹 编辑:程序博客网 时间:2024/05/16 00:49
package HolidaySummaryReport;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
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;
public class AircraftReport_ReadExcel {
// 该方法判断excel版本
@SuppressWarnings("resource")
public static Workbook openWorkbook(String fileName)
throws IOException {
InputStream in = new FileInputStream(fileName); // 创建输入流
Workbook wb = null;
if (fileName.toLowerCase().endsWith(".xlsx")) {//toLowerCase 区分大小写
wb = new XSSFWorkbook(in);// Excel 2007
} else {
wb = (Workbook) new HSSFWorkbook(in);// Excel 2003
}
return wb;
}
// 该方法处理excel的数据
public static Map<String, Double> readExcel(String fileName, int sheetIndex, List<Map<String, Object>> readValues) throws Exception {
Map<String, Double> result = new HashMap<String, Double>();
Workbook wb = openWorkbook(fileName);// 获取Excel文件对象
Sheet sheet = wb.getSheetAt(sheetIndex);// 获取文件的指定工作表m
FormulaEvaluator evaluator = wb.getCreationHelper()
.createFormulaEvaluator();//考虑到有些表格位置值为计算的结果,这里为获取计算结果值,避免把公式读出来了
for (Map<String, Object> info : readValues){//通过readValues遍历key和value
Row row = sheet.getRow((int)info.get("y"));// 获取行
Cell cell = row.getCell((int)info.get("x"));// 获取列
CellValue cellValue = evaluator.evaluate(cell);// 获表格公式计算后的值
if(cellValue != null){
switch (cellValue.getCellType()) {// 判断值类型
case Cell.CELL_TYPE_NUMERIC:
result.put(info.get("user").toString(), cellValue.getNumberValue());
System.out.println("键为: " + info.get("user") + "读取到的值是: " + result.get(info.get("user").toString()));
break;
default:
result.put(info.get("user").toString(), 0.0d);
break;
}
}else{
System.out.println("单元格的值为空!");
}
}
return result;
}
public static void main(String[] args) throws Exception {
String fileName = "D://User//user.xlsx";//D://文件夹名//文件名.xlsx"
List<Map<String, Object>> readValues = new ArrayList<Map<String, Object>>();
Map<String, Object> readValue = new HashMap<String, Object>();
readValue.put("user","姓名");//user 是取名表头类型(user) 尽量取一个代表性名字,后面为属性比如:姓名,年龄
readValue.put("x", 1);//横坐标
readValue.put("y", 1);//纵坐标
readValues.add(readValue);
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
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;
public class AircraftReport_ReadExcel {
// 该方法判断excel版本
@SuppressWarnings("resource")
public static Workbook openWorkbook(String fileName)
throws IOException {
InputStream in = new FileInputStream(fileName); // 创建输入流
Workbook wb = null;
if (fileName.toLowerCase().endsWith(".xlsx")) {//toLowerCase 区分大小写
wb = new XSSFWorkbook(in);// Excel 2007
} else {
wb = (Workbook) new HSSFWorkbook(in);// Excel 2003
}
return wb;
}
// 该方法处理excel的数据
public static Map<String, Double> readExcel(String fileName, int sheetIndex, List<Map<String, Object>> readValues) throws Exception {
Map<String, Double> result = new HashMap<String, Double>();
Workbook wb = openWorkbook(fileName);// 获取Excel文件对象
Sheet sheet = wb.getSheetAt(sheetIndex);// 获取文件的指定工作表m
FormulaEvaluator evaluator = wb.getCreationHelper()
.createFormulaEvaluator();//考虑到有些表格位置值为计算的结果,这里为获取计算结果值,避免把公式读出来了
for (Map<String, Object> info : readValues){//通过readValues遍历key和value
Row row = sheet.getRow((int)info.get("y"));// 获取行
Cell cell = row.getCell((int)info.get("x"));// 获取列
CellValue cellValue = evaluator.evaluate(cell);// 获表格公式计算后的值
if(cellValue != null){
switch (cellValue.getCellType()) {// 判断值类型
case Cell.CELL_TYPE_NUMERIC:
result.put(info.get("user").toString(), cellValue.getNumberValue());
System.out.println("键为: " + info.get("user") + "读取到的值是: " + result.get(info.get("user").toString()));
break;
default:
result.put(info.get("user").toString(), 0.0d);
break;
}
}else{
System.out.println("单元格的值为空!");
}
}
return result;
}
public static void main(String[] args) throws Exception {
String fileName = "D://User//user.xlsx";//D://文件夹名//文件名.xlsx"
List<Map<String, Object>> readValues = new ArrayList<Map<String, Object>>();
Map<String, Object> readValue = new HashMap<String, Object>();
readValue.put("user","姓名");//user 是取名表头类型(user) 尽量取一个代表性名字,后面为属性比如:姓名,年龄
readValue.put("x", 1);//横坐标
readValue.put("y", 1);//纵坐标
readValues.add(readValue);
readExcel(fileName, 0 , readValues);//文件名,sheet下标,值
}
}阅读全文
1 0
- java读取excel指定的位置
- java读取excle指定位置的数据
- java读取Excel指定格式的数据
- 读取文件的指定位置
- Java读取指定文件下的文件名到Excel中
- 文件的指定位置读取内容
- excel的读取 java
- java创建Excel并下载至本地指定位置
- python读取指定字节及位置的文件内容
- java读取excel的方法
- 简单 的java 读取 excel
- Java Web的Excel读取
- java读取Excel读取
- 使用VBA读取指定Excel文件指定Sheet指定单元格的内容
- 读取指定目录下的所有文件生成一个excel
- java中配置文件的位置和读取
- java web中读取配置文件的位置
- java语言读取文本文件的指定行
- usaco Friday the Thirteenth
- Plugin with id 'com.android.application' not found. 解决
- 面试系列-冒泡排序
- jvm 系统调优,确定高CPU程序代码
- Android4.4修改状态栏颜色
- java读取excel指定的位置
- vsftpd不能显示文件目录的解决方法
- Jenkins远程部署 gitLab配置
- springboot
- BaseActivity封装
- SSH综合项目实战(快递) -- day11 shiro权限控制、后台用户登录
- TRS Database admin数据字段类型总结
- h5 input type=date 怎么进行赋值(回显)
- TypeScript入门知识三(面向对象特性)