• Java 操作Excel 之Poi(第一讲)


    1、Poi 简介

      Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。HSSF - 提供读写Microsoft Excel格式档案的功能。

    2、创建新工作簿

     1 package com.java1234.poi;
     2 
     3 import java.io.FileOutputStream;
     4 
     5 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     6 import org.apache.poi.ss.usermodel.Workbook;
     7 
     8 public class Demo1 {
     9 
    10     public static void main(String[] args) throws Exception {
    11         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
    12         FileOutputStream fileOut=new FileOutputStream("c:\用Poi搞出来的工作簿.xls");
    13         wb.write(fileOut);
    14         fileOut.close();
    15     }
    16 }

    3、创建新Sheet页

     1 package com.wcy.poi;
     2 
     3 import java.io.FileOutputStream;
     4 
     5 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     6 import org.apache.poi.ss.usermodel.Workbook;
     7 
     8 public class Demo02 {
     9 
    10     public static void main(String[] args) throws Exception{
    11         Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
    12         wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
    13         wb.createSheet("第二个Sheet页"); // 创建第二个Sheet页
    14         FileOutputStream fileOutputStream = new FileOutputStream("e:\创建Sheet页.xls");
    15         wb.write(fileOutputStream);
    16         fileOutputStream.close();
    17         
    18     }
    19 }

    4、创建单元格

     1 package com.java1234.poi;
     2 
     3 import java.io.FileOutputStream;
     4 
     5 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     6 import org.apache.poi.ss.usermodel.Cell;
     7 import org.apache.poi.ss.usermodel.Row;
     8 import org.apache.poi.ss.usermodel.Sheet;
     9 import org.apache.poi.ss.usermodel.Workbook;
    10 
    11 public class Demo3 {
    12 
    13     public static void main(String[] args) throws Exception{
    14         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
    15         Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
    16         Row row=sheet.createRow(0); // 创建一个行
    17         Cell cell=row.createCell(0); // 创建一个单元格  第1列
    18         cell.setCellValue(1);  // 给单元格设置值
    19         
    20         row.createCell(1).setCellValue(1.2);   // 创建一个单元格 第2列 值是1.2
    21         
    22         row.createCell(2).setCellValue("这是一个字符串类型"); // 创建一个单元格 第3列 值为一个字符串
    23         
    24         row.createCell(3).setCellValue(false);  // 创建一个单元格 第4列 值为布尔类型
    25         
    26         FileOutputStream fileOut=new FileOutputStream("c:\用Poi搞出来的Cell.xls");
    27         wb.write(fileOut);
    28         fileOut.close();
    29     }
    30 }

     声明:此程序代码本人只是用于学习总结,非原创,如有侵权,联系本人。

  • 相关阅读:
    从netty源码里拿到的关于http错误码,自己学习下
    9步搞定:用迅雷等工具下载百度网盘资源
    jstack定位cpu高占用
    solr学习笔记section2-solr单机(节点)简单的core操作
    solr学习笔记section1-在tomcat中部署单(节点)机solr5.5.4
    简单排序
    Thrift生成的bean对象,用java内省操作时注意(自己笔记)
    Netty方法误解ChannelHandlerContext.writeAndFlush(Object msg)
    腾讯笔试题,木棍组成多边形判断
    微软笔试题,luckstring
  • 原文地址:https://www.cnblogs.com/wangchaoyuan/p/5957823.html
Copyright © 2020-2023  润新知