Ⅰ java poi怎么导入excel数据
package poi;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel001 {
public static void main(String[] args) {
readXml("D:/test.xlsx");
System.out.println("-------------");
readXml("d:/test2.xls");
}
public static void readXml(String fileName){
boolean isE2007 = false; //判断是否是excel2007格式
if(fileName.endsWith("xlsx"))
isE2007 = true;
try {
InputStream input = new FileInputStream(fileName); //建立输入流
Workbook wb = null;
//根据文件格式(2003或者2007)来初始化
if(isE2007)
wb = new XSSFWorkbook(input);
else
wb = new HSSFWorkbook(input);
Sheet sheet = wb.getSheetAt(0); //获得第一个表单
Iterator<Row> rows = sheet.rowIterator(); //获得第一个表单的迭代器
while (rows.hasNext()) {
Row row = rows.next(); //获得行数据
System.out.println("Row #" + row.getRowNum()); //获得行号从0开始
Iterator<Cell> cells = row.cellIterator(); //获得第一行的迭代器
while (cells.hasNext()) {
Cell cell = cells.next();
System.out.println("Cell #" + cell.getColumnIndex());
switch (cell.getCellType()) { //根据cell中的类型来输出数据
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println("unsuported sell type");
break;
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
参考自http://blog.csdn.net/shuwei003/article/details/6741649
Ⅱ java excel poi 怎么导入
1、下载poi相关jar,maven的集成如下:(把${poi.version}替换成你要的版本)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
2、根据poi相关api读取sheet、row、cell,获得excel的数据:
封装row的对象,即每一行数据为一个对象,每个cell为对象里的一个属性,
整个sheet的数据装进集合里;
3、处理数据,可以对数据进行验证或其他操作;
4、写数据库操作。
Ⅲ java用poi实现将数据库里面的数据导入已经存在的excel模板中。最好有实例参考,谢谢。
这是我之前写的用反射的将数据导入到excel中的类,具体实现,代码里有注释。不知道楼主持久层用的什么东东?如果是its,建议使用ibatis的rowhandler,导出部分的实现,和下面这个类也很类似,楼主自己改改吧,这样性能会高一些,尤其是在你处理的要写入文件的数据,比较多的情况下。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import cn.emag.framework.Util.LogName;
/**
* 导出成excel文件工具类
*/
public class Export2ExcelUtil
{
private static Logger log = Logger.getLogger(LogName.ERROR_LOG);
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
/**
* 生成excel文件存入服务器
* @param importdata 待导入excle文件的内容
* @param header excel”表头“部分内容
* @param attr 与”表头“顺序对应的importdata中的成员变量名,首字母大写
* @param fileName 导入到目标文件中,完整路径
*/
public static void export2exc(List<Object> importdata,String[] header,String[] attr, String fileName)
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
// 创建头文件
writeHeader(header,sheet,wb);
// 创建文件内容
int i=1;
HSSFRow row;
for(Object object:importdata)
{
row = sheet.createRow(i++);
writeBody(object, attr, row);
}
// 写入文件
OutputStream os = null;
try
{
os = new FileOutputStream(fileName);
wb.write(os);
}
catch (Exception e)
{
log.error("写入文件失败",e);
}
finally
{
try
{
os.close();
}
catch (IOException e)
{
log.error("写入文件失败",e);
}
}
}
/**
* 创建文件头
* @param header 头内容
* @param sheet
*/
public static void writeHeader(String[] header,HSSFSheet sheet,HSSFWorkbook wb)
{
HSSFRow row = sheet.createRow(0);
HSSFCellStyle cellstype = wb.createCellStyle();
cellstype = wb.createCellStyle();
cellstype.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cellstype.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellstype.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellstype.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellstype.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellstype.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellstype.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell;
HSSFRichTextString str;
int n = 0;
for (String head : header)
{
sheet.setColumnWidth(n, 4000);
cell = row.createCell(n++);
cell.setCellStyle(cellstype);
str = new HSSFRichTextString(head);
cell.setCellValue(str);
}
}
/**
* 写行内容
* @param object
* @param atrr
* @param row
* @throws Exception
*/
public static void writeBody(Object object,String[] attr,HSSFRow row)
{
int n = 0;
HSSFCell cell;
Method method;
HSSFRichTextString str;
String content=null;
for(String atr : attr)
{
cell = row.createCell(n++);
try
{
method = object.getClass().getMethod("get"+atr);
Object o = method.invoke(object);
if(null!=o)
{
if(o instanceof Date)
{
content=format.format(o);
}else
{
content = String.valueOf( method.invoke(object));
}
}else
{
content=null;
}
}
catch (SecurityException e)
{
log.error("excel写入单元格内容失败",e);
}
catch (IllegalArgumentException e)
{
log.error("excel写入单元格内容失败",e);
}
catch (NoSuchMethodException e)
{
log.error("excel写入单元格内容失败",e);
}
catch (IllegalAccessException e)
{
log.error("excel写入单元格内容失败",e);
}
catch (InvocationTargetException e)
{
log.error("excel写入单元格内容失败",e);
}
str = new HSSFRichTextString(content);
cell.setCellValue(str);
}
}
}
Ⅳ poi导入excel
方法/步骤
一,ExcelUtils工具类(也就是解析EXCEL文件,判断EXCEL的类型以及数据的类型)
importjava.io.IOException;
importjava.io.InputStream;
importjava.math.BigDecimal;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importorg.apache.poi.hssf.usermodel.HSSFDateUtil;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.ss.usermodel.Cell;
importorg.apache.poi.ss.usermodel.Row;
importorg.apache.poi.ss.usermodel.Sheet;
importorg.apache.poi.ss.usermodel.Workbook;
importorg.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
privatefinalstaticString excel2003L =“.xls”;// 2003-版本的excel
privatefinalstaticString excel2007U =“.xlsx”;// 2007 +版本的excel
/ **
*描述:获取IO流中的数据,组装成List <List <Object>对象
* @param in,fileName
* @返回
* @throws IOException
* /
publicList <List <Object> getBankListByExcel(InputStream in,String fileName)throwsException {
列表<List <Object> list =null;
//创建的Excel工作薄
Workbook work =this.getWorkbook(in,fileName);
if(null== work){
抛出新的异常(“创建Excel工作薄为空!”);
}
Sheet sheet =null;//页数
行row =null;//行数
Cell cell =null;//列数
list =newArrayList <List <Object >>();
//遍历的Excel中所有的片
for(inti =0; i <work.getNumberOfSheets(); i ++){
sheet = work.getSheetAt(i);
if(sheet ==null){continue;}
//遍历当前片中的所有行
for(intj = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j ++){
row = sheet.getRow(j);
if(row ==null|| row.getFirstCellNum()== j){continue;}
//遍历所有的列
列表<Object> li =newArrayList <Object>();
for(inty = row.getFirstCellNum(); y <row.getLastCellNum(); y ++){
cell = row.getCell(y);
li.add(this.getValue(cell));
}
list.add(LI);
}
}
return 单
}
/ **
*描述:根据文件后缀,自适应上传文件的版本
* @param inStr,fileName
* @返回
* @throws异常
* /
publicWorkbook getWorkbook(InputStream inStr,String fileName)throwsException {
工作簿wb =null;
String fileType = fileName.substring(fileName.lastIndexOf(“。”));
if(excel2003L.equals(fileType)){
wb =newHSSFWorkbook(inStr);// 2003-
}elseif(excel2007U.equals(fileType)){
wb =newXSSFWorkbook(inStr);// 2007 +
}else{
抛出新的异常(“解析的文件格式有误!”);
}
返回wb;
}
/ **
*描述:对表格中数值进行格式化
* @param单元格
* @返回
* /
//解决擅长类型问题,获得数值
publicString getValue(Cell cell){
String value =“”;
if(null== cell){
返回值
}
switch(cell.getCellType()){
//数值型
案例Cell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
//如果是date类型则,获取该单元格的日期值
Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
SimpleDateFormat format =newSimpleDateFormat(“yyyy-MM-dd”);
value = format.format(date);;
}else{//纯数字
BigDecimal big =newBigDecimal(cell.getNumericCellValue());
value = big.toString();
//解决1234.0去掉后面的.0
if(null!= value &&!“”.equals(value.trim())){
String [] item = value.split(“[。]”);
if(1<item.length &&“0”.equals(item [1])){
value = item [0];
}
}
}
break;
//字符串类型
案例Cell.CELL_TYPE_STRING:
value = cell.getStringCellValue()。toString();
break;
//公式类型
案例Cell.CELL_TYPE_FORMULA:
//读公式计算值
value = String.valueOf(cell.getNumericCellValue());
if(value.equals(“NaN”)){//如果获取的数据值为非法值,则转换为获取字符串
value = cell.getStringCellValue()。toString();
}
break;
//布尔类型
案例Cell.CELL_TYPE_BOOLEAN:
value =“”+ cell.getBooleanCellValue();
break;
默认值:
value = cell.getStringCellValue()。toString();
}
if(“null”.endsWith(value.trim())){
value =“”;
}
返回值
}
}
二,定义两个实体类,一个是对于的Excel文件,解析它的数据(ExcelBean),另一个是导入数据库表的实体类(人)
ExcelBean.java
<strong> <span style =“font-size:18px;”>importorg.apache.poi.xssf.usermodel.XSSFCellStyle;
公共类ExcelBean实现java.io.Serializable {
privateString headTextName;//列头(标题)名
privateString propertyName;//对应字段名
私有整数列;//合并单元格数
私人XSSFCellStyle cellStyle;
publicExcelBean(){
}
publicExcelBean(String headTextName,String propertyName){
这个.headTextName = headTextName;
这个.propertyName = propertyName;
}
publicExcelBean(String headTextName,String propertyName,Integer cols){
super();
这个.headTextName = headTextName;
这个.propertyName = propertyName;
这个.cols = cols;
}
publicString getHeadTextName(){
returnheadTextName;
}
publicvoidsetHeadTextName(String headTextName){
这个.headTextName = headTextName;
}
publicString getPropertyName(){
returnpropertyName;
}
publicvoidsetPropertyName(String propertyName){
这个.propertyName = propertyName;
}
publicInteger getCols(){
返回列;
}
公共无效setCols(Integer cols){
这个.cols = cols;
}
上市XSSFCellStyle getCellStyle(){
返回cellStyle;
}
公共无效setCellStyle(XSSFCellStyle cellStyle){
这个.cellStyle = cellStyle;
}
} </ span> </ strong>
people.java
importjava.util.Date;
公共课人
私有整数id
privateString userName;
私人字符串密码;
私人整数年龄;
私人日期;
publicInteger getId(){
返回id
}
publicvoidsetId(Integer id){
这个.id = id;
}
publicString getUserName(){
returnuserName;
}
publicvoidsetUserName(String userName){
这个.userName = userName ==null?null:userName.trim();
}
publicString getPassword(){
返回密码
}
publicvoidsetPassword(String password){
这个.password = password ==null?null:password.trim();
}
publicInteger getAge(){
回归年龄
}
publicvoidsetAge(Integer age){
这个.age = age
}
publicDate getDate(){
退货日期
}
publicvoidsetDate(Date date){
这个.date = date
}
}
Ⅳ poi如何去写入excel文件
直接全部在action里面写的,这个就不多说了,直接上代码:
public String executeExcel() throws Exception{ String realPath = ServletActionContext.getServletContext().getRealPath("/fileupload");
System.out.println(fileFileName);
String filePath = "";
if(this.file!=null){
File saveFile = new File(new File(realPath),this.fileFileName);
filePath = realPath+"\\"+this.fileFileName;
System.out.println(filePath);
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs(); }
FileUtils.File(file, saveFile); }
this.exlToDB(filePath);
ActionContext.getContext().put("message","导入成功");
return "success"; } //读取excel2007,并把数据插入数据库
public void exlToDB(String filePath){ boolean flag = true;
AllKpi akpi = new AllKpi(); try { // 文件流指向excel文件
FileInputStream fin=new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(fin);// 创建工作薄
XSSFSheet sheet = workbook.getSheetAt(0);// 得到工作表
XSSFRow row = null;// 对应excel的行
XSSFCell cell = null;// 对应excel的列
int totalRow = sheet.getLastRowNum();// 得到excel的总记录条数
System.out.println(totalRow); // 以下的字段一一对应数据库表的字段
float idd = 0.0f;
String id = "";
String Name = "";
String DEPT_NAME = "";
String Weight = "";
<span></span>String ALGORITHM = "";
String text = " ";
//String sql = "insert into DSP_TJ_KPI values(DSP_TJ_KPI_SEQ.nextval,?,?,?,'无',?)";
for (int i = 1; i <= totalRow; i++) {
row = sheet.getRow(i);
//System.out.println(row.getCell(0).toString());
if(row.getCell(0) != null && !"".equals(row.getCell(0)) && row.getCell(1) != null && !"".equals(row.getCell(1)) && row.getCell(2) != null && !"".equals(row.getCell(2)) && row.getCell(3) != null && !"".equals(row.getCell(3))){
cell = row.getCell((short) 0);
Name = cell.toString();
System.out.println(Name);
cell = row.getCell((short) 1);
Weight = cell.toString();
System.out.println(Weight);
cell = row.getCell((short) 2);
DEPT_NAME = cell.toString();
System.out.println(DEPT_NAME);
cell = row.getCell((short) 3);
ALGORITHM = cell.toString();
System.out.println(ALGORITHM);
akpi.setAllkpiName(Name);
akpi.setAllkpiDeptName(DEPT_NAME);
akpi.setAllkpiWeight(Weight);
akpi.setAlgorithm(ALGORITHM);
akpi.setText(text);
allKpiService.addAllKpi(akpi); //以下注释代码为连接jdbc测试代码块
/*pst = con.prepareStatement(sql);
//pst.setString(1, student_id);
pst.setString
(1, DEPT_NAME);
pst.setString
(2, Name);
pst.setString
(3, Weight);
<span></span>pst.setString(4, ALGORITHM);
pst.execute();*/
System.out.println("preparestatement successful"); } }
/*pst.close();
con.close();*/
fin.close(); } catch (FileNotFoundException e)
{
flag = false;
e.printStackTrace();
}
catch (IOException ex)
{
flag = false;
ex.printStackTrace();
}
Ⅵ poi导入excel时,excel不是标准的excel文件,导致报错,需要另存为下才可以导入,求解决办法
你可考虑过把JXL和POI配合起来使用?即:用JXL从非标准的EXCEL里读取数据然后用POI来另存为EXCEL,然后再用POI来使用它
Ⅶ 如何使用POI对Excel表进行导入和导出
导入POI的jar包
新建一个项目,在根目录在新建一个lib文件夹,将jar包复制粘贴到lib文件夹后,右键将其添加到项目的build path中,最后的结果如图所示:
2
编写java类,新建一个实体类,比如我们要导出数据库的有关电脑的信息,那么就建一个Computer实体类,代码如下:
package com.qiang.poi;
public class Computer {
private int id;
private String name;
private String description;
private double price;
private double credit;
public int getId() {
return id;
}
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 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;
}
}
3
新建一个写入excel的方法,如write2excel,参数可以后面边写边决定(站在一个不熟悉POI的角度)
public static void write2Excel(){}
4
创建操作Excel的HSSFWorkbook对象
HSSFWorkbook excel= new HSSFWorkbook();
创建HSSFSheet对象
Excel中的一个sheet(工作表)对应着java中的一个HSSFSheet对象,利用HSSFWorkbook对象可以创建一个HSSFSheet对象
如:创建一个sheet名为computer的excel
HSSFSheet sheet = excel.createSheet("computer");
创建第一行标题信息的HSSFRow对象
我们都知道excel是表格,即由一行一行组成的,那么这一行在java类中就是一个HSSFRow对象,我们通过HSSFSheet对象就可以创建HSSFRow对象
如:创建表格中的第一行(我们常用来做标题的行) HSSFRow firstRow = sheet.createRow(0); 注意下标从0开始
创建标题行中的HSSFCell数组
当然,excel中每一行是由若干个单元格,我们常称为cell,它对应着java中的HSSFCell对象
如:创建5个单元格 HSSFCell cells[] = new HSSFCell[5];
//假设我们一行有五列数据
创建标题数据,并通过HSSFCell对象的setCellValue()方法对每个单元格进行赋值
既然单元格都准备好了,那最后是不是该填充数据了呀。对的,没错。填充数据之前,得把数据准备好吧,
数据:String[] titles = new String[]{"id","name","description","price","credit"};
插入一句话: 在这个时代,能让机器做的,尽量不让人来做,记住这句话。
好的,继续。现在就通过for循环来填充第一行标题的数据
for (int i = 0; i < 5; i++) {
cells[0] = firstRow.createCell(i);
cells[0].setCellValue(titles[i]);
}
数据分析
第一行标题栏创建完毕后,就准备填充我们要写入的数据吧,在java中,面向对象给我们带来的好处在这里正好体现了,没错
把要填写的数据封装在对象中,即一行就是一个对象,n行就是一个对象列表嘛,好的,走起。
创建对象Computer,私有属性id,name,description,price,credit,以及各属性的setter和getter方法,如步骤二所示。
假设我们要写入excel中的数据从数据库查询出来的,最后就生成了一个List<Computer>对象computers
数据写入
具体数据有了,又该让机器帮我们干活了,向excel中写入数据。
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());
}
将数据真正的写入excel文件中
做到这里,数据都写好了,最后就是把HSSFWorkbook对象excel写入文件中了。
OutputStream out = null;
try {
out = new FileOutputStream(file);
excel.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("数据已经写入excel"); //温馨提示
看看我的main方法吧
public static void main(String[] args) throws IOException {
File file = new File("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);
}
工程目录及执行main方法后的test1.xls数据展示
源码分享,computer就不贴了
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("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();
}
}
}