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读出需要的信息。

目录
相关文章
|
11月前
|
数据可视化 数据挖掘 大数据
1.1 学习Python操作Excel的必要性
学习Python操作Excel在当今数据驱动的商业环境中至关重要。Python能处理大规模数据集,突破Excel行数限制;提供丰富的库实现复杂数据分析和自动化任务,显著提高效率。掌握这项技能不仅能提升个人能力,还能为企业带来价值,减少人为错误,提高决策效率。推荐从基础语法、Excel操作库开始学习,逐步进阶到数据可视化和自动化报表系统。通过实际项目巩固知识,关注新技术,为职业发展奠定坚实基础。
excel 百分位函数 学习
excel 百分位函数 学习
529 1
|
前端开发 JavaScript Java
导出excel的两个方式:前端vue+XLSX 导出excel,vue+后端POI 导出excel,并进行分析、比较
这篇文章介绍了使用前端Vue框架结合XLSX库和后端结合Apache POI库导出Excel文件的两种方法,并对比分析了它们的优缺点。
2853 0
|
Java Apache
Apache POI java对excel表格进行操作(读、写) 有代码!!!
文章提供了使用Apache POI库在Java中创建和读取Excel文件的详细代码示例,包括写入数据到Excel和从Excel读取数据的方法。
1730 0
|
Java API Spring
集成EasyPoi(一个基于POI的Excel导入导出工具)到Spring Boot项目中
集成EasyPoi(一个基于POI的Excel导入导出工具)到Spring Boot项目中
1310 1
【POI】常用excel操作方法
【POI】常用excel操作方法
157 1
|
easyexcel Java API
Apache POI与easyExcel:Excel文件导入导出的技术深度分析
Apache POI与easyExcel:Excel文件导入导出的技术深度分析
|
Java Apache 索引
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
2056 0
|
5月前
|
Python
如何根据Excel某列数据为依据分成一个新的工作表
在处理Excel数据时,我们常需要根据列值将数据分到不同的工作表或文件中。本文通过Python和VBA两种方法实现该操作:使用Python的`pandas`库按年级拆分为多个文件,再通过VBA宏按班级生成新的工作表,帮助高效整理复杂数据。
|
5月前
|
数据采集 数据可视化 数据挖掘
用 Excel+Power Query 做电商数据分析:从 “每天加班整理数据” 到 “一键生成报表” 的配置教程
在电商运营中,数据是增长的关键驱动力。然而,传统的手工数据处理方式效率低下,耗费大量时间且易出错。本文介绍如何利用 Excel 中的 Power Query 工具,自动化完成电商数据的采集、清洗与分析,大幅提升数据处理效率。通过某美妆电商的实战案例,详细拆解从多平台数据整合到可视化报表生成的全流程,帮助电商从业者摆脱繁琐操作,聚焦业务增长,实现数据驱动的高效运营。

热门文章

最新文章