zoukankan      html  css  js  c++  java
  • poi学习

    需要节点

     <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.17-beta1</version>
    </dependency>
     
    创建一个学生类,并录入简单的属性
    private Integer id;
    private String name;
    private Integer age;

    在测试类中编写代码:
    //插入单行数据
    @Test
    public void hssTmp() throws IOException { HSSFWorkbook wk=new HSSFWorkbook(); HSSFSheet sheet=wk.createSheet("学生表"); HSSFRow row=sheet.createRow(0); HSSFCell cell=row.createCell(0);//此时后面不能+".setCellValue("")",否则会报错 cell.setCellValue("学生成绩表"); sheet.addMergedRegion(new CellRangeAddress(0,0,0,2)); HSSFRow row1 =sheet.createRow(1); row1.createCell(0).setCellValue("学生编号"); row1.createCell(1).setCellValue("学生姓名"); row1.createCell(2).setCellValue("学生年龄"); HSSFRow row2 =sheet.createRow(2); row2.createCell(0).setCellValue("1"); row2.createCell(1).setCellValue("张三"); row2.createCell(2).setCellValue("20"); FileOutputStream outputStream=new FileOutputStream("E:\workbook.xls"); wk.write(outputStream); outputStream.flush(); }
    
    
    //将集合数据插入文件

    @Test
    public void outExcel() throws IOException { HSSFWorkbook wk=new HSSFWorkbook(); HSSFSheet sheet=wk.createSheet("学生表"); HSSFRow row =sheet.createRow(0); HSSFCell cell=row.createCell(0); cell.setCellValue("学生编号"); cell=row.createCell((short)1); cell.setCellValue("学生姓名"); cell=row.createCell((short)2); cell.setCellValue("学生年龄"); cell=row.createCell((short)3); List<Student> list=new ArrayList<Student>(); Student stu1=new Student(); stu1.setId(1); stu1.setName("张三"); stu1.setAge(20); Student stu2=new Student(); stu2.setId(2); stu2.setName("李四"); stu2.setAge(20); Student stu3=new Student(); stu3.setId(3); stu3.setName("王五"); stu3.setAge(20); for (short i=0; i<list.size();i++ ){ row=sheet.createRow(i+1); row.createCell(0).setCellValue(list.get(i).getId()); row.createCell(1).setCellValue(list.get(i).getId()); row.createCell(2).setCellValue(list.get(i).getId()); } }


  • 相关阅读:
    剑指offer[30]——连续子数组的最大和
    移动端数据爬取
    Scrapy框架基础应用和持久化存储
    爬虫的验证码处理,图片懒加载,selenium和 PhantomJS,requests模块的session,线程池
    爬虫之数据解析(bs4,Xpath)
    爬虫之jupyter的使用,requests模块,正则表达式
    git的分支,多人协作,标签管理
    git 的基础
    利用python操作excel表
    nginx基于uwsgi部署Django
  • 原文地址:https://www.cnblogs.com/hfddz/p/7444760.html
Copyright © 2011-2022 走看看