调用

public class ExcelUtil {

public static void fileDownload(String filename, InputStream input, HttpServletResponse response) {try {

// filename = “供应商基本信息模板.xls”;
byte[] buffer = new byte[4096];
int readLength = 0;
response.addHeader(“Content-Disposition”,
“attachment;filename=” + new String(filename.getBytes(“utf-8”), “ISO-8859-1”));
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
while ((readLength = input.read(buffer)) > 0) {
byte[] bytes = new byte[readLength];
System.arraycopy(buffer, 0, bytes, 0, readLength);
toClient.write(bytes);
}
input.close();
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}

}private final static String excel2003L =".xls";    //2003- 版本的excel
private final static String excel2007U =".xlsx";   //2007+ 版本的excel/*** 描述:获取IO流中的数据,组装成List<List<Object>>对象* @param in,fileName* @return* @throws IOException*/
public  List<Map<String, Object>> getBankListByExcel(InputStream in,String fileName) throws Exception{List<Map<String, Object>> list = null;//创建Excel工作薄Workbook work = this.getWorkbook(in,fileName);if(null == work){throw new Exception("创建Excel工作薄为空!");}Sheet sheet = null;  //页数Row row = null;  //行数Cell cell = null;  //列数list = new ArrayList<Map<String, Object>>();sheet = work.getSheetAt(0);//遍历当前sheet中的所有行for (int j = 1; j <= sheet.getLastRowNum(); j++) {row = sheet.getRow(j);//遍历所有的列Map<String, Object> li = new HashMap<String, Object>();for (int y = 1; y < row.getLastCellNum(); y++) {cell = row.getCell(y);if(j==1&&y==1) {cell = row.getCell(1);li.put("suppliername", this.getValue(cell));}else if(j==2&&y==1) {cell = row.getCell(1);li.put("companyaddress", this.getValue(cell));}else if(j==3&&y==1) {cell = row.getCell(1);li.put("legal", this.getValue(cell));}else if(j==3&&y==4) {cell = row.getCell(4);li.put("legalphone", this.getValue(cell));}else if(j==4&&y==1) {cell = row.getCell(1);li.put("dank", this.getValue(cell));}else if(j==5&&y==1) {cell = row.getCell(1);li.put("conpanytype", this.getValue(cell));}else if(j==6&&y==1) {cell = row.getCell(1);li.put("shcode", this.getValue(cell));}else if(j==6&&y==4) {cell = row.getCell(4);li.put("capital", this.getValue(cell));}else if(j==7&&y==1) {cell = row.getCell(1);li.put("registrationDate", this.getValue(cell));}else if(j==7&&y==4) {cell = row.getCell(4);li.put("effectiveDate", this.getValue(cell));}else if(j==8&&y==1) {cell = row.getCell(1);li.put("areaCovered", this.getValue(cell));}else if(j==8&&y==4) {cell = row.getCell(4);li.put("peopleQk", this.getValue(cell));}else if(j==9&&y==1) {cell = row.getCell(1);li.put("contacts", this.getValue(cell));}else if(j==9&&y==4) {cell = row.getCell(4);li.put("phoneNumber", this.getValue(cell));}else if(j==10&&y==1) {cell = row.getCell(1);li.put("fixPhone", this.getValue(cell));}else if(j==10&&y==4) {cell = row.getCell(4);li.put("yx", this.getValue(cell));}else if(j==11&&y==1) {cell = row.getCell(1);li.put("mainProSer", this.getValue(cell));}else if(j==17&&y==1) {cell = row.getCell(1);li.put("mainCustomers", this.getValue(cell));}}list.add(li);

// list.addAll(dl);

 }List< Map<String, Object>> dl = new ArrayList<>();Map<String, Object> map12=new HashMap<>();Row row12 = sheet.getRow(12);map12.put("PRODUCT_NAME1", this.getValue(row12.getCell(1)));map12.put("CODE1", this.getValue(row12.getCell(3)));map12.put("CER_TIME1", this.getValue(row12.getCell(5)));dl.add(map12);Map<String, Object> map13=new HashMap<>();Row row13 = sheet.getRow(13);map13.put("PRODUCT_NAME2", this.getValue(row13.getCell(1)));map13.put("CODE2", this.getValue(row13.getCell(3)));map13.put("CER_TIME2", this.getValue(row13.getCell(5)));dl.add(map13);Map<String, Object> map14=new HashMap<>();Row row14 = sheet.getRow(14);map14.put("PRODUCT_NAME3", this.getValue(row14.getCell(1)));map14.put("CODE3", this.getValue(row14.getCell(3)));map14.put("CER_TIME3", this.getValue(row14.getCell(5)));dl.add(map14);list.addAll(2, dl);return list;}/*** 描述:根据文件后缀,自适应上传文件的版本* @param inStr,fileName* @return* @throws Exception*/
