Java Poi操作Excel文件
2017-11-08 18:26
351 查看
Excel操作类
package excelTest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelOperation { private static XSSFWorkbook workbook = null; /** *判断文件是否存在. */ public static boolean fileExist(String fileDir) { boolean flag = false; File file = new File(fileDir); flag = file.exists(); return flag; } /** *判断文件的sheet是否存在. */ public static boolean sheetExist(String fileDir, String sheetName) throws Exception { boolean flag = false; File file = new File(fileDir); if (file.exists()) { try { workbook = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet sheet = workbook.getSheet(sheetName); if (sheet != null) flag = true; } catch (Exception e) { throw e; } } else { flag = false; } return flag; } /** *创建新excel */ public static void createExcel(String fileDir, String sheetName, String titleRow[]) throws Exception { 1c6f4 workbook = new XSSFWorkbook(); FileOutputStream out = null; try { XSSFRow row = workbook.createSheet(sheetName).createRow(0); for (int i = 0; i < titleRow.length; i++) { XSSFCell cell = row.createCell(i); cell.setCellValue(titleRow); } out = new FileOutputStream(fileDir); [i]workbook.write(out); } catch (Exception e) { throw e; } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** *往excel中写入mapList.size()行数据 */ public static void writeToExcelRow(String fileDir, String sheetName, List<Map<String, String>> mapList, int rowId) throws Exception { File file = new File(fileDir); try { workbook = new XSSFWorkbook(new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream out = null; XSSFSheet sheet = workbook.getSheet(sheetName); // 获取表格的总行数 // introwCount = sheet.getLastRowNum() + 1; // 需要加一 // 获取表头的列数 int columnCount = sheet.getRow(0).getLastCellNum(); try { XSSFRow titleRow = sheet.getRow(0); if (titleRow != null) { for (Map<String, String> map : mapList) { XSSFRow newRow = sheet.createRow(rowId++); for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { String mapKey = titleRow.getCell(columnIndex).toString().trim(); XSSFCell cell = newRow.createCell(columnIndex); cell.setCellValue(map.get(mapKey) == null ? null : map.get(mapKey).toString()); } } } out = new FileOutputStream(fileDir); workbook.write(out); } catch (Exception e) { throw e; } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** *往excel中写入一格数据 */ public static void writeToExcelCell(String fileDir, String sheetName, String value, int rowId, int columnId) throws Exception { File file = new File(fileDir); try { workbook = new XSSFWorkbook(new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream out = null; XSSFSheet sheet = workbook.getSheet(sheetName); // 获取表格的总行数 // introwCount = sheet.getLastRowNum() + 1; // 需要加一 // 获取表头的列数 // intcolumnCount = sheet.getRow(0).getLastCellNum(); try { XSSFRow row = sheet.getRow(rowId); if (row != null) { XSSFCell cell = row.getCell(columnId); if (cell != null) { cell.setCellValue(value == null ? null : value); } else { XSSFCell newCell = row.createCell(columnId); newCell.setCellValue(value == null ? null : value); } } out = new FileOutputStream(fileDir); workbook.write(out); } catch (Exception e) { throw e; } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } } |
测试
package excelTest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ExcelTest { public static void main(String[] args) throws Exception{ String excelFilePath="D:/test.xlsx"; String sheetName="sheet0"; String[] titleRow={"id", "name", "password"}; Map<String, String> map1=new HashMap<String, String>(); map1.put("id","1001"); map1.put("name", "anna"); map1.put("password", "ANNA"); Map<String, String> map2=new HashMap<String, String>(); map2.put("id","1002"); map2.put("name", "bob"); map2.put("password", "BOB"); List<Map<String, String>> mapList=new ArrayList<Map<String, String>>(); mapList.add(map1); mapList.add(map2); System.out.println(ExcelOperation.fileExist(excelFilePath)); ExcelOperation.createExcel(excelFilePath, sheetName, titleRow); System.out.println(ExcelOperation.fileExist(excelFilePath)); ExcelOperation.writeToExcelRow(excelFilePath, sheetName, mapList, 1);//从表的第二行开始写 } } |
结果
jar包
需要的jar包jar包下载:http://poi.apache.org
ps:本文处理的是.xlsx文件,把代码中XSSF全部改成HSSF即可访问.xls文件。xls文件是07版之前的,一张表最多只能存65536行数据。xlsx是07版之后的Excel,可以存很多很多很多…
相关文章推荐
- POIjava操作excel文件
- java使用POI操作excel文件
- java操作excel文件(使用poi)
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI生成Excel文件(一)
- poi读取文件,并获取相应属性 (java操作Excel文件)
- java poi 创建XLS 用Jakarta POI操作Excel文件 POI创建 解析Excel
- java使用jxl与poi操作excel文件
- java使用POI操作excel文件,实现批量导出,和导入
- java POI操作Excel文件(合并单元格、插入图片、超链接)
- java poi操作excel文件 (新建Excel文件 添加行列、单元格
- poi笔记(1)--java操作excel文件
- java的POI操作excel文件
- POI 与 JXL 的一点比较——关于使用Java操作excel文件
- java poi操作excel文件 (新建Excel文件 添加行列、单元格)
- Java Poi 操作Excle
- java操作excel文件
- java生成和操作Excel文件
- Java对poi操作生成Excel表
- 利用JAVA操作EXCEL文件
- Java POI 操作excel 无硬编码,反射,史上最牛