一、读EXCEL文件

 package com.ruijie.wis.cloud.utils;

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class ProjectImportUtil {
public ProjectImportUtil() {
String fileName = "D:\tst.xlsx";
  InputStream input = new FileInputStream(fileName);     
} /* 导入销售数据 ,excel2007格式 */
public List<Map<String,Object>> importSaleXml(InputStream input, String industry) {
List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
DecimalFormat df =new DecimalFormat("#0"); try {
XSSFWorkbook wb = new XSSFWorkbook(input); // Excel 2003 使用wb = new HSSFWorkbook(input); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
 int index_name = 0;
int index_province = 0;
int index_city = 0;
int index_acs = 0;
int index_aps = 0;
int index_zuozhi = 0;
String industry_name = industry;
XSSFSheet sheet = wb.getSheet(industry_name);
if(sheet == null) {
logger.warn("importSaleXml - industy: " + industry_name + " not exist!");
wb.close();
return null;
}
Iterator<Row> rows = sheet.rowIterator(); while (rows.hasNext()) {
  Row row = rows.next();
  if(row.getRowNum() == 0) { // 查找关心的数据所在列号
  Iterator<Cell> cells = row.cellIterator();
  while (cells.hasNext()) {
    Cell cell = cells.next();
    String title = getCellValue(cell,evaluator);
    if(title.equals("最终客户名称")) {
      index_name = cell.getColumnIndex();
    } else if(title.equals("省份")) {
      index_province = cell.getColumnIndex();
    } else if(title.equals("城市")) {
      index_city = cell.getColumnIndex();
    } else if(title.equals("AC系列")) {
      index_acs = cell.getColumnIndex();
    } else if(title.equals("AP系列")) {
      index_aps = cell.getColumnIndex();
    } else if(title.equals("卓智客户名称")) {
      index_zuozhi = cell.getColumnIndex();
  }
  }
}   Cell cell_name = row.getCell(index_name);
  Cell cell_province = row.getCell(index_province);
  Cell cell_city = row.getCell(index_city);
  Cell cell_acs = row.getCell(index_acs);
  Cell cell_aps = row.getCell(index_aps);
  String projectName = getCellValue(cell_name,evaluator);
  String province = getCellValue(cell_province,evaluator);
  String city = getCellValue(cell_city,evaluator);
  String acs = getCellValue(cell_acs,evaluator);
  String aps = getCellValue(cell_aps,evaluator);   Map<String,Object> salevalue = new HashMap<String, Object>();
  salevalue.put("projectName", projectName);
  salevalue.put("industry_name", industry_name);
  if(province != null) {  
    salevalue.put("province", province);
  }
  if(city != null) {
    salevalue.put("city", city);
  }
  if(acs != null) {  
    salevalue.put("acs", acs);
  }
  if(aps != null) {
    salevalue.put("aps", aps);   }   result.add(salevalue);
}
  wb.close();
} catch (Exception e) {
  logger.error(e.toString(),e);
}
  return result;
} //根据cell中的类型来返回数据
public String getCellValue(Cell cell, FormulaEvaluator evaluator) {
  if(cell == null) {
    return null;
  }
  CellValue cellValue = evaluator.evaluate(cell);
  if(cellValue == null) {
    return null;
  }
  switch (cellValue.getCellType()) {
  case HSSFCell.CELL_TYPE_NUMERIC:
  return cell.getNumericCellValue() + "";
  case HSSFCell.CELL_TYPE_STRING:
  return cell.getStringCellValue() + "";
  case HSSFCell.CELL_TYPE_BOOLEAN:
  return cell.getBooleanCellValue() + "";
  case HSSFCell.CELL_TYPE_FORMULA:
  return cell.getCellFormula();
  default:
  return null;
}
} }

二、写EXCEL文件

 XSSFWorkbook wb=new XSSFWorkbook();
DeviceAlarmUtil outputResult = new DeviceAlarmUtil();
wb = outputResult.outConfigExceptionResult(wb, "配置告警信息", configAlarmList);
response.setContentType("application/msexcel");
response.setHeader( "Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1" ) ); ServletOutputStream os = null;
try {
os = response.getOutputStream();
//写到输出流
wb.write(os);
12 } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (wb != null) {
try {
wb.close();
} catch (IOException e) {
throw new Exception("IO错误,无法导出结果");
}
}
if (os != null) {
try {
  os.flush();
os.close();
} catch (IOException e) {
throw new Exception("IO错误,无法导出结果");
}
}
} public XSSFWorkbook outConfigExceptionResult(XSSFWorkbook wb, String sheetName, List<Map<String, Object>> sheetResult){
XSSFSheet sheet=wb.createSheet(sheetName);
XSSFRow row=sheet.createRow(0);
CellStyle cellStyle =wb.createCellStyle();
// 设置这些样式
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
XSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font); XSSFCell cell = row.createCell((short) 0);
cell.setCellValue("客户名称");
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(0, 5500);
cell = row.createCell((short) 1);
cell.setCellValue("AC MAC");
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(1, 5000);
cell = row.createCell((short) 2);
cell.setCellValue("检查项");
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(2, 5000);
cell = row.createCell((short) 3);
cell.setCellValue("检查次数");
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(3, 5000);
cell = row.createCell((short) 4);
cell.setCellValue("检查时间");
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(4, 5000);
cell = row.createCell((short) 5);
cell.setCellValue("记录更新时间");
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(5, 5000);
cell = row.createCell((short) 6);
cell.setCellValue("当前状态");
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(6, 4000);
cell = row.createCell((short) 7);
cell.setCellValue("异常等级");
cell.setCellStyle(cellStyle);
sheet.setColumnWidth(7, 4000);
cell = row.createCell((short) 8); wb.setSheetName(0, sheetName);
if(sheetResult.size() == 0){
XSSFRow rows=sheet.createRow(1);
rows.setHeight((short) 500);
rows.createCell(0).setCellValue("没有查询结果!");
return wb;
} for(int info = 0; info < sheetResult.size(); info ++){
XSSFRow rows=sheet.createRow(info+1);
rows.setHeight((short) 500);
Map<String,Object> map = sheetResult.get(info); rows.createCell(0).setCellValue(map.get("name") + "");
rows.createCell(1).setCellValue(map.get("ac_mac") + "");
rows.createCell(2).setCellValue(map.get("item_code") + "");
rows.createCell(3).setCellValue(map.get("check_times") + "");
rows.createCell(4).setCellValue(map.get("check_time") + "");
rows.createCell(5).setCellValue(map.get("update_time") + "");
rows.createCell(6).setCellValue(map.get("status") + "");
rows.createCell(7).setCellValue(map.get("exception_level") + "");
//多插一行,避免单元格溢出
rows.createCell(8).setCellValue(" ");
} return wb;
}

