//经过一天努力终于完成自己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();
}