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

itext poi 学习之旅 (2)创建excel的更多相关文章

  1. itext poi 学习之旅 (3)读取数据库信息并由excel展现出来

    DBConnection.java 连接数据库操作 package com.zkbj.poi; import java.sql.Connection; import java.sql.DriverMa ...

  2. itext poi 学习之旅 (1)创建pdf

    从零开始学习itext 创建pdf 1.用到流进行创建的pdf import java.io.File; import java.io.FileOutputStream; import com.ite ...

  3. VSTO学习(三)——创建Excel解决方案

    四.创建Excel外接程序介绍完了Excel对象模型之后,我们就可以利用这些对象来对Excel文档进行操作了,下面就创建一个简单的Excel外接程序的.首先我们模拟一个需求,大多说软件在使用时都会弹出 ...

  4. dotnet Core学习之旅(三):创建项目

    [重要:文中所有外链不能确保永久有效]>创建解决方案 在VSCode上,可以使用来自开源力量的vscode扩展vscode-solution-explorer来增强VSCode对.NET项目的支 ...

  5. VSTO之旅系列(二):创建Excel解决方案

    原文:VSTO之旅系列(二):创建Excel解决方案 本专题概要 引言 创建VSTO项目 Excel对象模型 创建Excel外接程序 创建Excel文档级自定义项 小结 一.引言 也许很多朋友都没有听 ...

  6. Java Struts2 POI创建Excel文件并实现文件下载

    Java Struts2 POI创建Excel文件并实现文件下载2013-09-04 18:53 6059人阅读 评论(1) 收藏 举报 分类: Java EE(49) Struts(6) 版权声明: ...

  7. java使用poi创建excel文件

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

  8. Struts2使用POI创建Excel并下载

    本文将讲解在Struts2框架下如何使用POI创建Office Excel文档并实现下载功能. Apache POI ,操作微软文档的Java API,简单来说就是可以用来操作Office文档的API ...

  9. SSIS 学习之旅 FTP文件传输-脚本任务

    这一章主要讲解一下用脚本怎么把CSV文件抛送到FTP服务器上 设计:   通过Demon库的Users表数据生成CSV文件.   生成后的CSV文件抛送到FTP指定目录下. 控件的使用这里就不做详细讲 ...

随机推荐

  1. Canvas -画图 关键字

    颜色.样式和阴影 属性 描述 fillStyle 设置或返回用于填充绘画的颜色.渐变或模式 strokeStyle 设置或返回用于笔触的颜色.渐变或模式 shadowColor 设置或返回用于阴影的颜 ...

  2. Nginx源码研究三:Epoll在NGINX中的使用

    Web服务器在面对高并发的情况下,网络的IO一般选择IO复用,像apache选择的Select/poll.Nginx在linux 2.6后选择Epoll做网路IO,提高了WEB服务的并发能力. 在本章 ...

  3. java注解实例-反射生成sql

    定义描述用户表的注解: package dao; import java.lang.annotation.ElementType; import java.lang.annotation.Retent ...

  4. Ftp协议Socket实现

    原来用WebRequest来传输文件,被人鄙视了.就弄个Socket版的,支持Active,Passive模式. 带事件日志,有时间的人可以拿去做C#版的flashfxp. public class ...

  5. Java DES 测试

    package com.des.test; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyEx ...

  6. WIN7下关闭驱动数字签名检查的方法

    内容是转的,最后一步貌似没什么用处,水印是去不掉的,不过也无所谓,关键是驱动能用了,要不完全瞎了 实测win7 32位旗舰版可用   ================================= ...

  7. Changing a Service's Configuration

    Changing a Service's Configuration   A service configuration program uses the ChangeServiceConfig an ...

  8. Linux系统编程(31)—— socket编程之TCP详解

    TCP有源端口号和目的端口号,通讯的双方由IP地址和端口号标识.32位序号.32位确认序号.窗口大小稍后详细解释.4位首部长度和IP协议头类似,表示TCP协议头的长度,以4字节为单位,因此TCP协议头 ...

  9. 【转】listView中,checkBox的显示和隐藏

    原文网址:http://www.cnblogs.com/vicma/p/3460500.html 在listView中,每个item都有一个ChexBox,当显示的时候在listView外面设置一个按 ...

  10. [译] 新手和老手都将受益的JavaScript小技巧

    这篇文章会分享一些鲜为人知但却很强大的JavaScript技巧, 各个级别的JavaScript开发者都会从中受益.   1. 用数组的length属性清空数组   我们知道在JS中对象类型是按引用传 ...