poi 读取写入excel
来源:互联网 发布:比特币交易平台 知乎 编辑:程序博客网 时间:2024/04/26 06:10
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtil {
//默认单元格内容为数字时格式
private static DecimalFormat df = new DecimalFormat("0");
// 默认单元格格式化日期字符串
private static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
// 格式化数字
private static DecimalFormat nf = new DecimalFormat("0.00");
public static ArrayList<ArrayList<Object>> readExcel(File file){
if(file == null){
return null;
}
if(file.getName().endsWith("xlsx")){
//处理ecxel2007
return readExcel2007(file);
}else{
//处理ecxel2003
return readExcel2003(file);
}
}
/*
* @return 将返回结果存储在ArrayList内,存储结构与二位数组类似
* lists.get(0).get(0)表示过去Excel中0行0列单元格
*/
public static ArrayList<ArrayList<Object>> readExcel2003(File file){
try{
ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>();
ArrayList<Object> colList;
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
Object value;
for(int i = sheet.getFirstRowNum() , rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows() ; i++ ){
row = sheet.getRow(i);
colList = new ArrayList<Object>();
if(row == null){
//当读取行为空时
if(i != sheet.getPhysicalNumberOfRows()){//判断是否是最后一行
rowList.add(colList);
}
continue;
}else{
rowCount++;
}
for( int j = row.getFirstCellNum() ; j <= row.getLastCellNum() ;j++){
cell = row.getCell(j);
if(cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){
//当该单元格为空
if(j != row.getLastCellNum()){//判断是否是该行中最后一个单元格
colList.add("");
}
continue;
}
switch(cell.getCellType()){
case XSSFCell.CELL_TYPE_STRING:
System.out.println(i + "行" + j + " 列 is String type");
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
System.out.println(i + "行" + j
+ " 列 is Number type ; DateFormt:"
+ value.toString());
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(i + "行" + j + " 列 is Boolean type");
value = Boolean.valueOf(cell.getBooleanCellValue());
break;
case XSSFCell.CELL_TYPE_BLANK:
System.out.println(i + "行" + j + " 列 is Blank type");
value = "";
break;
default:
System.out.println(i + "行" + j + " 列 is default type");
value = cell.toString();
}// end switch
colList.add(value);
}//end for j
rowList.add(colList);
}//end for i
return rowList;
}catch(Exception e){
return null;
}
}
public static ArrayList<ArrayList<Object>> readExcel2007(File file){
try{
ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>();
ArrayList<Object> colList;
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
Object value;
for(int i = sheet.getFirstRowNum() , rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows() ; i++ ){
row = sheet.getRow(i);
colList = new ArrayList<Object>();
if(row == null){
//当读取行为空时
if(i != sheet.getPhysicalNumberOfRows()){//判断是否是最后一行
rowList.add(colList);
}
continue;
}else{
rowCount++;
}
for( int j = row.getFirstCellNum() ; j <= row.getLastCellNum() ;j++){
cell = row.getCell(j);
if(cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK){
//当该单元格为空
if(j != row.getLastCellNum()){//判断是否是该行中最后一个单元格
colList.add("");
}
continue;
}
switch(cell.getCellType()){
case XSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
value = Boolean.valueOf(cell.getBooleanCellValue());
break;
case XSSFCell.CELL_TYPE_BLANK:
value = "";
break;
default:
value = cell.toString();
}// end switch
colList.add(value);
}//end for j
rowList.add(colList);
}//end for i
return rowList;
}catch(Exception e){
System.out.println("exception");
return null;
}
}
public static void writeExcel(ArrayList<ArrayList<Object>> result,String path){
if(result == null){
return;
}
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
for(int i = 0 ;i < result.size() ; i++){
HSSFRow row = sheet.createRow(i);
if(result.get(i) != null){
for(int j = 0; j < result.get(i).size() ; j ++){
HSSFCell cell = row.createCell(j);
cell.setCellValue(result.get(i).get(j).toString());
}
}
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try
{
wb.write(os);
} catch (IOException e){
e.printStackTrace();
}
byte[] content = os.toByteArray();
File file = new File(path);//Excel文件生成后存储的位置。
OutputStream fos = null;
try
{
fos = new FileOutputStream(file);
fos.write(content);
os.close();
fos.close();
}catch (Exception e){
e.printStackTrace();
}
}
public static DecimalFormat getDf() {
return df;
}
public static void setDf(DecimalFormat df) {
ExcelUtil.df = df;
}
public static SimpleDateFormat getSdf() {
return sdf;
}
public static void setSdf(SimpleDateFormat sdf) {
ExcelUtil.sdf = sdf;
}
public static DecimalFormat getNf() {
return nf;
}
public static void setNf(DecimalFormat nf) {
ExcelUtil.nf = nf;
}
public static void main(String[] args) {
File file = new File("读取文件名绝对路径.xlsx");
ArrayList<ArrayList<Object>> result = ExcelUtil.readExcel(file);
for(int i = 1 ;i < result.size() ;i++){
for(int j = 0;j<result.get(i).size(); j++){
System.out.println(result.get(i).toString());
}
}
ExcelUtil.writeExcel(result,"新建文件名绝对路径.xlsx");
}
}
阅读全文
0 0
- poi 读取写入excel
- POI读取和写入Excel
- POI操作Excel,读取,写入
- java8 POI 读取写入EXCEL
- POI excel的读取,写入
- poi读取数据写入excel
- 使用POI操作Excel,读取、写入Excel
- Apache POI 读取、写入Excel文件教程
- poi 中读取和写入excel 方法
- POI处理EXCEL文件(读取,写入)
- 利用POI将读取的数据写入Excel
- poi写入读取excel 2007版xlsx 97版xls
- poi写入读取excel 2007版xlsx 97版xls
- Java模块 -- 读取Excel文件写入数据库 Mybatis , POI , JXL
- java 通过poi 读取Excel 写入sqlser mysql
- Java通过poi写入和读取Excel文件
- 用POI写入Excel
- poi 写入excel
- 用js快速动态生成bootstrap table表格数据
- 蓝牙5:速度更快,范围更广,新的RF测试---电子技术资料
- .
- 2017ACM/ICPC亚洲区沈阳站【solved:6 / 13】
- MySql 的group by having 优化案例
- poi 读取写入excel
- UE4入门-常见的对象-概述
- DUBBO集群容错与负载均衡
- C++连接mysql数据库简单小例子(使用ODBC)
- 2017-12-08 python笔记2
- Ubuntu第一次登录给root用户设置密码
- SpringBoot开发微信公众号(一)
- python第三次
- STM32单片机以太网模块ENC28J60稳定 驱动程序SPI接口方便移植lwip或者uip