java excel报表文件_java生成excel报表文件

packagereadExcel;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.text.DecimalFormat;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.CellStyle;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;public classReadExcel {/***@paramargs

*@throwsIOException*/

public List> readExcel(File file) throwsIOException{

List> list=new ArrayList>();if(!file.exists()){

System.out.println("文件不存在");

}else{

InputStream fis=newFileInputStream(file);

list=parseExcel(file,fis);

}returnlist;

}public List> parseExcel(File file,InputStream fis) throwsIOException{

Workbook workbook=null;

List> list=new ArrayList>();if(file.toString().endsWith("xls")){

workbook=newHSSFWorkbook(fis);

}else if(file.toString().endsWith("xlsx")){

workbook=newXSSFWorkbook(fis);

}else{

System.out.println("文件不是excel文档类型 ,此处无法读取");

}for(int i=0;i

Sheet sheet=workbook.getSheetAt(i);if(sheet!=null){int lastRow=sheet.getLastRowNum();//获取表格中的每一行

for(int j=0;j<=lastRow;j++){

Row row=sheet.getRow(j);short firstCellNum=row.getFirstCellNum();short lastCellNum=row.getLastCellNum();

List rowsList=new ArrayList();if(firstCellNum!=lastCellNum){//获取每一行中的每一列

for(int k=firstCellNum;k

Cell cell=row.getCell(k);if(cell==null){

rowsList.add("");

}else{

rowsList.add(chanegType(cell));

}

}

}else{

System.out.println("该表格只有一列");

}

list.add(rowsList);

}

}

}returnlist;

}publicString chanegType(Cell cell){

String result= newString();switch (cell.getCellType()) { //获取单元格的类型

case HSSFCell.CELL_TYPE_NUMERIC://数字类型

if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ //如果是数值类型

short format = cell.getCellStyle().getDataFormat(); //获取这个单元的类型对应的数值

SimpleDateFormat sdf = null;if(format == 14 || format == 31 || format == 57 || format == 58){ //如果数值为14,31,57,58其中的一种//对应的日期格式为 2016-03-01这种形式,

sdf = new SimpleDateFormat("yyyy-MM-dd");double value =cell.getNumericCellValue();

Date date=org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);

result= sdf.format(date);//得到yyyy-MM-dd这种格式日期

}else if (format == 20 || format == 32) {//时间

sdf = new SimpleDateFormat("HH:mm");double value =cell.getNumericCellValue();

Date date=org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);

result= sdf.format(date);//得到HH:mm

} else{double value =cell.getNumericCellValue();

CellStyle style=cell.getCellStyle();

DecimalFormat dataformat= newDecimalFormat();

String temp=style.getDataFormatString();//单元格设置成常规

if (temp.equals("General")) {

dataformat.applyPattern("#");

}

result= dataformat.format(value); //得到单元格数值

}

}break;case HSSFCell.CELL_TYPE_STRING://String类型

result =cell.getRichStringCellValue().toString();break;caseHSSFCell.CELL_TYPE_BLANK:

result= "";default:

result= "";break;

}returnresult;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值