POI操作excel
来源:互联网 发布:淘宝女装黑色毛衣 编辑:程序博客网 时间:2024/06/06 19:08
<pre name="code" class="java">public class ExcelUtil { public Workbook createXlsxWorkbook() { return new XSSFWorkbook(); } public static Workbook getWorkbook(String file) { if(StringUtils.isEmpty(file)) return null; try { return WorkbookFactory.create(new FileInputStream(file)); } catch (IOException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } return null; } public static Sheet getSheet(Workbook workbook, String sheetName) { if(workbook==null) return null; Sheet sheet = workbook.getSheet(sheetName); if(sheet==null) sheet=workbook.createSheet(sheetName); return sheet; } public static Cell getCell(Sheet sheet,int row,int col) { if(sheet==null) return null; Row sheetrow = sheet.getRow(row); if(sheetrow==null) sheetrow=sheet.createRow(row); Cell cell = sheetrow.getCell(col); if(cell==null) cell=sheetrow.createCell(col); return cell; } public static void setText(Cell cell,String text) { cell.setCellValue(text); } public static String getValue(Cell cell) { return cell.getStringCellValue(); }}
private static void importExcel(){ Workbook workbook = ExcelUtil.getWorkbook("E:/digital2016-09-21 10-49-39.xls"); Sheet sheet = ExcelUtil.getSheet(workbook, "非标设备"); for(Row row:sheet) { for(Cell cell:row) { if(cell!=null) { System.out.println(ExcelUtil.getValue(cell)); } } }}private static void export() { XSSFWorkbook workbook = new XSSFWorkbook();//创建工作薄 //Workbook workbook = ExcelUtil.CreateWorkbook("F:/1.xlsx"); if(workbook!=null) { Sheet sheet = ExcelUtil.getSheet(workbook, "国标设备"); ExcelUtil.setText(ExcelUtil.getCell(sheet,0,0),"PVM"); ExcelUtil.setText(ExcelUtil.getCell(sheet,0,1),"10.11.68.1"); ExcelUtil.setText(ExcelUtil.getCell(sheet,1,0),"PVM"); ExcelUtil.setText(ExcelUtil.getCell(sheet,1,1),"10.11.68.1"); ExcelUtil.setText(ExcelUtil.getCell(sheet,2,0),"PVM"); FileOutputStream fileOut=null; try { fileOut = new FileOutputStream("E:/1.xlsx"); workbook.write(fileOut); } catch (IOException e) { e.printStackTrace(); if(fileOut!=null) { try { fileOut.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }}
maven依赖
<pre name="code" class="html"> <!--exl表格文件解析--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.9</version> </dependency>
0 0
- POI操作EXCEL
- POI操作EXCEL
- POI操作Excel文档
- 使用POI操作Excel
- poi操作excel文件
- poi操作excel文件
- poi 操作Excel
- POI操作Excel
- POI 操作excel
- POI 操作EXCEL
- poi操作excel文件
- 用POI操作Excel
- poi操作excel
- poi 操作 excel
- POI操作excel大全
- POI操作Excel总结
- POI操作EXCEL
- POI操作EXCEL(一)
- 数学(统计问题,uva 1640)
- openwrt学习历程(三)
- Linux下的C语言编程——从指定位置复制
- Textview字体滚动
- 64位Ubuntu14.04 tomcat日志报错 There is insufficient memory for the Java Runtime Environment to continue
- POI操作excel
- js中的arguments的时间开销
- 一个shell程序
- mysql配置文件my.ini my.cnf实例
- HDU 5092 Seam Carving DP
- linux内核
- USB从了解都熟悉2-----USB通信协议
- mysql和oracle的区别
- 在roslaunch中如何单独打开另一个终端