java生成excel文件以及设置字体,Excel基本数据类型,和合并单元格

来源:互联网 发布:淘宝助理申通快递模板 编辑:程序博客网 时间:2024/06/05 07:32

文章From : http://blog.sina.com.cn/s/blog_8d960c4c0101cd7n.html
import java.io.File;
import java.io.IOException;
import java.util.Date;
import jxl.Workbook;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.NumberFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.Number;
import jxl.write.Boolean;
public class Test {
//生成excel文件
public static void writeExcel() throws IOException{
try{
String Divpath = “d:\test”;//文件保存路径
File dirFile = new File(Divpath);
if(!dirFile.exists()){//文件路径不存在时,自动创建目录
dirFile.mkdir();
}
String path = Divpath+”\test.xls”;//文件名字
//创建一个可写入的excel文件对象
WritableWorkbook workbook = Workbook.createWorkbook(new File(path));
//使用第一张工作表,将其命名为“测试”
WritableSheet sheet = workbook.createSheet(“测试”, 0);

        //设置字体种类和格式         WritableFont bold = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD);         WritableCellFormat wcfFormat = new WritableCellFormat(bold);         wcfFormat.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中        //单元格是字符串格式!第一个是代表列数,第二是代表行数,第三个代表要写入的内容,第四个代表字体格式  (0代表excel的第一行或者第一列)            Label label01 = new Label(0, 0, "测试数据:",wcfFormat); //这里的(0,0)表示第一行第一列的表格                sheet.addCell(label01);         Label label02 = new Label(1, 0, "测试的结果是成功的");                     sheet.addCell(label02);        //合并单元格,合并既可以是横向的,也可以是纵向的            //这里的第一个数据代表第二列,第二个数据代表第一行,第三个数据代表第四列,第四个数据代表第二行      sheet.mergeCells(1, 0, 3, 1);      //设置第2行的高度         sheet.setRowView(1,400,false);                //设置列宽         sheet.setColumnView(0, 15);         sheet.setColumnView(1, 40);        //插入图片         File file=new File("d:\\test\\123.png");                    //WritableImage前面四个参数的类型都是double,依次是 x, y, width, height,这里的宽和高可不是图片的宽和高,而是图片所要占的单位格的个数         WritableImage image=new WritableImage(1, 3, 1, 3,file);         sheet.addImage(image);         //整型数据         Number label2 = new Number(0, 1,31415926);         sheet.addCell(label2);        //添加带有formatting的Number对象            NumberFormat nf = new NumberFormat("#.##");            WritableCellFormat wcfN = new WritableCellFormat(nf);            Number labelNF = new Number(0, 3, 3.1415926, wcfN);            sheet.addCell(labelNF);         //boolean型数据         Boolean label3 = new Boolean(0,4,true);         sheet.addCell(label3);         //添加DateTime对象            DateTime labelDT = new DateTime(0, 5, new Date());         sheet.addCell(labelDT);       //添加带有formatting的DateFormat对象         DateFormat df = new DateFormat("yyyy-MM-dd HH:mm:ss"); //HH是24小时制,hh是12小时制            WritableCellFormat wcfDF = new WritableCellFormat(df);            DateTime labelDTF = new DateTime(4, 1, new Date(), wcfDF);            sheet.addCell(labelDTF);            //关闭对象,释放资源            workbook.write();            workbook.close();}catch(Exception e){       e.printStackTrace();     }

}
public static void main(String[] args) {
try {
writeExcel();
} catch (IOException e) {
e.printStackTrace();
}
}
}