package com.qingruxu.excel;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Border;
import jxl.read.biff.BiffException;
import jxl.write.Blank;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableHyperlink;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class Excel {
public static void importExcel(String path)
{
try {
Workbook workbook=Workbook.getWorkbook(new File(path));
Sheet sh=workbook.getSheet(0);
for(int i=0;i<sh.getRows();i++)
{
System.out.println(sh.getCell(0, i).getContents());
}
workbook.close();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void outputExcel(String path)
{
try {
WritableWorkbook workbook =Workbook.createWorkbook(new File(path));
WritableSheet sh=workbook.createSheet("导出的数据", 0);
//字符串
Label name=new Label(0,0,"姓名");
sh.addCell(name);
//数字
Number num=new Number(1,0,1.25);
sh.addCell(num);
//图片 注意只可以是 png 的其它的可以使用PS转一下
WritableImage img=new WritableImage(5, 12, 4, 10,new File("d:/051123Webshots19.png"));
sh.addImage(img);
//超链接
WritableHyperlink link = new WritableHyperlink(2, 0,new URL("http://www.qingruxu.com"));
sh.addHyperlink(link);
//必写
workbook.write();
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
outputExcel("d:/test2.xls");
}
}
- 上一篇:jspsmart中文文件下载方法
- 下一篇:java当中最有效率的遍历List