码迷,mamicode.com
首页 > 其他好文 > 详细

POI操作excel03和excel07以上版本

时间:2016-04-23 18:25:05      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI 。jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel,个人认为已经是被淘汰的框架,而poi可以操作Excel 95及以后的版本,即可操作后缀为 .xls 和 .xlsx两种格式的excel。

POI全称 Poor Obfuscation Implementation,直译为“可怜的模糊实现”,当然并不可怜而且很实用,利用POI接口可以通过JAVA操作Microsoft office 套件工具的读写功能。官网:http://poi.apache.org ,POI支持office的所有版本,并且在接下来的演示中需要从前端页面导入用户上传的版本不确定的excel文件,所以选择POI来讲解。在官网,下载POI :对于只操作2003 及以前版本的excel,只需要 ,如果需要同时对2007及以后版本进行操作则需要复制

引入以下jar包:

poi-3.10.1-20140818.jar(若需求为03版本的,以下两个jar包不需要引入)

poi-ooxml-3.10.1-20140818.jar,

poi-ooxml-schemas-3.10.1-20140818.jar,以及复制在ooxml-lib目录下的xmlbeans-2.6.0.jar,dom4j-1.6.1.jar。

在POI包中有如下几个主要对象和excel的几个对象对应:

HSSFWorkbook

Excel 工作簿workbook

HSSFSheet

Excel 工作表 sheet

HSSFRow

Excel 行

HSSFCell

Excel 单元格

                       (07以上版本与之不同是使用的XSSFWorkbook,XSSFSheet,XSSFRow,XSSFCell)

