jxl实现excel文件变成java集合

package cn.lmj.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;


public class GenerateSqlFromExcel
{
public static  List<String[]> excelToList(File file)
{
InputStream is = null;
Workbook wb = null;
List<String[]> list = new ArrayList<String[]>();

try
{
if(file == null)
{
throw new RuntimeException("文件为空");
}
is = new FileInputStream(file);
wb = Workbook.getWorkbook(is);

Sheet[] sheet = wb.getSheets();
if(sheet != null)
{
for(int i = 0;i<sheet.length;i++)
{
if(!sheet[i].getName().equalsIgnoreCase("Page1"))
{
throw new RuntimeException("指定文件中不包含名称为Page的sheet,请重新指定!");
}
for(int j = 1;j<sheet[i].getRows();j++)
{
String[] val = new String[5];
for(int k = 0;k<sheet[i].getColumns();k++)
{
Cell cell = sheet[i].getCell(k,j);
String content = "";
if(cell.getType() == CellType.DATE)
{
DateCell dateCell = (DateCell)cell;
Date date = dateCell.getDate();
long eighthour = 8*60*60*1000;
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long time = date.getTime() - eighthour;
Date d = new Date();
date.setTime(time);
content = simpledate.format(d);
}
else
{
String tempcontent = (cell.getContents() == null ? "" : cell.getContents());
content = tempcontent.trim().replace('\'',' ');
}
val[k] = content;
}
list.add(val);
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(is != null)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if(wb != null)
{
wb.close();
}
}

return list;
}

public static void main(String[] args)
{
File file = new File("C:\\Users\\Administrator\\Desktop\\POIService01.xls");
List<String[]> list = excelToList(file);
for(String[] str : list)
{
System.out.print(str[0]+",");
System.out.print(str[1]+",");
System.out.print(str[2]+",");
System.out.print(str[3]+",");
System.out.print(str[4]+",");
System.out.println();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值