首先网上搜索了一些文章,现在java操作excel比较多的使用jxl与POI。
前者比较轻便,更有针对性,但是功能不够强大。
有人已经使用代码测试过了,详情见:http://sunjun041640.blog.163.com/blog/static/256268322008525113505/
POI还没用过,先弄个jxl的简单操作Excel供大家参考。
package excelFile;
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.Boolean;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
public class TestJlxFuction {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
readExl("d:/test/wbook.xls");
creatExl("d:/test/writebook.xls");
modifyExl("d:/test/writebook.xls");
}
private static void creatExl(String fileName)
{
WritableWorkbook wwb = null;
try {
wwb = Workbook.createWorkbook(new File(fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WritableSheet ws = wwb.createSheet("第1页", 1);
/**
*下面的格式设置出自: http://javacrazyer.iteye.com/blog/723924
*/
//**************往工作表中添加数据*****************
//1.添加Label对象
Label label = new Label(0,0,"this is a label test");
try {
ws.addCell(label);
//添加带有字型Formatting对象
WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
WritableCellFormat wcf = new WritableCellFormat(wf);
Label labelcf = new Label(1,0,"this is a label test",wcf);
ws.addCell(labelcf);
//添加带有字体颜色的Formatting对象
WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,
UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);
ws.addCell(labelCF);
//2.添加Number对象
Number labelN = new Number(0,1,3.1415926);
ws.addCell(labelN);
//添加带有Formatting的Number对象
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfN = new WritableCellFormat(nf);
Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
ws.addCell(labelNF);
//3.添加Boolean对象
Boolean labelB = new jxl.write.Boolean(0,2,false);
ws.addCell(labelB);
//4.添加DateTime对象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
ws.addCell(labelDT);
//添加带有Formatting的DateFormat对象
DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat wcfDF = new WritableCellFormat(df);
DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);
ws.addCell(labelDTF);
//添加图片对象,jxl只支持png格式图片
File image = new File("f:\\2.png");
WritableImage wimage = new WritableImage(0,1,2,2,image);
ws.addImage(wimage);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
wwb.write();
try {
wwb.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 读取excel中的信息
* 通过获取row与column进行遍历
*/
private static void readExl(String fileName)
{
Workbook book = null;
Sheet [] sheets = null;
Sheet sheet0 = null;
try {
try {
book = Workbook.getWorkbook(new File(fileName));
} catch (BiffException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sheets = book.getSheets();
sheet0 = sheets[0];
/*
* 可以通过两层循环的方式遍历获取excel中的数据
*/
int rowNum = sheet0.getRows();
int colNum = sheet0.getColumns();
System.out.println("Rows :" + rowNum);
System.out.println("Columns :" + colNum);
for(int r = 0; r < rowNum; r++)
{
for(int c = 0; c < colNum; c++)
{
//注意getCell 参数1为列标,参数2二行标
Cell cell = sheet0.getCell(c, r);
String value = cell.getContents();
System.out.println(value);
}
}
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 更新Excel内容
* 采用copy的方式创建book进行操作
*/
public static void modifyExl(String fileName)
{
try
{
Workbook rwb = Workbook.getWorkbook(new File(fileName));
//打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook wwb = Workbook.createWorkbook(new File(fileName),rwb);//copy
WritableSheet ws = wwb.getSheet(0);
WritableCell wc = ws.getWritableCell(0,0);
//判断单元格的类型,做出相应的转换
if(wc.getType() == CellType.LABEL)
{
Label label = (Label)wc;
label.setString("The value has been modified");
}
wwb.write();
wwb.close();
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}