java使用org.apache.poi读取与保存EXCEL文件的更多相关文章

  1. 【Java】使用Apache POI生成和解析Excel文件

    概述 Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作,分别有jxl和poi,2种方式. HSSF is the POI Project's pure ...

  2. Java中使用POI读取大的Excel文件或者输入流时发生out of memory异常参考解决方案

    注意:此参考解决方案只是针对xlsx格式的excel文件! 背景 前一段时间遇到一种情况,服务器经常宕机,而且没有规律性,查看GC日志发生了out of memory,是堆溢出导致的,分析了一下堆的d ...

  3. Java 使用Apache POI读取和写入Excel表格

    1,引入所用的包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxm ...

  4. Java开发小技巧(六):使用Apache POI读取Excel

    前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...

  5. Java使用POI读取和写入Excel指南

    Java使用POI读取和写入Excel指南 做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃 ...

  6. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  7. 用 Apache POI 读取 XLSX 数据

    最近因为项目的原因,需要从一些 Microsoft Office Excel 文件读取数据并加载到数据库. Google了一下方法,发现其实可以用的 Java 第三方库很多,最著名的是 Apache ...

  8. java使用poi读取doc和docx文件(maven自动导入依赖包)

    java使用poi读取doc和docx文件(maven自动导入依赖包) 于是在网上搜寻了一阵之后才发现原来doc文档和excel一样不能用普通的io流的方法来读取,而是也需要用poi,于是进行了一番尝 ...

  9. Apache POI – Reading and Writing Excel file in Java

    来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...

随机推荐

  1. acdream 1685 多民族王国(DFS,并查集)

    Problem Description 娜娜好不容易才回忆起自己是娜娜而不是什么Alice,也回忆起了自己要继续探索这个世界的目标,便偷偷溜出皇宫.娜娜发现这个王国有很多个民族组成,每个民族都有自己的 ...

  2. 理解matplotlib绘图

    matplotlib是基于Python语言的开源项目,旨在为Python提供一个数据绘图包.Matplotlib 可能是 Python 2D-绘图领域使用最广泛的套件.它能让使用者很轻松地将数据图形化 ...

  3. (转)每天一个Linux命令(4): mkdir

    http://www.cnblogs.com/peida/archive/2012/10/25/2738271.html linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前 ...

  4. 函数式宏定义用do...while(0)的好处

    函数式宏定义经常写成这样:<pre lang="c" escaped="true">#define device_init_wakeup(dev, ...

  5. android部分手机onclick事件触发2次

    var t1 = null; function btnSave() { if (t1 == null) { t1 = new Date().getTime(); } else { var t2 = n ...

  6. K2 K2Blackpearl安装步骤详解(服务端)

    转:http://www.cnblogs.com/dannyli/archive/2011/11/30/2269485.html 以下是K2 Blackpearl的安装步骤,本人亲测可用哦. 1.安装 ...

  7. 在Ubuntu中用root帐号登录

    一.其实我个人认为这没有多大必要,因为当你需要 root 的权限时,使用 sudo 便可以了.如果你实在需要在 Ubuntu 中启用 root 帐号的话,那么不妨执行下面的操作: 1.重新设置 roo ...

  8. DDoS攻防战(二):CC攻击工具实现与防御理论

    我们将要实现一个进行应用层DDoS攻击的工具,综合考虑,CC攻击方式是最佳选择,并用bash shell脚本来快速实现并验证这一工具,并在最后,讨论如何防御来自应用层的DDoS攻击. 第一步:获取大量 ...

  9. Yii系列教程(四):使用Memcache保存会话

    1环境准备 安装Memcached服务端: yum -y installmemcached.x86_64 安装PHP-Memcache扩展: yum -y installphp-pecl-memcac ...

  10. iframe根据子页面自动调整大小

    //iframe高度自适应 function IFrameReSize(iframename) { var pTar = document.getElementById(iframename); if ...