POI常用方法


点击(此处)折叠或打开

  1. public static void main(String[] args) throws IOException {
  2.         Workbook workbook = new HSSFWorkbook();
  3.         /*==========获取常用对象======begin===========
  4.         //读取文件
  5.         POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("C:/a.xls"));
  6.         //得到Excel工作簿对象
  7.         Workbook workbook = new HSSFWorkbook(fs);
  8.         //得到工作表对象
  9.         Sheet sheet = workbook.getSheetAt(0);
  10.         //得到工作表的行
  11.         Row row = sheet.getRow(0);
  12.         //得到指定行的单元格
  13.         Cell cell = row.getCell(0);
  14.         //得到单元格样式
  15.         CellType cellType = CellType.forInt(cell.getCellType());
  16.         =========获取常用对象=========end===========*/

  17.         /*=============建立常用对象=======begin=======
  18.         //创建工作簿
  19.         Workbook workbook = new HSSFWorkbook();
  20.         //创建工作表
  21.         Sheet sheet= workbook.createSheet("new Sheet");
  22.         //创建行
  23.         Row row = sheet.getRow(0);
  24.         //创建单元格样式
  25.         CellStyle cellStyle = workbook.createCellStyle();
  26.         //在指定行创建指定样式的单元格
  27.         row.createCell(0).setCellStyle(cellStyle);
  28.         //在指定行创建含有指定值的单元格
  29.         row.createCell(0).setCellValue(1);
  30.         ===============建立常用对象=======end========*/

  31.         /*============设置sheet名称和单元格内容======begin======
  32.         Workbook workbook = new HSSFWorkbook();
  33.         workbook.setSheetName(1, "第一张工作表");
  34.         Cell cell = workbook.createSheet().createRow(0).createCell(0);
  35.         cell.setCellValue("单元格内容");
  36.         ============设置sheet名称和单元格内容=======end======*/

  37.         /*============取得sheet的数量========begin====
  38.         Workbook workbook = new HSSFWorkbook();
  39.         workbook.getNumberOfSheets();
  40.         ============取得sheet的数量========end====*/

  41.         //根据index取得sheet对象
  42.         Sheet sheet = workbook.getSheetAt(0);
  43.         //取得有效的行数
  44.         int count = sheet.getLastRowNum();
  45.         //取得一行有效的单元格个数
  46.         Row row = sheet.getRow(0);
  47.         row.getLastCellNum();
  48.         //单元格类型的读写
  49.         Cell cell = row.getCell(0);
  50.         cell.setCellType(CellType.STRING);//设置单元格为String类型
  51.         cell.getNumericCellValue();//读取数值类型的单元格内容

  52.         //设置列宽 行高
  53.         sheet.setColumnWidth(100, 100);
  54.         row.setHeight((short)100);

  55.         //添加区域 合并单元格
  56.         sheet.addMergedRegion(new CellRangeAddress(0,3,0,0));
  57.         Row row1 = sheet.createRow(0);
  58.         row1.createCell(0).setCellValue("0");
  59.         row1.createCell(1).setCellValue("1");
  60.         row1.createCell(2).setCellValue("2");
  61.         row1.createCell(3).setCellValue("3");

  62.         //保存Excel文件
  63.         String path = "C:/a.xls";
  64.         FileOutputStream fileOutputStream = new FileOutputStream(path);
  65.         workbook.write(fileOutputStream);

  66.         //常用单元格边框格式
  67.         CellStyle style = workbook.createCellStyle();
  68.         style.setBorderTop(CellStyle.BORDER_DOTTED);//上边框
  69.         style.setBorderBottom(CellStyle.BORDER_DOTTED);//下边框
  70.         style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
  71.         style.setBorderRight(CellStyle.BORDER_DOTTED);//右边框

  72.         //设置字体
  73.         Font font = workbook.createFont();
  74.         font.setFontHeightInPoints((short)11);//设置字体
  75.         font.setBoldweight(Font.BOLDWEIGHT_NORMAL);//加粗

  76.         //设置位置
  77.         style.setFont(font);
  78.         style.setAlignment(CellStyle.ALIGN_CENTER);//左右居中
  79.         style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//上下居中
  80.         style.setRotation((short)10);//单元格内容的旋转的角度

  81.         //设置
  82.         DataFormat dataFormat = workbook.createDataFormat();
  83.         style.setDataFormat(dataFormat.getFormat("0.00%"));//设置单元格数据格式
  84.         cell.setCellFormula("");//给单元格设置公式

  85.         //插入图片
  86.         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  87.         BufferedImage bufferedImage = ImageIO.read(new File("a.jpg"));
  88.         ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);

  89.         //自定义颜色
  90.         Font font1 = workbook.createFont();
  91.         font1.setColor(HSSFColor.RED.index);
  92.         style.setFont(font1);
  93.         cell.setCellStyle(style);

  94.     }


点击(此处)折叠或打开

  1. /*=================根据单元不同属性返回字符串数值========begin============*/
  2.     public String getCellStringValue(Cell cell){
  3.         String cellValue = "";
  4.         switch (CellType.forInt(cell.getCellType())){
  5.             case _NONE:
  6.                 break;
  7.             case NUMERIC:
  8.                 cellValue = String.valueOf(cell.getNumericCellValue());
  9.                 break;
  10.             case STRING:
  11.                 cellValue = cell.getStringCellValue();
  12.                 if (cellValue.trim().equals("") || cellValue.trim().length() <= 0){
  13.                     cellValue = "";
  14.                 }
  15.                 break;
  16.             case FORMULA:
  17.                 cell.setCellType(CellType.NUMERIC);
  18.                 cellValue = String.valueOf(cell.getNumericCellValue());
  19.                 break;
  20.             case BLANK:
  21.                 cellValue = "";
  22.                 break;
  23.             case BOOLEAN:
  24.                 break;
  25.             case ERROR:
  26.                 break;
  27.         }
  28.         return cellValue;
  29.     }
  30.     /*=================根据单元不同属性返回字符串数值========end============*/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30046312/viewspace-2150663/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30046312/viewspace-2150663/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值