(本人下的是jexcelapi_2_6_12.tar.gz,解压后将里面的jxl.jar复制到WEB-INF/lib目录下面即可)
Java Excel API的jar包可以通过以下URL获得:(推荐)
一、JExcel介绍
JExcel是Java对Excel进行操作的包,可以实现创建一个Excel并写入或读取Excel的数据等操作;
JExcel的主要类为:
(1)Workbook:工作簿
(2)WritableWorkbook:可写工作簿
(3)Sheet:表单;
(4)WritableSheet:可写表单;
(5)Label:单元格;
二、Java写入Excel
写入test.xls数据代码:
1 importjava.io.File;2
3 importjxl.Sheet;4 importjxl.Workbook;5 importjxl.write.Label;6 importjxl.write.WritableSheet;7 importjxl.write.WritableWorkbook;8
9 public classCreateTest {10
11 public static void main(String[] args) throwsException{12 String str[][] = {{"xiazdong","12345"},{"xzdong","54321"}};13 File f = new File("test.xls");14 WritableWorkbook workbook =Workbook.createWorkbook(f);15 WritableSheet sheet = workbook.createSheet("sheet1", 0);16 Label lab = null;17 for(int i=0;i
20 sheet.addCell(lab);21 }22 }23 workbook.write();24 workbook.close();25 }26
27 }
注意:Label中构造函数的参数,第一个参数是column,第二个参数是row,因此如果要插入第一行第二列,则(1,0);
总结:WritableWorkbook和WritableSheet都用于写入Excel文件;读取通过Workbook和Sheet执行;
三、读取Excel数据
读取test.xls中数据并显示代码:
1 importjava.io.File;2
3 importjxl.Sheet;4 importjxl.Workbook;5
6 public classCreateTest {7
8 public static void main(String[] args) throwsException{9 Workbook workbook = Workbook.getWorkbook(new File("test.xls"));10 Sheet sheet[] =workbook.getSheets();11 String lab = null;12 for(int a=0;a
22 }23
24 }