Apache POI(2):单元格(cell)

来源:互联网 发布:淘宝店铺设置在哪里 编辑:程序博客网 时间:2024/04/29 21:26
package com.hthk.iisz.util;import java.io.File;import java.io.FileOutputStream;import java.util.Date;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFCellStyle;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class CellTest {public static void main(String[] args) throws Exception {//typeOfCells();cellStyle();}// 创建一个单元格public static Cell createCell() {// create new workbookXSSFWorkbook workbook = new XSSFWorkbook();// create spreadsheet with a nameXSSFSheet sheet = workbook.createSheet("new sheet");// create first row on a created spreadsheetXSSFRow row = sheet.createRow(0);// create first cell on created rowXSSFCell cell = row.createCell(0);return cell;}// 单元格类型/* * 1 Blank cell value 2 Boolean cell value 3 Error cell value 4 Numeric cell * value 5 String cell value */public static void typeOfCells() throws Exception {XSSFWorkbook workbook = new XSSFWorkbook();XSSFSheet sheet = workbook.createSheet("cell types");XSSFRow row = sheet.createRow((short) 2);row.createCell(0).setCellValue("Type of Cell");row.createCell(1).setCellValue("cell value");row = sheet.createRow((short) 3);row.createCell(0).setCellValue("set cell type blank");row.createCell(1);row = sheet.createRow(4);row.createCell(0).setCellValue("set cell type boolean");row.createCell(1).setCellValue(true);row = sheet.createRow(5);row.createCell(0).setCellValue("set cell type error");row.createCell(1).setCellValue(XSSFCell.CELL_TYPE_ERROR);row = sheet.createRow(6);row.createCell(0).setCellValue("set cell type date");row.createCell(1).setCellValue(new Date());row = sheet.createRow(7);row.createCell(0).setCellValue("set cell type numeric");row.createCell(1).setCellValue(20);row = sheet.createRow(8);row.createCell(0).setCellValue("set cell type string");row.createCell(1).setCellValue("A String");FileOutputStream fos = new FileOutputStream(new File("typeOfCells.xlsx"));workbook.write(fos);fos.close();System.out.println("typesofcells.xlsx written successfully");}// 单元格样式public static void cellStyle() throws Exception {XSSFWorkbook workbook = new XSSFWorkbook();XSSFSheet spreadsheet = workbook.createSheet("cellstyle");XSSFRow row = spreadsheet.createRow((short) 1);row.setHeight((short) 800);XSSFCell cell = (XSSFCell) row.createCell((short) 1);cell.setCellValue("test of merging");// MEARGING CELLS// this statement for merging cellsspreadsheet.addMergedRegion(new CellRangeAddress(1, // first row// (0-based)1, // last row (0-based)1, // first column (0-based)4 // last column (0-based)));// CELL Alignmentrow = spreadsheet.createRow(5);cell = (XSSFCell) row.createCell(0);row.setHeight((short) 800);// Top Left alignmentXSSFCellStyle style1 = workbook.createCellStyle();spreadsheet.setColumnWidth(0, 8000);style1.setAlignment(XSSFCellStyle.ALIGN_LEFT);style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);cell.setCellValue("Top Left");cell.setCellStyle(style1);row = spreadsheet.createRow(6);cell = (XSSFCell) row.createCell(1);row.setHeight((short) 800);// Center Align Cell ContentsXSSFCellStyle style2 = workbook.createCellStyle();style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);cell.setCellValue("Center Aligned");cell.setCellStyle(style2);row = spreadsheet.createRow(7);cell = (XSSFCell) row.createCell(2);row.setHeight((short) 800);// Bottom Right alignmentXSSFCellStyle style3 = workbook.createCellStyle();style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);style3.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);cell.setCellValue("Bottom Right");cell.setCellStyle(style3);row = spreadsheet.createRow(8);cell = (XSSFCell) row.createCell(3);// Justified AlignmentXSSFCellStyle style4 = workbook.createCellStyle();style4.setAlignment(XSSFCellStyle.ALIGN_JUSTIFY);style4.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);cell.setCellValue("Contents are Justified in Alignment");cell.setCellStyle(style4);// CELL BORDERrow = spreadsheet.createRow((short) 10);row.setHeight((short) 800);cell = (XSSFCell) row.createCell((short) 1);cell.setCellValue("BORDER");XSSFCellStyle style5 = workbook.createCellStyle();style5.setBorderBottom(XSSFCellStyle.BORDER_THICK);style5.setBottomBorderColor(IndexedColors.BLUE.getIndex());style5.setBorderLeft(XSSFCellStyle.BORDER_DOUBLE);style5.setLeftBorderColor(IndexedColors.GREEN.getIndex());style5.setBorderRight(XSSFCellStyle.BORDER_HAIR);style5.setRightBorderColor(IndexedColors.RED.getIndex());style5.setBorderTop(XSSFCellStyle.BIG_SPOTS);style5.setTopBorderColor(IndexedColors.CORAL.getIndex());cell.setCellStyle(style5);// Fill Colors// background colorrow = spreadsheet.createRow((short) 10);cell = (XSSFCell) row.createCell((short) 1);XSSFCellStyle style6 = workbook.createCellStyle();style6.setFillBackgroundColor(HSSFColor.LEMON_CHIFFON.index);style6.setFillPattern(XSSFCellStyle.LESS_DOTS);style6.setAlignment(XSSFCellStyle.ALIGN_FILL);spreadsheet.setColumnWidth(1, 8000);cell.setCellValue("FILL BACKGROUNG/FILL PATTERN");cell.setCellStyle(style6);// Foreground colorrow = spreadsheet.createRow((short) 12);cell = (XSSFCell) row.createCell((short) 1);XSSFCellStyle style7 = workbook.createCellStyle();style7.setFillForegroundColor(HSSFColor.BLUE.index);style7.setFillPattern(XSSFCellStyle.LESS_DOTS);style7.setAlignment(XSSFCellStyle.ALIGN_FILL);cell.setCellValue("FILL FOREGROUND/FILL PATTERN");cell.setCellStyle(style7);FileOutputStream out = new FileOutputStream(new File("cellstyle.xlsx"));workbook.write(out);out.close();System.out.println("cellstyle.xlsx written successfully");}}

1 0
原创粉丝点击