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. phpcms v9 表单向导里添加图片字段,提示会话过期,请重新登陆

    用 phpcms v9 制作前端用户提交信息时,有个图片上传的栏目. 如果注释 /phpcms/modules/attachment/attachment.php 20//判断是否登录 21 if(e ...

  2. Java泛型的基本应用

    一.泛型概述 jdk1.5版本以后出现的新特性,用于解决安全问题,是一个安全机制. 好处: 1,将运行时期的问题ClassCastException转到了编译时期. 2,避免了强制转换的麻烦. 什么时 ...

  3. Dom4j修改xml文档

    1. 写出内容到xml文档 XMLWriter writer = new XMLWriter(OutputStream, OutputForamt) wirter.write(Document); 2 ...

  4. JS--图片轮播效果

    搞了很长时间才弄清楚图片轮播效果的原理,理解各个事件发生的原因,浪费了这么长的时间,只怪自己的知识太过于薄弱.现将代码写下,供大家参看,如有不妥之处还望指出,大家一起学习. 功能: 1.点击左右两边的 ...

  5. [POJ] 2226 Muddy Fields(二分图最小点覆盖)

    题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...

  6. MSP430 中断优先级

    MSP430的中断优先级.打开关闭.中断嵌套 优先级顺序从高到低为:    PORT2_VECTOR (1 * 2u) /* 0xFFE2 Port 2 */    PORT1_VECTOR (4 * ...

  7. 用Doxygen生成文档

    我是生成C/C++的文档,输出html格式的文档.就不做成CHM了. 注释要这种写:(当然,有数种注释风格,选择任意你喜欢的就行) /** * * 一系列的doxygen的 command * * * ...

  8. android 让一个控件按钮居于底部的几种方法

    android 让一个控件按钮居于底部的几种方法1.采用linearlayout布局:android:layout_height="0dp" <!-- 这里不能设置fill_ ...

  9. css属性之appearance

    appearance 属性允许您使元素看上去像标准的用户界面元素. 案例: 使 div 元素看上去像一个按钮 <!DOCTYPE html> <html> <head&g ...

  10. FFT修正

    #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #inc ...