星空网 > 软件开发 > Java

POI操作excel03和excel07以上版本

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-oo

poi-oo

在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版本excel114    * @throws Exception115   */116   @Test117   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版本excel141    * @throws Exception142   */143   @Test144   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))$")){//判断是否是excel151       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 Exception173   */174   @Test175   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, lastCol185     //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以上版本

关键词:excel

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

对美排除申请:https://www.goluckyvip.com/tag/70082.html
怎么申请美团送外卖:https://www.goluckyvip.com/tag/70087.html
送美团外卖怎么申请:https://www.goluckyvip.com/tag/70088.html
美团怎么申请送外卖:https://www.goluckyvip.com/tag/70089.html
TACoS上升:https://www.goluckyvip.com/tag/7009.html
怎么开通美团外卖骑手:https://www.goluckyvip.com/tag/70090.html
深度解析:美国公司股东人数限制 :https://www.kjdsnews.com/a/1840835.html
深度解析:美国公司股东人数限制 :https://www.xlkjsw.com/news/88201.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流