java导出excel float_java 导出exceL

本文介绍了使用Java如何将各种类型的数据(包括float)写入Excel文件,详细展示了使用jxl库创建工作簿、设置单元格格式、写入数据及保存文件的过程。
摘要由CSDN通过智能技术生成

//经过一天努力终于完成自己excel导,收获最大的就是基础知识还是需要经常温习,最重要的一点事要敢于尝试,只要敢想敢做就有实现的可能!

public class ImportExportExcel {

/**

* 对象数据写入到excel

*/

public  boolean  writeExcel(List> List,String ExcelFileName,String workSheetName)

{

WritableWorkbook book=null;

try

{

String fileName = "D:/学校竞争力情况.xls";

if(new File(fileName).exists())

{

File path=new File(fileName);

//System.out.println("存在");

deleteAllFilesOfDir(path);

}

File file = new File(fileName);//根据文件名创建一个文件对象

//打开文件

book=Workbook.createWorkbook(file);

//参数零表示第一页

WritableSheet sheet=book.createSheet(workSheetName,0);

//设置单元格的文字格式

WritableFont wf = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,false,

UnderlineStyle.NO_UNDERLINE,Colour.BLUE); //设置字体大小以及颜色

WritableCellFormat wcf = new WritableCellFormat(wf); //设置字体格式

wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  //设置竖直居中

// wcf.setAlignment(Alignment.CENTER);

wcf.setWrap(true);

if(List!=null&&!List.isEmpty())

{

for(int i=0;i

{

sheet.setRowView(i, 500);

//表头

Map hs=new HashMap();

hs=(Map)List.get(i);

int m=0;

int t=0;

if(i==0)

{

for(Object keyval:hs.keySet())

{

jxl.write.Label label;

/* if (i==0)

{

//Label(列号,行号 ,内容 ) 添加excel  标题

label = new jxl.write.Label(t, 0, (String)keyval); //put the title in row1

sheet.addCell(label);

}*/

//获取值类型

Object param =hs.get(keyval);

if(param instanceof Integer)

{

int intvalue =((Integer)param).intValue();

//添加数字

jxl.write.Number number = new jxl.write.Number(m,1,intvalue); //put the

sheet.addCell(number);

//Label(列号,行号 ,内容 ) 添加excel  标题

label = new jxl.write.Label(m,0,(String)keyval); //put the title in row1

sheet.addCell(label);

sheet.setColumnView(m, 10);

}

else if(param instanceof String)

{

String s=(String) param;

//添加数字

//添加带有字型Formatting的对象

jxl.write.WritableFont wff= new jxl.write.WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,true);

jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wff);

jxl.write.Label labelCF = new jxl.write.Label(m,1,s,wcfF);

sheet.addCell(labelCF);

//Label(列号,行号 ,内容 ) 添加excel  标题

label = new jxl.write.Label(m,0,(String)keyval); //put the title in row1

sheet.addCell(label);

sheet.setColumnView(m, 10);

}

else if (param instanceof Double)

{

Double doubled =((Double) param).doubleValue();

//添加数字

jxl.write.Number number = new jxl.write.Number(m,1,doubled); //put the

sheet.addCell(number);

//Label(列号,行号 ,内容 ) 添加excel  标题

label = new jxl.write.Label(m,0,(String)keyval); //put the title in row1

sheet.addCell(label);

sheet.setColumnView(m, 10);

}

else if(param instanceof Float)

{

float f =((Float) param).floatValue();

//添加数字单精度浮点类型

jxl.write.Number number = new jxl.write.Number(m,1,f); //put the

sheet.addCell(number);

//Label(列号,行号 ,内容 ) 添加excel  标题

label = new jxl.write.Label(m,0,(String)keyval); //put the title in row1

sheet.addCell(label);

sheet.setColumnView(m, 10);

}

else if(param instanceof Long)

{

long l =((Long) param).longValue();

//添加数字 长整形

jxl.write.Number number = new jxl.write.Number(m,1,l); //put the

sheet.addCell(number);

//Label(列号,行号 ,内容 ) 添加excel  标题

label = new jxl.write.Label(m,0,(String)keyval); //put the title in row1

sheet.addCell(label);

sheet.setColumnView(m, 10);

}

else if(param instanceof Boolean)   //添加bool对象

{

boolean b =((Boolean) param).booleanValue();

//3.添加Boolean对象

jxl.write.Boolean labelB = new jxl.write.Boolean(m,1,b);

sheet.addCell(labelB);

//Label(列号,行号 ,内容 ) 添加excel  标题

label = new jxl.write.Label(m,0,(String)keyval); //put the title in row1

sheet.addCell(label);

sheet.setColumnView(m, 10);

}

else if(param instanceof Date)

{

Date d=(Date) param;

//4.添加DateTime对象

jxl.write.DateTime labelDT = new jxl.write.DateTime(m,1,new java.util.Date());

sheet.addCell(labelDT);

//Label(列号,行号 ,内容 ) 添加excel  标题

label = new jxl.write.Label(m,0,(String)keyval); //put the title in row1

sheet.addCell(label);

sheet.setColumnView(m, 10);

}

m++;

}

}

else

{

for(Object keyval:hs.keySet())

{

Object param =hs.get(keyval);

if(param instanceof Integer)

{

int intvalue =((Integer)param).intValue();

//添加数字

jxl.write.Number number = new jxl.write.Number(t,i,intvalue); //put the

sheet.addCell(number);

sheet.setColumnView(t, 10);

}

else if(param instanceof String)

{

String s=(String) param;

//添加数字

//添加带有字型Formatting的对象

jxl.write.WritableFont wff= new jxl.write.WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,true);

jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wff);

jxl.write.Label labelCF = new jxl.write.Label(t,i,s,wcfF);

sheet.addCell(labelCF);

sheet.setColumnView(t, 10);

}

else if (param instanceof Double)

{

Double doubled =((Double) param).doubleValue();

//添加数字

jxl.write.Number number = new jxl.write.Number(t,i,doubled); //put the

sheet.addCell(number);

sheet.setColumnView(t, 10);

}

else if(param instanceof Float)

{

float f =((Float) param).floatValue();

//添加数字单精度浮点类型

jxl.write.Number number = new jxl.write.Number(t,i,f); //put the

sheet.addCell(number);

sheet.setColumnView(t, 10);

}

else if(param instanceof Long)

{

long l =((Long) param).longValue();

//添加数字 长整形

jxl.write.Number number = new jxl.write.Number(t,i,l); //put the

sheet.addCell(number);

sheet.setColumnView(t, 10);

}

else if(param instanceof Boolean)   //添加bool对象

{

boolean b =((Boolean) param).booleanValue();

//3.添加Boolean对象

jxl.write.Boolean labelB = new jxl.write.Boolean(t,i,b);

sheet.addCell(labelB);

sheet.setColumnView(t, 10);

}

else if(param instanceof Date)

{

Date d=(Date) param;

//4.添加DateTime对象

jxl.write.DateTime labelDT = new jxl.write.DateTime(t,i,new java.util.Date());

sheet.addCell(labelDT);

sheet.setColumnView(t, 10);

}

t++;

}

}

i++;

}

}

//写入数据并关闭文件

book.write();

}

catch(Exception ex)

{

System.out.println(ex);

}

finally

{

if(book!=null)

{

try

{

book.close();

return true;

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

}

return false;

}

public static void deleteAllFilesOfDir(File path) {

if (!path.exists())

return;

if (path.isFile()) {

path.delete();

return;

}

File[] files = path.listFiles();

for (int i = 0; i < files.length; i++) {

deleteAllFilesOfDir(files[i]);

}

path.delete();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值