itext poi 学习之旅 (2)创建excel

简介: Computer.javapackage com.qiang.poi;public class Computer { private int id;private String name; priva...
Computer.java
package com.qiang.poi;

public class Computer {

 private int id;


private String name;

 private String description;

 private double price;

 private double credit;
 
 public Computer(int id, String name, String description, double price,
         double credit) {
     super();
     this.id = id;
     this.name = name;
     this.description = description;
     this.price = price;
     this.credit = credit;
 }
 
 public void setId(int id) {

  this.id = id;

 }
 
 
 public int getId() {
     
     return id;
     
 }

 public String getName() {

  return name;

 }

 public void setName(String name) {

  this.name = name;

 }

 public String getDescription() {

  return description;

 }

 public void setDescription(String description) {

  this.description = description;

 }

 public double getPrice() {

  return price;

 }

 public void setPrice(double price) {

  this.price = price;

 }

 public double getCredit() {

  return credit;

 }

 public void setCredit(double credit) {

  this.credit = credit;

 }

}

ReadExcel.java

package com.qiang.poi;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ReadExcel {

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

  File file = new File("D:/test1.xls");

  if(!file.exists()){

   file.createNewFile();

  }

  List<Computer> computers = new ArrayList<Computer>();

  computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));

  computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));

  computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));

  computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));

  computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));

  write2excel(computers, file);

 }

 

 public static void write2excel(List<Computer> computers,File file) {

  HSSFWorkbook excel = new HSSFWorkbook();

  HSSFSheet sheet = excel.createSheet("computer");

  HSSFRow firstRow = sheet.createRow(0);

  HSSFCell cells[] = new HSSFCell[5];

  String[] titles = new String[] { "id", "name", "description", "price",

    "credit" };

  for (int i = 0; i < 5; i++) {

   cells[0] = firstRow.createCell(i);

   cells[0].setCellValue(titles[i]);

  }

  for (int i = 0; i < computers.size(); i++) {

   HSSFRow row = sheet.createRow(i + 1);

   Computer computer = computers.get(i);

   HSSFCell cell = row.createCell(0);

   cell.setCellValue(computer.getId());

   cell = row.createCell(1);

   cell.setCellValue(computer.getName());

   cell = row.createCell(2);

   cell.setCellValue(computer.getDescription());

   cell = row.createCell(3);

   cell.setCellValue(computer.getPrice());

   cell = row.createCell(4);

   cell.setCellValue(computer.getCredit());

  }

        OutputStream out = null;

        try {

            out = new FileOutputStream(file);

            excel.write(out);

            out.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

 }

}

引入poi 文件就可以进行操作。能够从D:/test1.xls读出需要的信息。

目录
相关文章
|
1月前
|
easyexcel Java 测试技术
读取Excel还用POI?试试这款开源工具EasyExcel
读取Excel还用POI?试试这款开源工具EasyExcel
55 0
|
2月前
|
API
Poi 中文API文档 「40种操作 Excel文件的姿势」
Poi 中文API文档 「40种操作 Excel文件的姿势」
116 0
|
2月前
|
Java
POI上传excel的java后台逻辑
POI上传excel的java后台逻辑
|
2月前
|
Java
使用POI导出Excel
使用POI导出Excel
|
3月前
|
缓存 Java 关系型数据库
Java使用POI操作Excel
Java使用POI操作Excel
34 0
|
3月前
|
Python
Python办公自动化【Excel数据拆分-xlrd、Excel读取数据-openpyxl、Excel写入数据-openpyxl】(二)-全面详解(学习总结---从入门到深化)
Python办公自动化【Excel数据拆分-xlrd、Excel读取数据-openpyxl、Excel写入数据-openpyxl】(二)-全面详解(学习总结---从入门到深化)
36 0
|
3月前
|
Python
Python办公自动化【Excel写入数据-xlwt、Excel读取数据-xlrd、Excel更新数据-xlutils、Excel设置样式】(一)-全面详解(学习总结---从入门到深化)
Python办公自动化【Excel写入数据-xlwt、Excel读取数据-xlrd、Excel更新数据-xlutils、Excel设置样式】(一)-全面详解(学习总结---从入门到深化)
34 0
|
3月前
|
存储 easyexcel Java
SpringBoot整合Easyexcel操作Excel,闲暇之余,让我们学习更多
SpringBoot整合Easyexcel操作Excel,闲暇之余,让我们学习更多
105 0
|
4月前
|
XML 存储 Java
Apache POI 实现用Java操作Excel完成读写操作
Apache POI 实现用Java操作Excel完成读写操作
|
22天前
|
SQL 缓存 easyexcel
面试官问10W 行级别数据的 Excel 导入如何10秒处理
面试官问10W 行级别数据的 Excel 导入如何10秒处理
51 0