poi对excel的基本读写操作
最近简单的弄了下poi对excel的应用,为方便自己以后的使用就把一些基本操作记录下来,其他更复杂的操作可以等以后有需求的时候再来深入了解一番!
写操作:
- /**
- *
- * 层次结构就是workbook-->Sheet-->Row-->Cell
- * 只要按照这种层次结构操作就不会有什么大的问题
- * @author Administrator
- *
- */
- public class Test1 {
- public static void main(String args[]) throws IOException {
- //HSSFWorkbook对应的是2003
- //XSSFWorkbook对应的是2007
- //对于03和07它们的操作都是差不多的,只是在需要用07的时候把相应的HSSF前缀改成XSSF前缀就可以了
- //第一步建一个工作簿,即workbook
- Workbook workbook = new HSSFWorkbook();
- //第二步建一个工作表单,急sheet
- Sheet sheet = workbook.createSheet("mysheet1");
- for (int i=0;i<5;i++) {
- //有了表单以后就是行Row了,
- Row row = sheet.createRow(i);
- for (int j=0;j<5;j++) {
- //有了row以后就是row上的一个个小的单元格了
- Cell cell = row.createCell(j);
- //给单元格添加内容
- cell.setCellValue("row"+(i+1)+",column"+(j+1));
- }
- }
- //建一个用于存放新建的excel的文件输出流
- OutputStream os = new FileOutputStream("file/test1.xls");
- //把形成的workbook写到一个输出流里面
- workbook.write(os);
- os.close();
- }
- }
读操作:
- public class Test4 {
- public static void main(String args[]) throws IOException {
- InputStream is = new FileInputStream("file/test1.xls");
- Workbook wb = new HSSFWorkbook(is);
- Sheet sheet = wb.getSheetAt(0);
- // 因为Row,Cell,Sheet都继承了java.lang.Iterator接口,所以可以用下面的方法来进行遍历
- for (Row row : sheet) {
- for (Cell cell : row) {
- int cellType = cell.getCellType();
- switch (cellType) {
- //在取数据的时候类型一定要取对,否则将抛出异常
- case Cell.CELL_TYPE_STRING:
- String str = cell.getRichStringCellValue().getString();
- System.out.println(str);// 对取得的数据的简单处理;
- break;
- case Cell.CELL_TYPE_BLANK:
- if (DateUtil.isCellDateFormatted(cell)) {
- System.out.println(cell.getDateCellValue());
- } else {
- System.out.println(cell.getNumericCellValue());
- }
- break;
- case Cell.CELL_TYPE_FORMULA:
- System.out.println(cell.getCellFormula());
- break;
- case Cell.CELL_TYPE_BOOLEAN:
- System.out.println(cell.getBooleanCellValue());
- break;
- default:
- System.out.println("---------------------");
- break;
- }
- }
- }
- //当然还可以这样来遍历
- //row是从1开始的,cell是从头0开始的,但是在创建的时候它们都是0-based;
- for (int i=0;i<sheet.getLastRowNum()+1;i++) {
- Row row = sheet.getRow(i);
- if (row==null)
- continue;
- for (int j=0;j<row.getLastCellNum();j++) {
- Cell cell = row.getCell(j);
- if (cell==null)
- continue;
- //在取数据的时候数据类型一定要取对,否则将抛出异常,like NumberFormatException
- System.out.println(cell.getStringCellValue());
- }
- }
- is.close();
- }
- }
合并单元格:
- public static void main(String args[]) throws IOException {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("sheet1");
- Row row = sheet.createRow(1);
- Cell cell = row.createCell(1);
- cell.setCellValue("a test of merge!");
- //执行合并操作的语句
- sheet.addMergedRegion(new CellRangeAddress(
- 1,// 开始行
- 1,// 结束行
- 1,// 开始列
- 3// 结束列
- ));
- OutputStream os = new FileOutputStream("file/test3.xls");
- wb.write(os);
- os.close();
- }
换行:
- public static void main(String args[]) throws IOException {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet();
- Row row = sheet.createRow(6);
- sheet.autoSizeColumn(2);
- for (int i=0;i<5;i++) {
- Cell cell = row.createCell(i+2);
- CellStyle style = wb.createCellStyle();
- //to set cell newLine should set its wrap true
- style.setWrapText(true);
- //利用\n来实现换行操作,只有在Cell设置为setWrapText(true)的时候才能实现人为的换行
- cell.setCellValue("just use \n to wrap in a cell!");
- cell.setCellStyle(style);
- }
- OutputStream os = new FileOutputStream("file/test6_newLine.xls");
- wb.write(os);
- os.close();
- System.out.println("----------------------------");
- }
画图:
- //drawing shapes
- /*
- * To create a shape you have to go through the following steps:
- 1.Create the patriarch.
- 2.Create an anchor to position the shape on the sheet.
- 3.Ask the patriarch to create the shape.
- 4.Set the shape type (line, oval, rectangle etc...)
- 5.Set any other style details converning the shape. (eg: line thickness, etc...)
- */
- HSSFPatriarch partriarch = (HSSFPatriarch) sheet5.createDrawingPatriarch();
- HSSFSimpleShape shape = partriarch.createSimpleShape(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,5));
- shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
- //shape可以设置很多的属性
- shape.setFillColor(255,200,200);
- shape.setLineStyle(HSSFSimpleShape.LINESTYLE_DASHGEL);
- //Text boxes are created using a different call:
poi对excel的基本读写操作的更多相关文章
- java封装实现Excel建表读写操作
对 Excel 进行读写操作是生产环境下常见的业务,网上搜索的实现方式都是基于POI和JXL第三方框架,但都不是很全面.小编由于这两天刚好需要用到,于是就参考手写了一个封装操作工具,基本涵盖了Exce ...
- Python Excel文件的读写操作(xlwt xlrd xlsxwriter)
转:https://www.cnblogs.com/ultimateWorld/p/8309197.html Python语法简洁清晰,作为工作中常用的开发语言还是很强大的(废话). python关于 ...
- [python]使用xlrd对Excel表格进行读写操作
2.1 导入模块 import xlrd 2.2 打开Excel文件读取数据 data = xlrd.open_workbook("excelFile.xls") 2.3 使用技巧 ...
- pandas对Excel文件的读写操作
1.将Excel数据读为dataframe 1.1 直接读取 df = pd.read_excel('data.xlsx') 1.2 根据sheet索引 xls = pd.ExcelFile('dat ...
- python对excel文件的读写操作
import xlrd,xlwt data = xlrd.open_workbook('a.xlsx') #读 table = data.sheets()[0] data_list = [] data ...
- spring boot 使用 POI 读取Excel文件
内容简介 本文主要介绍使用POI进行Excel文件的相关操作,涉及读取文件,获取sheet表格,对单元格内容进行读写操作,以及合并单元格的操作. Excel文件目录 Excel模板文件存了resour ...
- python读取数据写入excel的四种操作
Python对Excel的读写主要有:xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种 xlutils结合xlrd: 操作的是以xls后缀的excel,读取文件保留原格式 ...
- 使用POI来实现对Excel的读写操作
事实上我感觉直接贴代码就好了.代码里面差点儿做到每一行一个凝视.应该看起来会比較简单 代码托管在github上:https://github.com/chsj1/ExcelUtils package ...
- Java学习---Excel读写操作
1.1.1. 简介 Apache POI 使用Apache POI 完成Excel读写操作 Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API ...
随机推荐
- Go语言程序结构分析初探
每一种编程语言都有自己的语法.结构以及自己的风格,这也是每种语言展现各自魅力及众不同的地方.Go也不例外,它简单而优雅,与此同时使用起来也很有趣.在本文中,我们将讨论以下几点: Go程序结构 如何运行 ...
- swagger 常用注解说明
本内容引用自:https://blog.csdn.net/u014231523/article/details/76522486 常用注解: - @Api()用于类: 表示标识这个类是swagger的 ...
- JavaWeb学习 (十四)————JSP基础语法
一.JSP模版元素 JSP页面中的HTML内容称之为JSP模版元素. JSP模版元素定义了网页的基本骨架,即定义了页面的结构和外观. 二.JSP表达式 JSP脚本表达式(expression)用于将 ...
- httpd配置文件httpd.conf规则说明和一些基本指令
apache httpd系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 本文主要介绍的是httpd的配置文件,包括一些最基本的指令.配置规 ...
- vue-cli脚手架搭建项目简单入门一
搭建系统: Windows系统 简单了解Node.js.npm,安装Node.js,下载网址:http://nodejs.cn/download/ 查看node,npm安装成功与否.打开cmd命令行, ...
- Install/Remove of the Service Denied!
在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld --install 报错: 信息如下一: Install/Remove of the Service Den ...
- html页面背景设定相关
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- border-sizing属性详解和应用
box-sizing用于更改用于计算元素宽度和高度的默认的 CSS 盒子模型.它有content-box.border-box和inherit三种取值.inherit指的是从父元素继承box-sizi ...
- HashMap底层实现原理(JDK1.8)源码分析
ref:https://blog.csdn.net/tuke_tuke/article/details/51588156 http://www.cnblogs.com/xiaolovewei/p/79 ...
- The open source JavaScript graphing library that powers Plotly
https://plot.ly/javascript/time-series/ https://plot.ly/javascript/ https://github.com/plotly/plotly ...