源码
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* 利用JXL包读写Excel案例
*
* @author zhaochao
*
*/
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
writeExcel();
ReadExcel();
}
/**
* 写Excel
*
*/
private static void writeExcel() {
try {
WritableWorkbook book = Workbook.createWorkbook(new File("C:\\test.xls"));
// 生成名为"第一页"的工作表,参数0表示这是第一页
WritableSheet sheet1 = book.createSheet("第一页", 0);
WritableSheet sheet2 = book.createSheet("第二页", 1);
// 在label对象的构造方法中指名单元格位置是第一列,第一行(0,0)
// 以及单元格内容为test
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
Label label = new Label(i, j, "("+i+","+j+")");
// 将定义好的单元格添加到工作表中
sheet1.addCell(label);
}
}
for(int i=10;i<20;i++){
for(int j=10;j<20;j++){
Label label = new Label(i, j, "("+i+","+j+")");
// 将定义好的单元格添加到工作表中
sheet2.addCell(label);
}
}
// 写入数据并关闭文件
book.write();
book.close();
System.out.println("生成excel文件成功");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* 读Excel
*
*/
private static void ReadExcel() {
try {
InputStream is = new FileInputStream("C:\\test.xls") ;
Workbook wb = Workbook.getWorkbook(is);
jxl.Sheet [] sheet =wb.getSheets();
for(int s=0;s<sheet.length;s++){
for(int i=0;i<sheet[s].getRows();i++){
for(int j=0;j<sheet[s].getColumns();j++){
System.out.print(sheet[s].getCell(i, j).getContents()+" ");
}
System.out.println();
}
System.out.println();
}
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
结果
下载地址
JXL jar包及API文档下载地址