最近简单的弄了下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: