最近简单的弄了下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读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excelpoi读写excel
java利用poi对Excel进行读写操作支持多sheet格式代码可直接运行
poi读写excel,poi总结; poi读写excel,poi总结 poi读写excel,poi总结
POI读写excel文件+poi简单文档; POI读写excel文件+poi简单文档。
为更方便的使用POI的API来操作Excel(2003)文件,对POI中针对Excel文件的读写进行了简单封装。此类中包含以下功能: 1.根据模板创建Excel文件 2.获取及更新Excel文件内容 3.创建、复制Sheet 4.设置Sheet名称 ... ...
ExcelDemo_Excel导出_下载_POI读excel_JXL读写excel_java读写excel_列宽_读取合并或拆分单元格内容
poi读写excel表的实例,用java实现,在eclipse里直接运行
apache POI,文件读写 ,excel 对于使用apache poi 解析微软excel的一些文件
通过反射和相应的约定实现POI操作Excel的封装
POI读写excel(.xls/.xlsx)的Demo,直接import到myeclipse即可运行。 IDE:myeclipse 9.1 含jar包
poi3.9读写EXCEL 支持2007
java poi对于excel的读写与反射的结合使用,2个方法读2个方法写,是单独的源码文件,结合反射的方法读写各一个,还有个是list的2层集合excel读写方法,还包含使用到的简单函数封装
针对使用JAVA POI 4.0的Jar包进行了整理,提取了必要的jar整理打包LIb,并提供了 对Excel读写整理的可运行全工程。同类POI版本最高,最便宜的
POI读写海量Excel(代码)。整理版~
Java实现Excel读写的poi 5.2.1版本jar
POI工具包可用来读写Excel,在java开发中可用来创建、导出信息到excel表格中,还可读取excel中的信息使用到应用程序中。
使用poi读写微软的excel文件和WPS的et文件,poi3.1读写报表文件,此包是代码需要下载poi3.1jar
POI是java对Word和Excel 文件操作的插件,这文档主要是教我们怎么使用POI插件对Word和Excel文件进行读写操作
附件是java poi 3.17的lib库及其依赖库,全部下载引用后,可以进行excel的读写操作,无需引用其他lib库。poi-3.17适用于jdk1.7。 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft ...
1、POI_EXCEL包下分别有两个类,一个是读取excel内容,一个是想excel写入内容 2、cn.itcast包下的所有包,是为了实现从excel写入到数据库中,和从数据库写入到excel中 注意事项: 1、需要导入maven工程 2、使用...