public  Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{Workbook wb = null;String fileType = fileName.substring(fileName.lastIndexOf("."));if(excel2003L.equals(fileType)){wb = new HSSFWorkbook(inStr);  //2003-}else if(excel2007U.equals(fileType)){wb = new XSSFWorkbook(inStr);  //2007+}else{throw new Exception("解析的文件格式有误!");}return wb;
}/*** 描述:对表格中数值进行格式化* @param cell* @return*/
//解决excel类型问题,获得数值
public  String getValue(Cell cell) {String value = "";if(null==cell){return value;}switch (cell.getCellType()) {//数值型case NUMERIC:if (HSSFDateUtil.isCellDateFormatted(cell)) {//如果是date类型则 ,获取该cell的date值Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");value = format.format(date);;}else {// 纯数字BigDecimal big=new BigDecimal(cell.getNumericCellValue());value = big.toString();//解决1234.0  去掉后面的.0if(null!=value&&!"".equals(value.trim())){String[] item = value.split("[.]");if(1<item.length&&"0".equals(item[1])){value=item[0];}}}break;//字符串类型case STRING:value = cell.getStringCellValue().toString();break;// 公式类型case FORMULA://读公式计算值value = String.valueOf(cell.getNumericCellValue());if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串value = cell.getStringCellValue().toString();}break;// 布尔类型case BOOLEAN:value = " "+ cell.getBooleanCellValue();break;default:value = cell.getStringCellValue().toString();}if("null".endsWith(value.trim())){value="";}return value;
}/*** 移除map的空key* @param map* @return*/
public static void removeNullKey(Map map){Set set = map.keySet();/*for (Iterator iterator = set.iterator(); iterator.hasNext();) {Object obj = (Object) iterator.next();remove(obj, iterator);}*/
}

}

Excel下载附件工具类相关推荐

  1. 一个基于POI的通用excel导入导出工具类的简单实现及使用方法

    前言: 最近PM来了一个需求,简单来说就是在录入数据时一条一条插入到系统显得非常麻烦,让我实现一个直接通过excel导入的方法一次性录入所有数据.网上关于excel导入导出的例子很多,但大多相互借鉴. ...

  2. java中使用jxl导出excel表格的工具类(全网唯一亲测可用,在原来基础上扩展)

    java中后台导出excel的话,有两种方案,一是使用poi(不过由于是windows版本的,存在不兼容,但功能更多,更强大),而是使用jxl(纯java编写,不过兼容,简单一些),可以设置输出的ex ...

  3. 基于POI的读写Excel文件的工具类

    依赖的jar包: import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStrea ...

  4. java spring文件下载_SpringMVC实现文件上传和下载的工具类

    本文主要目的是记录自己基于SpringMVC实现的文件上传和下载的工具类的编写,代码经过测试可以直接运行在以后的项目中. 开发的主要思路是对上传和下载文件进行抽象,把上传和下载的核心功能抽取出来分装成 ...

  5. golang利用反射写入excel的简单工具类

    golang利用反射写入excel的简单工具类 工具类源码 使用方法 工具类源码 package excelimport ("errors""github.com/tea ...

  6. java Excel导入导出工具类 及使用demo

    java Excel导入导出工具类 及使用demo 前言:相信进来的都是想尽快解决问题的,话不多说,按照以下步骤来,可以操作导出excel到本地,导入同理,自行学习.步骤一:直接复制以下excel工具 ...

  7. Java操作百万数据量Excel导入导出工具类(程序代码教程)

    Java操作百万数据量Excel导入导出工具类(程序代码教程): # 功能实现1.自定义导入数据格式,支持配置时间.小数点类型(支持单/多sheet)(2种方式:本地文件路径导入(只支持xls.xls ...

  8. 常用工具类五 Excel转图片工具类

    市面上大多数excel转图片为收费工具,借鉴他人用awt的Graphics2D自己实现的工具类,只涉及poi依赖. /** * 版权: taylor * 描述: 将excel转为图片工具类 * 创建时 ...

  9. excel转图片工具类

    添加依赖 <repository><id>AsposeJavaAPI</id><name>Aspose Java API</name>< ...

最新文章

  1. 达观数据于敬:个性化推荐系统实践
  2. 清华团队将Transformer用到3D点云分割上后,效果好极了
  3. 产业丨一文读懂人工智能产业链,未来10年2000亿美元市场
  4. Animy.js,自己编写的功能丰富的html动画库
  5. python包导入细节_python循环导入是一个实现细节吗?
  6. 170 道 Python 爬虫面试题(2019 版)
  7. 【电子信息复试】考研复试常考问题——软件工程
  8. 「C语言」数据类型及混合运算与类型转换
  9. 测试 MathJax 排版功效
  10. photoshop制作gif去掉杂色
  11. AMD上线Linux专版驱动17.10:支持最新API接口
  12. ad怎么批量改元器件封装_在AD软件中的PCB界面如何批量修改封装?
  13. tabbar图标大小更改
  14. 2015年9月 javaweb餐厅系统
  15. (转载)0x0F1AFD76 (libcocos2d.dll) (Plane.exe 中)处有未经处理的异常: 0xC0000005: 读取位置 0x00000018 时发生访问冲突。
  16. Adb文件及文件夹操作命令
  17. 一个二维码实现苹果和安卓两个市场安装包自动分发
  18. 宏任务和微任务的详解
  19. Java定时任务调度工具之Timer
  20. Association-Aggregation-Composition区别

热门文章

  1. C#类的成员之Field(字段)
  2. 数据链路层故障排除实例
  3. 如何用计算机给手机杀毒,怎么用手机给u盘杀毒
  4. 渝粤题库 陕西师范大学 《教育法学》作业
  5. 阿里云域名持有者过户
  6. stm32开发3D打印机(五)——TF卡spi协议与FATFS文件系统(已完成)
  7. 未来几年GPT/大模型如何影响软件研发?
  8. 30 岁后,让你走上坡路的 3 种能力
  9. CpG ODN——艾美捷ODN 1826 (TLRGRADE)说明书
  10. 普元元数据产品如何安装到普元应用服务器AppServer上