这东西有什么用呢,作为一枚程序员,Excel对于我来说是没什么用的,但是想想,如果公司人力的妹纸每个月底都要一个一个的去核对一个同事有没有迟到,那是有多蛋疼的事,所以,将打卡的数据导入到Excel,然后遍历一下,什么麻烦事都没有了。然后人力妹纸就不用每次月底蛋疼地核对数据,哈哈,程序员太伟大了,而且事后还能被对方请吃饭,这也是当初论坛里为什么有一个人为公司写了一个方便大家的东西而满满的幸福感。
Java将数据处理导出到Excel的有方法有使用jxl和poi。本来靠着一手资料写了半篇博客,误入了一家也是java excel的英文网站,看了半天最后竟然差个中间件跑不了。
后面发现网上讲的jxl是韩国人写的,老外的一款产品叫JExcel。
jxl的jar和api下载:jexcelapi_2_6_12。
为什么要导出到Excel,因为数据不止是给自己看,还要给别人看,文员的话肯定喜欢Excel的形式。
要有个标题,有列名,还有数据。
Excel其实就是SpreadSheets,数据电子表格。一个表格也就是一个Workbook(工作簿)。
一个工作簿里面一般最多是255个sheet(表),单元格叫Cell。知道这几个英文代表了什么,再查查API,基本就可以上手了。
1、使用JXL。
接下来,简单封装标题名,列名称(字符数组),列数据(list)传进。
简单的User类:
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class UserList {
public List<User> getUserList(){
List<User> list = new ArrayList<User>();
User u = null;
for (int i = 0; i < 100; i++) {
u = new User();
u.setAge(i);
u.setId(i);
u.setName("id"+i);
list.add(u);
}
return list;
}
}
public class MyExcel {
public void exportExcel(String sheetname,String rowname[],String title,List list){
File file = new File("e:\\export.xls");
int length = rowname.length;
WritableSheet sheet = null;
try {
WritableWorkbook book = Workbook.createWorkbook(file);
sheet = book.createSheet(sheetname, 1);
//合并单元格
sheet.mergeCells(0, 0, length-1, 0);
//使标题居中 拿到表格列宽 减去标题长度
int all = sheet.getColumnView(0).getSize()*length;
int space = (all-title.length())/2;
String addSpace = "";
for (int i = 0; i < space; i++) {
addSpace +=" ";
}
Label l = new Label(0,0,addSpace+title);
sheet.addCell(l);
//列名添加
for (int i = 0; i < rowname.length; i++) {
Label temp = new Label(i,1,rowname[i]);
sheet.addCell(temp);
}
//列数据添加
for (int i = 2; i < list.size()+2; i++) {
User u = (User)list.get(i-2);
String id = String.valueOf(u.getId());
String name = u.getName();
String age = String.valueOf(u.getAge());
int j = 0;
Label temp = new Label(j,i,id);
Label temp2 = new Label(++j,i,name);
Label temp3 = new Label(++j,i,age);
sheet.addCell(temp);
sheet.addCell(temp2);
sheet.addCell(temp3);
}
book.write();
book.close();
} catch (IOException e) {
e.printStackTrace();
}catch (WriteException e) {
e.printStackTrace();
}
try {
Workbook in = Workbook.getWorkbook(file);
Sheet s = in.getSheet(0);
//获取api版本号
//System.out.println(in.getVersion());
//文件分隔符
//System.out.println(File.separator);
} catch (BiffException | IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String rowname[] = {"ID","年龄","性别"};
MyExcel excel = new MyExcel();
excel.exportExcel("myexcel",rowname,"后台数据 ",new UserList().getUserList());
}
}2、使用POI
POI是apache的一个项目,之前在StackOverFlow看到的是,jxl是不支持excel2007以及之后的版本的,不知道升级之后有没有改进,POI强大之处是不止Excel本身,还能处理MicroSoft的其他文档格式。
搞懂这些英文的缩写也比较容易:
POIFS - a pure Java implementation of the OLE 2 Compound Document format
HPSF - Java API to Handle Microsoft Format Document Properties
XSSF - Java API To Access Microsoft Excel Format Files
HSSF - the POI Project‘s pure Java implementation of the Excel ‘97(-2007) file format
HWPF- our port of the Microsoft Word 97 (-2003) file format to pure Java.
XWPF - for the WordprocessingML (2007+) format from the OOXML specification.
POI的文字提取:
String inputFile = "e://poj.doc"; FileInputStream fis = new FileInputStream(inputFile); POIFSFileSystem fileSystem = new POIFSFileSystem(fis); POIOLE2TextExtractor oleTextExtractor = ExtractorFactory.createExtractor(fileSystem); System.out.println(oleTextExtractor.getText());
常用的方法:
public class MyExcel {
public static void main(String[] args) {
// .xlsx格式的使用 XSSFWorkbook
Workbook book = new HSSFWorkbook();
try {
//使用file和inputstream的区别
//file类减少内存的消耗 而inputstream需要更多的内存因为需要缓存整个文件
FileOutputStream out = new FileOutputStream("e:\\hssf.xls");
//表名的设定 不像jxl 可以指定顺序 最开始的一个置于最前面
Sheet sheet = book.createSheet("a");
Sheet sheet2 = book.createSheet("b");
Sheet sheet3 = book.createSheet("c");
//选中表单
sheet3.setSelected(true);
//选中一个表插入一行 0为第一行
Row row = sheet.createRow(0);
//单元格插入,也不像jxl一样直接可以以二维数组这样的位置定位
Cell cell = row.createCell(0);
Cell cell2 = row.createCell(1);
//合并单元格 指从0到第五行 从0到第二列
sheet.addMergedRegion(new CellRangeAddress(0,5,0,2));
//这种合并利用了另外一种坐标合并
CellRangeAddress region = CellRangeAddress.valueOf("A1:E10");
sheet.addMergedRegion(region);
cell.setCellValue("cell1");
cell2.setCellValue(123.123);
Row row2 = sheet.createRow(3);
Cell cell3 = row2.createCell(6);
cell3.setCellValue("cell3");
//将0行到第四行的数据向下移动10行
sheet.shiftRows(0, 4, 10);
book.write(out);
out.close();
//相对的 遍历却方便了
HSSFWorkbook read= new HSSFWorkbook(new FileInputStream("e:\\hssf.xls"));
Sheet first = read.getSheetAt(0);
for(Row temp: first){
for(Cell c:temp){
System.out.println(c.getRowIndex());
System.out.println(c.getColumnIndex());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}原文:http://blog.csdn.net/iaiti/article/details/41960321