准备工作:
1,创建Java项目或者Java web项目
2,导入jxl.jar
3,创建类
接下来上代码:
package excel;
import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class JxlExcel2 {
public static void main(String[] args) {
String[] title = {"编号","姓名","性别","头像"};//用字符串数组存放标题
//要插入的图片
File imgFile = new File("d:\\Azzan.png");//自定义盘符中的文件
//创建Excel文件
File file = new File("d:/Azzan_test2.xls");
try {
//创建文件
file.createNewFile();
//创建工作簿
WritableWorkbook workbook = Workbook.createWorkbook(file);
//创建sheet页
WritableSheet sheet = workbook.createSheet("sheet1", 0);//sheet1名字,0索引的值
//写入标题
Label label = null;
for (int i = 0; i < title.length; i++) {
label = new Label(i,0,title[i]);
sheet.addCell(label);
}
//追加数据
for (int i = 1; i <= 10; i++) {
label = new Label(0,i,"a"+i);
sheet.addCell(label);
label = new Label(1,i,"user"+i);
sheet.addCell(label);
label = new Label(2,i,"男");
sheet.addCell(label);
//WritableImage(col, row, width, height, imgFile);
// 列 行 宽 高
WritableImage image = new WritableImage(3,i,1, 1, imgFile);
sheet.addImage(image);
}
workbook.write();//流的写入
workbook.close();//流的关闭
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:
jxl无行列概念,因此通过坐标来定位要操作的位置.