读取excel大概在java中有两种方法,选用apache的POI读取
示例
示例
public class ExportStandName { /** * 取得标准表中的标准字段放在一个map的key中 * * @return standMap */ private Map<String, String> readStandExcel() { Map<String, String> standMap = new HashMap<String, String>(); String filePath = "path/Stand.xls"; File file = new File(filePath); try { FileInputStream fileInputStream = new FileInputStream(file); POIFSFileSystem readPoiFileSystem = new POIFSFileSystem( fileInputStream); HSSFWorkbook workbook = new HSSFWorkbook(readPoiFileSystem); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rows = sheet.rowIterator(); while (rows.hasNext()) { Row row = rows.next(); String name = row.getCell(1).getStringCellValue(); standMap.put(name, ""); } } catch (Exception e) { e.printStackTrace(); } for (String s : standMap.keySet()) { System.out.println(s); } return standMap; } /** * * * @param location * @param map * @return map */ private Map<String, String> readCityExcel(String location, Map<String, String> map) { Map<String,String>cityMap=new HashMap<String, String>(); String filePath = "C:/Users/Administrator/Desktop/工作/" + location + ".xls"; File file = new File(filePath); try { POIFSFileSystem readPoiFileSystem = new POIFSFileSystem( new FileInputStream(file)); HSSFWorkbook workbook = new HSSFWorkbook(readPoiFileSystem); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rows = sheet.rowIterator(); while (rows.hasNext()) { Row row = rows.next(); String value = row.getCell(0).getStringCellValue(); System.out.println(">>>>>" + value); Cell cell = row.getCell(3); if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) { String name = row.getCell(3).getStringCellValue(); if (name != null && name.length() > 0) { String mapValue = cityMap.get(name); if (map.containsKey(name)) { value += ","; mapValue += value; cityMap.put(name, mapValue); } } } } } catch (Exception e) { e.printStackTrace(); } return cityMap; } private void writeExcel(int index,String location, Map<String, String> map) { String filePath = "C:/Users/Administrator/Desktop/工作/Stand.xls"; File file = new File(filePath); FileOutputStream fileOutputStream = null; try { POIFSFileSystem readPoiFileSystem = new POIFSFileSystem(new FileInputStream(file)); HSSFWorkbook readWorkbook = new HSSFWorkbook(readPoiFileSystem); HSSFSheet readSheet = readWorkbook.getSheetAt(0); Row row1 = readSheet.getRow(0); if(row1==null){ row1=readSheet.createRow(0); } int rowNum = row1.getLastCellNum(); HSSFCell pnCell = null; if (index >=rowNum) { pnCell = (HSSFCell) row1.createCell(index); } else { pnCell=(HSSFCell)row1.getCell(index); } pnCell.setCellValue(location); for(Row row:readSheet){ HSSFCell nameCell=(HSSFCell)row.getCell(1); if(nameCell!=null){ String name=nameCell.getStringCellValue(); HSSFCell valueCell=(HSSFCell)row.getCell(index); if(valueCell==null){ valueCell=(HSSFCell)row.createCell(index); } String value=map.get(name); if(value!=null&&value.length()>0){ value = value.substring(4, value.length()-1); } valueCell.setCellValue(value); } } fileOutputStream=new FileOutputStream(file); fileOutputStream.flush(); readWorkbook.write(fileOutputStream); fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } public void doSomething() { Map<String, String> map = readStandExcel(); setStanMap(map); Map<String, String> map1=null; for (int i = 0; i < locations.length; i++) { map1= readCityExcel(locations[i], getStanMap()); writeExcel(i + 2, locations[i], map1); } } public static void main(String[] args) { new ExportStandName().doSomething(); // String value = "啊21保守党发按时"; // value = value.substring(0,value.length()-1); // System.out.println(value); } }
发表评论
-
java中线程池
2016-06-26 16:31 364最近在看线程池的类容,自己动手实现一个 package ... -
jvm常用设置
2015-09-08 22:48 383在我们实际工作中,可能会经常对JVM有一些设置,但是很多设置都 ... -
WebService相关
2015-07-26 21:55 970之前由于工作需要 ... -
java RMI
2015-07-23 23:04 441JAVA RMI相关 分布 ... -
java jdbc 连接池
2015-07-21 00:18 480经常使用hibernate,在使用hibernate的时候经常 ... -
java 流传输压缩包
2015-07-14 23:14 577有的时候,页面上的下载文件是从后台使用流的形式下载来的,而不是 ... -
java 网络传输文件
2015-06-01 21:14 433package sterning; import java. ... -
java MD5加密
2015-03-30 14:41 297MD5是常用的加密算法,也经常用于校验信息完整,如文件的完整性 ... -
java 并发容器
2014-12-18 23:19 355在多线程的开发 ... -
java设计模式 之 builder模式
2014-12-11 22:19 413最近在看java设计模式,看到builder模式感觉十分不错, ... -
网上见到一篇compareTo文章较好,收藏之
2014-12-04 21:04 5421、compareTo(Object o)方法是jav ... -
Hibernate 和 spring 整合使用
2014-10-14 21:28 753在hibernate和spring时总结的一些知识,以供将来查 ... -
hiernate 集中管理session
2014-10-13 21:55 483问题: 使用hibern ... -
java写入txt文件
2014-05-22 23:10 1092File file = new File("d: ... -
定时器 quartz 和 spring
2014-04-17 23:30 481最近学习了一个quartz 和spring 的定时调度 这个 ... -
java 基于Struts2的 文件上传和下载
2014-04-03 22:56 531文件上传: 页面代码: <form action=' ... -
java IO
2014-03-23 23:23 410import java.io.BufferedReader ... -
自己写的一个JDBC工具类
2014-03-11 20:00 1167在最近的一段时间写毕业设计,由于本科阶段的毕业设计不是特别大的 ... -
java汉字数字转换成阿拉伯数字
2014-02-20 17:14 6554今天做一个日期转换的时候发现有些时间是用大写汉字写成的,需 ...
相关推荐
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
JAVA IO操作 POI读取excel的内容
poi读取excel所需jar包,可用来使用poi读写word excel。
poi读取excel并输出到jsp页面
POI读取Excel
实现了JAVA 窗口,读取EXCEL文件,用poi读取EXCEL内容只是一个小例子
NULL 博文链接:https://yxhcquedu.iteye.com/blog/828707
apache poi 读取 Excel 的 jar 包 博文链接:https://wxinpeng.iteye.com/blog/231895
POI生成Excel POI读取Excel java操作Excel Servlet生成Excel web项目,包含含读取Excel与生成Excel方法
POI读取excel的例子
本案例中利用Apache Poi读取Excel用法,源码注释详细基本用法!
poi读取excel2007和2003兼容工具例子.利用简单工厂方法
之前用jxl发现不支持excel2007,不得以就去用poi实现excel的读取了!本人亲测的一个实例,支持excel2007!
解决poi读取excel2007出现内存溢出问题代码参
android5使用poi读取excel,android5使用poi读取excel,android5使用poi读取excel,android5使用poi读取excel
Java用poi读取excel文件Java用poi读取excel文件Java用poi读取excel文件
使用poi读取excel内容 所需jar包
POI 读取Excel文件 POI 读取Excel文件
阐述如何用POI来读取/写入完整的Excel文件。
利用POI取得Excel所用到的jar包