* 下载列表为excel(带图片)
* @param ex 实体类
* @param headers excel里的标题行
* @param dataset 列表数据
* * @param title 标题
* @param x 图片所在列
* @return 下载好的文件名
*/
public String downLoadExcelWithPicture(ExportExcel<T> ex,String[] headers,List<T> dataset,String title,int x)
{
//通过配置文件获取路径
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");long now=System.currentTimeMillis();
java.util.Random random=new java.util.Random();// 定义随机类
int result=random.nextInt(100);// 返回[0,10)集合中的整数,注意不包括10
//生成随机名称
String name=""+now+result;
Properties p = new Properties();
try{
p.load(inputStream);
} catch (IOException e1){
e1.printStackTrace();
}
//获取路径
String path=p.getProperty("DOWNLOAD_PATH");try{
//打开文件
WritableWorkbook book = Workbook.createWorkbook( new File( path+name+".xls" ));
//生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet(title,0);
//设定单元格高度与宽度 设定第一行高度500 设定第一列宽度30
sheet.setRowView(0, 500);
sheet.setColumnView(x, 30);
//在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
//以及单元格内容为test
//Label()方法三个参数 1:列数 2:行数 3:要写入的内容 4:样式(可选项)
//添加表格头
for(int i=0;i<headers.length;i++){
Label label = new Label(i,0,headers[i]);
//将定义好的单元格添加到工作表中
sheet.addCell(label);
}
//添加内容
//循环得到行数
for(int i=0;i<dataset.size();i++){
Object data= dataset.get(i);
int k=0;//初始列
//获取类的所有属性
Field[] field = data.getClass().getDeclaredFields();for (int j = 0; j < field.length; j++) {
// 获取属性的名字
String na = field[j].getName();
// 将属性的首字符大写,方便构造get,set方法
na = na.substring(0, 1).toUpperCase() + na.substring(1);
Method m = data.getClass().getMethod("get" + na);
String value = (String) m.invoke(data);
if(k==x){
String imgPath = path+value;
//将图片的格式转换成png(因为jxl目前只支持png格式)
if(imgPath.indexOf(".png")==-1 && value!=null){
String changePath=imgPath.substring(0,imgPath.indexOf("."))+".png";
boolean flag=narrowAndFormateTransfer(imgPath,changePath,400,400,"png");
if(flag){
imgPath=changePath;
}else{
imgPath=null;
}
}
File imgFile = new File(imgPath);
WritableImage image = new WritableImage(k,i+1,1,1,imgFile);
sheet.addImage(image);
sheet.setRowView(i+1, 2000);
k++;
}else{
Label label = new Label(k,i+1,value);
///将定义好的单元格添加到工作表中
sheet.addCell(label);
k++;
}
}
}
//写入数据并关闭文件
book.write();
book.close();
return name+".xls";
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/**
* 将图片格式转换
* @param srcPath
* @param destPath
* @param height
* @param width
* @param formate
* @return
*/
public boolean narrowAndFormateTransfer(String srcPath, String destPath, int height, int width, String formate) {
boolean flag = false;
try {
File file = new File(srcPath);
File destFile = new File(destPath);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdir();
}
BufferedImage src = ImageIO.read(file); // 读入文件
Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
flag = ImageIO.write(tag, formate, new FileOutputStream(destFile));// 输出到文件流
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}