zoukankan      html  css  js  c++  java
  • 认识Excel并创建一个excel(网址:http://poi.apache.org/)

    需要导入的jar包:

    package com.huawei.excel;

    import java.io.FileOutputStream;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class TestExcel {

    /**
    * 一个excel
    *
    * excel版本
    * 97版本:指的是 可以用office 2007以前的版本打开
    * 07版本:指的是 只能由 office 2007以及以后的版本能打开
    *
    * 一个excel包括 一个工作簿 ,工作簿里面有 工作表 , 而工作簿里面有表格数据
    *
    */

    public static void main(String[] args) throws Exception {
    // Workbook wb = new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    FileOutputStream fileOut = new FileOutputStream("F:/workbook.xlsx");
    wb.write(fileOut);
    fileOut.close();
    }

    }

    package com.huawei.excel;

    import java.io.File;
    import java.io.FileOutputStream;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;

    public class TestExcel01 {

    public static void main(String[] args) throws Exception {

    /**
    * 首先 创建一个工作簿
    *
    * 再在工作簿里面去创建工作表
    *
    * 在工作表里面创建一行
    *
    * 在一行中创建 单元格
    */

    //创建97版本的 excel
    Workbook wb = new HSSFWorkbook();
    //创建一个Sheet 工作表
    Sheet sheet = wb.createSheet("Hello Sheet");
    //创建一行
    Row row = sheet.createRow(0);
    //创建一个单元格
    Cell cell = row.createCell(0);
    //设置单元格的内容
    cell.setCellValue("this is a first excel");

    FileOutputStream out = new FileOutputStream(new File("F:/first.xls"));
    wb.write(out);
    out.flush();
    out.close();
    wb.close();
    }
    }

    package com.huawei.excel;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Date;

    import org.apache.catalina.startup.SetContextPropertiesRule;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.CreationHelper;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

    public class TestExcel02 {

    private Workbook wb = null;
    private String path = "F:/test.xls";

    //在所有的Test方法执行之前调用
    @Before
    public void createWorkBook(){
    this.wb = new HSSFWorkbook();
    }



    //在所有的test方法执行之后调用
    @After
    public void writeWorkBook() throws Exception{
    FileOutputStream out = new FileOutputStream(new File(this.path));
    this.wb.write(out);
    out.flush();
    out.close();
    this.wb.close();
    }
    //测试生成第一个工作簿
    @Test
    public void createFisrtWorkBook(){
    this.path = "F:/first.xls";
    }
    //测试生成第一个 单元格
    @Test
    public void createCell(){
    this.path = "F:/test_cell.xls";
    Sheet sheet = this.wb.createSheet("First");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("this is a first");
    }
    //创建一个日期类型的单元格
    @Test
    public void createDateCell(){
    Sheet sheet = this.wb.createSheet("日期");
    //得到一个CreationHelper 帮助器
    CreationHelper helper = this.wb.getCreationHelper();
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    //创建单元格样式
    CellStyle style = this.wb.createCellStyle();
    //设置日期的格式话
    style.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

    cell.setCellValue(new Date());
    cell.setCellStyle(style);
    }
    @Test
    public void createWorkBookOfUsersInfo(){

    String[][] data = new String[][]{
    {
    "123456789",
    "李四",
    "lisi@lisi.com",
    "20",
    "男"
    },{
    "2",
    "李四2",
    "lisi2@lisi.com",
    "30",
    "女"
    },{
    "3",
    "李四3",
    "lisi3@lisi.com",
    "22",
    "男"
    },{
    "4",
    "李四4",
    "lisi4@lisi.com",
    "24",
    "男"
    },{
    "5",
    "李四5",
    "lisi5@lisi.com",
    "35",
    "女"
    }
    };

    String []headers = new String[]{"ID","用户名","邮箱","年龄","性别"};





    //创建工作表
    Sheet sheet = this.wb.createSheet("用户信息");

    sheet.setColumnWidth(0, 256*8);

    //创建title
    Row title = sheet.createRow(0);
    //创建单元格样式
    CellStyle tStyle = this.wb.createCellStyle();
    //设置水平居中
    tStyle.setAlignment(CellStyle.ALIGN_CENTER);
    tStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

    title.setHeight((short)(40*20));

    Cell tCell = title.createCell(0);
    tCell.setCellValue("用户信息表");
    //合并单元格
    sheet.addMergedRegion(new CellRangeAddress(0,0,0,headers.length-1));
    tCell.setCellStyle(tStyle);
    //设置表头
    Row header = sheet.createRow(1);
    for(int i=0;i<headers.length;i++){
    Cell cell = header.createCell(i);
    cell.setCellValue(headers[i]);
    }

    for(int i=0;i<data.length;i++){
    //创建行
    Row row = sheet.createRow(i+2);
    for(int j=0;j<data[i].length;j++){
    //创建单元格
    Cell cell = row.createCell(j);
    //设置数据
    cell.setCellValue(data[i][j]);
    }

    }

    this.path = "F:/users.xls";

    }
    }

  • 相关阅读:
    SQL Server Audit监控触发器状态
    SQL Server 数据变更时间戳(timestamp)在复制中的运用
    SQL Server 更改跟踪(Chang Tracking)监控表数据
    SQL Server 变更数据捕获(CDC)监控表数据
    SQL Server 事件通知(Event notifications)
    SQL Server 堆表行存储大小(Record Size)
    SQL Server DDL触发器运用
    SQL Server 默认跟踪(Default Trace)
    SQL Server 创建数据库邮件
    SQL Server 跨网段(跨机房)FTP复制
  • 原文地址:https://www.cnblogs.com/hwgok/p/5880125.html
Copyright © 2011-2022 走看看