附代码

  1 package cn.test;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.FileOutputStream;
  5 
  6 import org.apache.poi.hssf.usermodel.HSSFCell;
  7 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  8 import org.apache.poi.hssf.usermodel.HSSFFont;
  9 import org.apache.poi.hssf.usermodel.HSSFRow;
 10 import org.apache.poi.hssf.usermodel.HSSFSheet;
 11 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 12 import org.apache.poi.hssf.util.CellRangeAddress;
 13 import org.apache.poi.hssf.util.HSSFColor;
 14 import org.apache.poi.ss.usermodel.Cell;
 15 import org.apache.poi.ss.usermodel.Row;
 16 import org.apache.poi.ss.usermodel.Sheet;
 17 import org.apache.poi.ss.usermodel.Workbook;
 18 import org.apache.poi.xssf.usermodel.XSSFCell;
 19 import org.apache.poi.xssf.usermodel.XSSFRow;
 20 import org.apache.poi.xssf.usermodel.XSSFSheet;
 21 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 22 import org.junit.Test;
 23 
 24 public class TestPOI2Excel {
 25     /**
 26      * 写入03版本excel
 27      * @throws Exception
 28      */
 29     @Test
 30     public void testWrite03Excel() throws Exception {
 31         /**
 32          * 工作簿-工作表-行-单元格
 33          * 
 34          */
 35         //工作簿
 36         HSSFWorkbook workbook = new HSSFWorkbook();
 37         //工作表
 38         HSSFSheet sheet = workbook.createSheet("hello world");
 39         //行--index从0开始
 40         HSSFRow row = sheet.createRow(2);
 41         //创建单元格--index从0开始
 42         HSSFCell cell = row.createCell(2);
 43         cell.setCellValue("hello world");
 44         //输出到硬盘
 45         FileOutputStream fileOutputStream = new FileOutputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\测试.xls");
 46         //吧excel输入到具体的地址
 47         workbook.write(fileOutputStream);
 48         
 49         //关闭
 50         workbook.close();
 51         fileOutputStream.close();
 52         
 53     }
 54     
 55     /**
 56      * 读取03版本excel
 57      * @throws Exception
 58      */
 59     @Test
 60     public void testRead03Excel() throws Exception {
 61         /**
 62          * 工作簿-工作表-行-单元格
 63          * 
 64          */
 65         FileInputStream fileInputStream = new FileInputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\测试.xls");
 66         //读取工作簿
 67         HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
 68         //读取第一个工作表
 69         HSSFSheet sheet = workbook.getSheetAt(0);
 70         //读取第三行--index从0开始
 71         HSSFRow row = sheet.getRow(2);
 72         //读取单元格--index从0开始
 73         HSSFCell cell = row.getCell(2);
 74         System.out.println(cell.getStringCellValue());
 75         
 76         //关闭
 77         workbook.close();
 78         fileInputStream.close();
 79         
 80     }
 81     
 82     /**
 83      * 写入07版本excel
 84      * @throws Exception
 85      */
 86     @Test
 87     public void testWrite07Excel() throws Exception {
 88         /**
 89          * 工作簿-工作表-行-单元格
 90          * 
 91          */
 92         //工作簿
 93         XSSFWorkbook workbook = new XSSFWorkbook();
 94         //工作表
 95         XSSFSheet sheet = workbook.createSheet("hello world");
 96         //行--index从0开始
 97         XSSFRow row = sheet.createRow(2);
 98         //创建单元格--index从0开始
 99         XSSFCell cell = row.createCell(2);
100         cell.setCellValue("hello world");
101         //输出到硬盘
102         FileOutputStream fileOutputStream = new FileOutputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\test.xlsx");
103         //吧excel输入到具体的地址
104         workbook.write(fileOutputStream);
105         
106         //关闭
107         workbook.close();
108         fileOutputStream.close();
109         
110     }
111     
112     /**
113      * 读取07版本excel
114      * @throws Exception
115      */
116     @Test
117     public void testRead07Excel() throws Exception {
118         /**
119          * 工作簿-工作表-行-单元格
120          * 
121          */
122         FileInputStream fileInputStream = new FileInputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\test.xlsx");
123         //读取工作簿
124         XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
125         //读取第一个工作表
126         XSSFSheet sheet = workbook.getSheetAt(0);
127         //读取第三行--index从0开始
128         XSSFRow row = sheet.getRow(2);
129         //读取单元格--index从0开始
130         XSSFCell cell = row.getCell(2);
131         System.out.println(cell.getStringCellValue());
132         
133         //关闭
134         workbook.close();
135         fileInputStream.close();
136         
137     }
138     
139     /**
140      * 读取07版本excel
141      * @throws Exception
142      */
143     @Test
144     public void testRead03And07Excel() throws Exception {
145         /**
146          * 工作簿-工作表-行-单元格
147          * 
148          */
149         String fileName = "D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\test.xls";
150         if(fileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){//判断是否是excel
151             boolean is03Excel = fileName.matches("^.+\\.(?i)(xls)$");
152             FileInputStream fileInputStream = new FileInputStream(fileName);
153             
154             //读取工作簿
155             Workbook workbook = is03Excel?new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream);
156             //读取第一个工作表
157             Sheet sheet = workbook.getSheetAt(0);
158             //读取第三行--index从0开始
159             Row row = sheet.getRow(2);
160             //读取单元格--index从0开始
161             Cell cell = row.getCell(2);
162             System.out.println(cell.getStringCellValue());
163             
164             //关闭
165             workbook.close();
166             fileInputStream.close();
167         }
168     }
169     
170     /**
171      * 创建合并单元格
172      * @throws Exception
173      */
174     @Test
175     public void testExcelStyle() throws Exception {
176         /**
177          * 工作簿-工作表-行-单元格
178          * 
179          */
180         //工作簿
181         HSSFWorkbook workbook = new HSSFWorkbook();
182         
183         //1.1创建合并单元格对象合并第三行第三列到第五列
184         CellRangeAddress cellRangeAddress = new CellRangeAddress(2,2,2,4);//firstRow, lastRow, firstCol, lastCol
185         //1.2创建单元格样式        
186         HSSFCellStyle style = workbook.createCellStyle();
187         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
188         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
189         //1.3设置字体
190         HSSFFont font = workbook.createFont();
191         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
192         font.setFontHeightInPoints((short)16);
193         //加载字体
194         style.setFont(font);
195         //单元格背景
196         //style.setFillPattern(HSSFCellStyle.DIAMONDS);//填充模式
197         //style.setFillBackgroundColor(HSSFColor.YELLOW.index);//背景色
198         
199         //2、工作表
200         HSSFSheet sheet = workbook.createSheet("hello world");
201         
202         //2.1加载合并单单元格对象
203         sheet.addMergedRegion(cellRangeAddress);
204         
205         
206         //3.行--index从0开始
207         HSSFRow row = sheet.createRow(2);
208         //4.创建单元格--index从0开始
209         HSSFCell cell = row.createCell(2);
210         cell.setCellStyle(style);
211         cell.setCellValue("hello world");
212         //输出到硬盘
213         FileOutputStream fileOutputStream = new FileOutputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\test2.xls");
214         //吧excel输入到具体的地址
215         workbook.write(fileOutputStream);
216         
217         //关闭
218         workbook.close();
219         fileOutputStream.close();
220         
221     }
222 }

 

POI操作excel03和excel07以上版本

标签:

原文地址:http://www.cnblogs.com/GeekRegister/p/5425076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!