- 浏览: 174786 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (321)
- eclipse (4)
- idea (2)
- Html (8)
- Css (14)
- Javascript (8)
- Jquery (6)
- Ajax Json (4)
- Bootstrap (0)
- EasyUI (0)
- Layui (0)
- 数据结构 (0)
- Java (46)
- DesPattern (24)
- Algorithm (2)
- Jdbc (8)
- Jsp servlet (13)
- Struts2 (17)
- Hibernate (11)
- Spring (5)
- S2SH (1)
- SpringMVC (4)
- SpringBoot (11)
- WebService CXF (4)
- Poi (2)
- JFreeChart (0)
- Shiro (6)
- Lucene (5)
- ElasticSearch (0)
- JMS ActiveMQ (3)
- HttpClient (5)
- Activiti (0)
- SpringCloud (11)
- Dubbo (6)
- Docker (0)
- MySQL (27)
- Oracle (18)
- Redis (5)
- Mybatis (11)
- SSM (1)
- CentOS (10)
- Ant (2)
- Maven (4)
- Log4j (7)
- XML (5)
最新评论
导入jar包 poi-3.9-20121203.jar
1. 创建新工作簿
Demo01.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; public class Demo01 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 FileOutputStream fileOut = new FileOutputStream("E:\\用Poi创建的工作簿.xls"); wb.write(fileOut); fileOut.close(); } }
2. 创建新Sheet页
Demo02.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; public class Demo02 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 wb.createSheet("第二个Sheet页"); // 创建第二个Sheet页 FileOutputStream fileOut = new FileOutputStream("E:\\用Poi创建的Sheet页.xls"); wb.write(fileOut); fileOut.close(); } }
3. 创建单元格
Demo03.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo03 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(0); // 创建一个行 Cell cell = row.createCell(0); // 创建一个单元格 第1列 cell.setCellValue(1); // 给单元格设置值 row.createCell(1).setCellValue(1.2); // 创建一个单元格 第2列 值是1.2 row.createCell(2).setCellValue("这是一个字符串类型"); // 创建一个单元格 第3列 值为一个字符串 row.createCell(3).setCellValue(false); // 创建一个单元格 第4列 值为布尔类型 FileOutputStream fileOut = new FileOutputStream("E:\\用Poi创建的Cell.xls"); wb.write(fileOut); fileOut.close(); } }
4. 创建一个时间格式的单元格
Demo04.java package com.andrew.poi; import java.io.FileOutputStream; import java.util.Calendar; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo04 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(0); // 创建一个行 Cell cell = row.createCell(0); // 创建一个单元格 第1列 cell.setCellValue(new Date()); // 给单元格设置值 CreationHelper createHelper = wb.getCreationHelper(); CellStyle cellStyle = wb.createCellStyle(); // 单元格样式类 cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyy-mm-dd hh:mm:ss")); cell = row.createCell(1); // 第二列 cell.setCellValue(new Date()); cell.setCellStyle(cellStyle); cell = row.createCell(2); // 第三列 cell.setCellValue(Calendar.getInstance()); cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("E:\\工作簿04.xls"); wb.write(fileOut); fileOut.close(); } }
5. 处理不同内容格式的单元格
Demo05.java package com.andrew.poi; import java.io.FileOutputStream; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo05 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(0); // 创建一个行 Cell cell = row.createCell(0); // 创建一个单元格 第1列 cell.setCellValue(new Date()); // 给单元格设置值 row.createCell(1).setCellValue(1); row.createCell(2).setCellValue("一个字符串"); row.createCell(3).setCellValue(true); row.createCell(4).setCellValue(HSSFCell.CELL_TYPE_NUMERIC); row.createCell(5).setCellValue(false); FileOutputStream fileOut = new FileOutputStream("E:\\工作簿05.xls"); wb.write(fileOut); fileOut.close(); } }
6. 遍历工作簿的行和列并获取单元格内容
Demo06.java package com.andrew.poi; import java.io.FileInputStream; import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class Demo06 { public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("E:\\二货名单.xls"); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet hssfSheet = wb.getSheetAt(0); // 获取第一个Sheet页 if (hssfSheet == null) { return; } // 遍历行Row for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow == null) { continue; } // 遍历列Cell for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++) { HSSFCell hssfCell = hssfRow.getCell(cellNum); if (hssfCell == null) { continue; } System.out.print(" " + getValue(hssfCell)); } System.out.println(); } } private static String getValue(HSSFCell hssfCell) { if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { return String.valueOf(hssfCell.getNumericCellValue()); } else { return String.valueOf(hssfCell.getStringCellValue()); } } } 运行结果: 1.0 tom 12.0 true 2.0 cat 15.0 false
7. 文本提取
Demo07.java package com.andrew.poi; import java.io.FileInputStream; import java.io.InputStream; import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class Demo07 { public static void main(String[] args) throws Exception { InputStream is = new FileInputStream("E:\\二货名单.xls"); POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); ExcelExtractor excelExtractor = new ExcelExtractor(wb); excelExtractor.setIncludeSheetNames(false);// 我们不需要Sheet页的名字 System.out.println(excelExtractor.getText()); } } 运行结果: 1 tom 12 true 2 cat 15 false
8. 单元格对齐方式
Demo08.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo08 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(1); // 创建一个行 Cell cell = row.createCell(1); cell.setCellValue("XX"); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色 cellStyle.setFillPattern(CellStyle.BIG_SPOTS); cell.setCellStyle(cellStyle); Cell cell2 = row.createCell(2); cell2.setCellValue("YYY"); CellStyle cellStyle2 = wb.createCellStyle(); cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色 cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND); cell2.setCellStyle(cellStyle2); FileOutputStream fileOut = new FileOutputStream("E:\\工作簿08.xls"); wb.write(fileOut); fileOut.close(); } }
9. 单元格边框处理
Demo09.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo09 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(1); // 创建一个行 Cell cell = row.createCell(1); // 创建一个单元格 cell.setCellValue(4); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色 cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 左边边框 cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); // 左边边框颜色 cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框 cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // 右边边框颜色 cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边边框颜色 cell.setCellStyle(cellStyle); FileOutputStream fileOut = new FileOutputStream("e:\\工作簿09.xls"); wb.write(fileOut); fileOut.close(); } }
10. 单元格填充色和颜色操作
Demo10.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo10 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(1); // 创建一个行 Cell cell = row.createCell(1); cell.setCellValue("XX"); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色 cellStyle.setFillPattern(CellStyle.BIG_SPOTS); cell.setCellStyle(cellStyle); Cell cell2 = row.createCell(2); cell2.setCellValue("YYY"); CellStyle cellStyle2 = wb.createCellStyle(); cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色 cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND); cell2.setCellStyle(cellStyle2); FileOutputStream fileOut = new FileOutputStream("e:\\工作簿10.xls"); wb.write(fileOut); fileOut.close(); } }
11. 单元格合并
Demo11.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo11 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(1); // 创建一个行 Cell cell = row.createCell(1); cell.setCellValue("单元格合并测试"); sheet.addMergedRegion(new CellRangeAddress(1, // 起始行 2, // 结束行 1, // 其实列 2 // 结束列 )); FileOutputStream fileOut = new FileOutputStream("e:\\工作簿11.xls"); wb.write(fileOut); fileOut.close(); } }
12. 字体处理
Demo12.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo12 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(1); // 创建一个行 // 创建一个字体处理类 Font font = wb.createFont(); font.setFontHeightInPoints((short) 24); font.setFontName("Courier New"); font.setItalic(true); font.setStrikeout(true); CellStyle style = wb.createCellStyle(); style.setFont(font); Cell cell = row.createCell((short) 1); cell.setCellValue("This is test of fonts"); cell.setCellStyle(style); FileOutputStream fileOut = new FileOutputStream("e:\\工作簿12.xls"); wb.write(fileOut); fileOut.close(); } }
13. 读取和重写工作簿
Demo13.java package com.andrew.poi; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo13 { public static void main(String[] args) throws Exception { InputStream inp = new FileInputStream("e:\\工作簿.xls"); POIFSFileSystem fs = new POIFSFileSystem(inp); Workbook wb = new HSSFWorkbook(fs); Sheet sheet = wb.getSheetAt(0); // 获取第一个Sheet页 Row row = sheet.getRow(0); // 获取第一行 Cell cell = row.getCell(0); // 获取单元格 if (cell == null) { cell = row.createCell(3); } cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue("测试单元格"); FileOutputStream fileOut = new FileOutputStream("e:\\工作簿04.xls"); wb.write(fileOut); fileOut.close(); } }
14. 单元格中使用换行
Demo14.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo14 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 Row row = sheet.createRow(2); // 创建一个行 Cell cell = row.createCell(2); cell.setCellValue("我要换行 \n 成功了吗?"); CellStyle cs = wb.createCellStyle(); // 设置可以换行 cs.setWrapText(true); cell.setCellStyle(cs); // 调整下行的高度 row.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints()); // 调整单元格宽度 sheet.autoSizeColumn(2); FileOutputStream fileOut = new FileOutputStream("e:\\工作簿14.xls"); wb.write(fileOut); fileOut.close(); } }
15. 创建用户自定义数据格式
Demo15.java package com.andrew.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo15 { public static void main(String[] args) throws Exception { Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页 CellStyle style; DataFormat format = wb.createDataFormat(); Row row; Cell cell; short rowNum = 0; short colNum = 0; row = sheet.createRow(rowNum++); cell = row.createCell(colNum); cell.setCellValue(111111.25); style = wb.createCellStyle(); style.setDataFormat(format.getFormat("0.0")); // 设置数据格式 cell.setCellStyle(style); row = sheet.createRow(rowNum++); cell = row.createCell(colNum); cell.setCellValue(1111111.25); style = wb.createCellStyle(); style.setDataFormat(format.getFormat("#,##0.000")); cell.setCellStyle(style); FileOutputStream fileOut = new FileOutputStream("e:\\工作簿15.xls"); wb.write(fileOut); fileOut.close(); } }
相关推荐
POI包对Excel的基本操作使用。
使用POI实现报表打印功能。博客地址:http://blog.csdn.net/sdksdk0/article/details/53393453 一些基本的操作手册和用户所需要的api文档。
java通过poi操作excel的基本例子. 单元格基本操作 poi画线 矩形 圆形 excel表的操作 行列操作
poi操作Excel表格基本操作
POI 官方API大全及基本操作实例(含jar包),通过实例和查看API,你能够很快掌握基本的excel操作。
1.2 POI基本类 5 1.3 POI简单读取Excel数据 5 1.4 POI简单写出Excel 9 2.复杂读取 16 2.1 单元格各类型数据读取 16 2.1.1 基本类型 16 2.1.2 日期类型 18 2.2 自定义类型 21 3.复杂写入 22 3.1 复杂写入 22 3.2 ...
POIFS:此组件是所有其他POI元素的基本因素。 它用于显式读取不同的文件。 HSSF:用于读取和写入.xls格式的MS-Excel文件。 XSSF(XML SpreadSheet格式):用于MS-Excel的.xlsx文件格式。 HPSF:用于提取MS-Office文件的...
POI3.5_HSSF_和XSSF_Excel操作快速入门手册.pdf
1.2 POI基本类 5 1.3 POI简单读取Excel数据 5 1.4 POI简单写出Excel 9 2.复杂读取 16 2.1 单元格各类型数据读取 16 2.1.1 基本类型 16 2.1.2 日期类型 18 2.2 自定义类型 21 3.复杂写入 22 3.1 复杂写入 22 3.2 ...
poi的基础使用教程,poi操作excel 上传,下载,单元格的操作
用java开发,根据poi操作excel的导入,导出功能,基本上能满足业务的80%需求操作
POI操作包括,上下合并单元格,左右合并单元格,设置单元格基本格式,写入公式简单操作,做个备份记录学习用.
Apache的POI组件是Java操作Microsoft Office办公套件的强大API,其中对Word,Excel和PowperPoint都有支持,当然使用较多的还是Excel,因为Word和PowerPoint用程序动态操作的应用较少。那么本文就结合POI来介绍一下...
关于poi的一些基本操作方法和api文档
POI的一些基本的操作,对excel的一些简单操作,欢迎下载
poi java操作office的基本功能, 非常不错,可以尝试
poiExcel简单操作,这里面主要举了一个例子,这个例子是简单的讲述如何使用poi获取Excle里面的值的方式,很简单,但是也是最基础的东西。
通过实例代码,帮助大家掌握POI操作excel2003,2007调用的方法名不同,但基本类似。
除开最基本的介绍和使用介绍外,另有使用详解,包含POI结构与常用类、Excel的基本操作、Excel的单元格操作、使用Excel的公式、使用图形、Excel表操作、Excel行列操作、Excel的其他功能、POI对字的基本操作等……
java的POI操作Excel文件.doc POI_API帮助文档.chm poi-bin-3.9-20121203.tar.gz poi帮助.docx POI中文帮助文档.pdf poi中文教程.doc第一章 POI简介 实际的开发中,表现层的解决方案虽然有多样,但是IE浏览器已成为...