(1)操作excel的几种开源框架

  Apache POI

  OpenXls

  JEXCEL

  JXLS

  xlSQL

  JCOM


package com.common;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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.hssf.util.HSSFColor;
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.XSSFWorkbook;public class UploadExcel {public static void main(String[] args) throws IOException {String excelFilePath = "F://项目信息表格.XLSX";UploadExcel.readExcel(excelFilePath);//UploadExcel.writeExcel();
    }//对日期格式//数字类型的格式化public static String getFormatKey(short formatNumber, Date date){String format = "" ;SimpleDateFormat sdf = null;if(formatNumber == HSSFDataFormat.getBuiltinFormat("h:mm")){sdf = new SimpleDateFormat("HH:mm");}else if(formatNumber == HSSFDataFormat.getBuiltinFormat("m/d/yy")){sdf = new SimpleDateFormat("yyyy-MM-dd");}else if(formatNumber == HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")){sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");}else{sdf = new SimpleDateFormat("MM-dd");}format = sdf.format(date);return format;}public static String getCellValue(Cell cell){String cellValue = "";if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){cellValue = String.valueOf(cell.getBooleanCellValue());}else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){short format = cell.getCellStyle().getDataFormat(); //获取类型值String printString = "";if( format == 14 || format == 20 || format == 22|| format == 31 || format == 32 || format == 57 || format == 58){printString  =  UploadExcel.getFormatKey(format, cell.getDateCellValue());}else{DecimalFormat df = new DecimalFormat("#.#");printString = df.format(cell.getNumericCellValue()); }cellValue = printString;}else if(cell.getCellType() == Cell.CELL_TYPE_STRING){cellValue = cell.getStringCellValue();}else{cellValue = cell.getStringCellValue();}return cellValue;}//读excelpublic static void readExcel(String excelFilePath) throws IOException{FileInputStream inputStream = new FileInputStream(new File(excelFilePath));//读写xls和xlsx格式时,HSSFWorkbook针对xls,XSSFWorkbook针对xlsx
            Workbook workbook = null; int index = excelFilePath.lastIndexOf(".");String suffix = excelFilePath.substring(index + 1).toLowerCase();if ("xls".equals(suffix)){  workbook = new HSSFWorkbook(inputStream); }else if("xlsx".equals(suffix)){  workbook = new XSSFWorkbook(inputStream);  }  else{System.err.println("错误");}Sheet firstSheet = workbook.getSheetAt(0);int columns = firstSheet.getRow(0).getPhysicalNumberOfCells();int rows = firstSheet.getPhysicalNumberOfRows();for(int i = 0 ; i < rows; i++){Row row = firstSheet.getRow(i);for(int j = 0; j < columns; j++){Cell cell = row.getCell(j);System.out.println(UploadExcel.getCellValue(cell));}System.out.println("###");}/**Iterator<Row> iterator = firstSheet.iterator();while (iterator.hasNext()) {Row nextRow = iterator.next();int lastCell = nextRow.getLastCellNum();//一行的单元格个数,从1开始Iterator<Cell> cellIterator = nextRow.cellIterator();while (cellIterator.hasNext()) {Cell cell = cellIterator.next();switch (cell.getCellType()) {case Cell.CELL_TYPE_BOOLEAN:{System.out.print(cell.getBooleanCellValue());break;}case Cell.CELL_TYPE_NUMERIC:{short format = cell.getCellStyle().getDataFormat(); //获取类型值String printString = "";if( format == 14 || format == 20 || format == 22|| format == 31 || format == 32 || format == 57 || format == 58){printString  =  UploadExcel.getFormatKey(format, cell.getDateCellValue());}else{DecimalFormat df = new DecimalFormat("#.#");printString = df.format(cell.getNumericCellValue()); }System.out.print(printString);break;}case Cell.CELL_TYPE_STRING:{System.out.print(cell.getStringCellValue());break;}default : {System.out.print(cell.getStringCellValue());}}System.out.print("|");}System.out.println("###");}**/workbook.close();inputStream.close();}//写excelpublic static void writeExcel(){try {FileOutputStream fileOut = new FileOutputStream("F://poi-test.xls");HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet worksheet = workbook.createSheet("POI Worksheet");// index from 0,0... cell A1 is cell(0,0)HSSFRow row1 = worksheet.createRow((short) 0);HSSFCell cellA1 = row1.createCell((short) 0);cellA1.setCellValue("Hello");HSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);cellA1.setCellStyle(cellStyle);HSSFCell cellB1 = row1.createCell((short) 1);cellB1.setCellValue("Goodbye");cellStyle = workbook.createCellStyle();cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);cellB1.setCellStyle(cellStyle);HSSFCell cellC1 = row1.createCell((short) 2);cellC1.setCellValue(true);HSSFCell cellD1 = row1.createCell((short) 3);cellD1.setCellValue(new Date());cellStyle = workbook.createCellStyle();cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));cellD1.setCellStyle(cellStyle);workbook.write(fileOut);fileOut.flush();fileOut.close();System.out.println("读取结束");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
}

导出excel名称乱码转化

response.setHeader("Content-disposition", "attachment;filename=" + new String("销售管理导出订单".getBytes( "gb2312" ), "ISO8859-1" ) + ".xls");

//跨行、跨列
Integer rowRange = rowNumber + rowspanNumber - 1;sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowRange, 0,0));sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowRange, 1,1));
 

转载于:https://www.cnblogs.com/he0xff/p/6038455.html

通过 poi 导入 Excel代码相关推荐

  1. Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)

    ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...

  2. poi导出excel写入公式_【java poi 写入Excel后读取公式值问题】poi导入excel

    java poi 写入Excel后读取公式值问题 不用改公式,只需要用cell.getNumericCellValue()获取,读出来就是正确的值了,如果你读出来的还有问题,说明你其他的地方写的还有问 ...

  3. web端实现表单提交poi导入excel文件

    <!--虎牌导入会员数据--> <a class="btn btn-primary btn-sm search-btn btn-wide btn-scroll btn-sc ...

  4. 如何优雅的用POI导入Excel文件

    在企业级项目开发中,要经常涉及excel文件和程序之间导入导出的业务要求,那么今天来讲一讲excel文件导入的实现.java实现对excel的操作有很多种方式,例如EasyExcel等,今天我们使用的 ...

  5. Poi导入excel(合并单元格)

    1.引入pom文件 <!-- 操作以 .xls 为后缀的 Excel --><dependency><groupId>org.apache.poi</grou ...

  6. Poi导入Excel校验表头

    我们在日常的开发过程中,poi导入数据要根据注解的列名和顺序进行识别,那么首先要校验模版的正确与否,即表头是否正确,然后才能开始数据和业务层面的校验,最近也是被这个东西烦的不行,解决后记录一下,一个是 ...

  7. poi导入excel日期处理_POI处理Excel中各种日期格式问题

    前不久写过一篇随笔<EXCEL解析之终极方法WorkbookFactory>,提到使用WorkbookFactory来处理Excel文件数据,最近发现一个问题就是这个办法不能很好的处理各种 ...

  8. poi导入excel数据思路

    1.实现思路            (1)  准备excel模板,减少用户非法输入,减少报错率. (2)  导入excel,返回错误Map,key是行数,value是对应的报错. (3)  把错误列表 ...

  9. 解决POI导入EXCEL兼容问题

    公司项目中需要用到excel导入,在权衡使用jxl还是POI后,决定使用版本兼容性更好的POI.下面介绍一下使用poi遇到的问题: 一开始写了一个工具类关键代码如下: HSSFWorkbook wor ...

最新文章

  1. 面试题4:二维数组中的查找
  2. 雷林鹏分享:PHP 数组
  3. spdk-nvmf指南
  4. 伪代码block转换成程序流程图_程序设计基础
  5. css高度自动填满_Unity--自动版面(Grid Layout Group)
  6. C++中STL-队列和优先队列
  7. Spring中的面向切面(AOP)
  8. 20165331 第二周学习总结
  9. 调查问卷java源码_2020年Java技术趋势
  10. iOS:KVO/KVC 的概述与使用
  11. 在Finder中显示隐藏文件
  12. Http压力测试工具HttpTest4Net
  13. 关于DM8168中移植算法速度慢、效率低的新发现
  14. 软考历年真题以及真题试卷
  15. 南阳oj 韩信点兵
  16. 《陶哲轩实分析》阅读
  17. 全球各个国家及其首都中英文对照表
  18. P02014080杨臣----周五信息论课程回答作业
  19. Java高并发处理:请求合并
  20. Facebook想要成为下一个微信,难! 1

热门文章

  1. 吃透web前端秘籍,来听听大佬是怎么说的
  2. 新手小白该怎么学习前端?附学习路线和资料
  3. 常见的锁策略、synchronized中的锁优化机制
  4. python程序源码百行_百行python代码,轻松完成贪吃蛇小游戏
  5. VS studio 2019 加 CUDA + CUDNN 开发环境配置
  6. Python中出现“TabError: inconsistent use of tabs and spaces in indentation”
  7. 机器学习理论基础 集成学习前期基础--分类决策树与回归决策树
  8. simplejson.scanner.JSONDecodeError: Extra data: line 1 column 22089 - line 1 column 22090
  9. 使用spring集成的kafka收发消息
  10. warning LNK4098: defaultlib MSVCRT conflicts with use of other libs; use /NODE