Jxl导出Excel(跨行、设置列宽、设置行高、自动对齐、自定义背景等)
packagecom.exl.test;
importjava.awt.Color;
importjava.io.File;
importjxl.CellView;
importjxl.Workbook;
importjxl.format.Alignment;
importjxl.format.Colour;
importjxl.format.UnderlineStyle;
importjxl.write.Label;
importjxl.write.WritableCellFormat;
importjxl.write.WritableFont;
importjxl.write.WritableSheet;
importjxl.write.WritableWorkbook;
importcom.exl.utils.ColourUtil;
publicclassTest{
publicstaticvoidmain(String[]args)throwsException{
Stringtitle="报表测试";
String[]navTitle={"第一行","第二行","第三行","第四行","第五行","第六行","第七行","第八行"};
String[][]content={
{"1","2","第naionfdapfn三行","第四niaodnfoanfdas行","第noandfoasnjdf五行","第六sdfadsafas行","第afdadfasdfs七a行","第adfasfdasf八行"},
{"2","2","第三行","第四行","第五行","第六行","第七行","sssssssssss第八sss行"},
{"3","2","第三行","第四行","第五行","第六行","第七行","第八行sssssssssssss"},
{"4","2","第三行","第四行","第sssssssssssssss五行","第ssssssssssssssssssss六行","第七行","第八行sssssssss"},
{"5","2","第三行","第ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd四行","第五行","第六行","第七行","第八行"},
{"6","2","第三行","第四行","第五行","第六行","第七行","第八行"},
{"7","2","第三行","第四ddddddddddddddddddddddddddddddd行","第五行","第六行","第七行","第八行"},
{"8","2","第三行","第四行","第五行","第六行","第七行","第八行"},
{"9","2","第三行","第ddddddddddddddddddddddddddddddd四行","第五行","第六行","第七行","第八行"},
{"10","2","第三行","第四行","第五行","第六行","第七行","第八行"},
{"11","2","第三行","第四行","第五行","第六dddddddddddddd行","第七行","第八行"},
{"12","2","第三行","第四行","第五行","第六行","第七行","第八行"},
{"13","2","第三行","第四行","第五行","dddddddddddddddddddddd第六行","第七行","第八行"},
{"14","2","第三行","第四行","第五行","第dddddddddddddddddddddd六行","第七行","第八行"},
};
StringfilePath="D:\\DesignSource\\tempT";
StringfileName="NewProject.xls";
Filedir=newFile(filePath);
if(!dir.isDirectory()){
dir.mkdirs();
}
Filefile=newFile(filePath+"\\"+fileName);
WritableWorkbookworkbook=Workbook.createWorkbook(file);
WritableSheetsheet=workbook.createSheet("报表统计",0);//单元格
/**
*title
*/
Labellab=null;
WritableFontwf2=newWritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);//定义格式字体下划线斜体粗体颜色
WritableCellFormatwcfTitle=newWritableCellFormat(wf2);
wcfTitle.setBackground(jxl.format.Colour.IVORY);//象牙白
wcfTitle.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);//BorderLineStyle边框
//wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直对齐
wcfTitle.setAlignment(Alignment.CENTRE);//设置垂直对齐
CellViewnavCellView=newCellView();
navCellView.setAutosize(true);//设置自动大小
navCellView.setSize(18);
lab=newLabel(0,0,title,wcfTitle);//Label(col,row,str);
sheet.mergeCells(0,0,navTitle.length-1,0);
sheet.setColumnView(0,navCellView);//设置col显示样式
sheet.setRowView(0,1600,false);//设置行高
sheet.addCell(lab);
/**
*status
*/
/**
*nav
*/
jxl.write.WritableFontwfcNav=newjxl.write.WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
WritableCellFormatwcfN=newWritableCellFormat(wfcNav);
Colorcolor=Color.decode("#0099cc");//自定义的颜色
workbook.setColourRGB(Colour.ORANGE,color.getRed(),color.getGreen(),color.getBlue());
wcfN.setBackground(Colour.ORANGE);
wcfN.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);//BorderLineStyle边框
wcfN.setAlignment(Alignment.CENTRE);//设置水平对齐
wcfN.setWrap(false);//设置自动换行
for(inti=0;i
lab=newLabel(i,1,navTitle[i],wcfN);//Label(col,row,str);
sheet.addCell(lab);
sheet.setColumnView(i,newString(navTitle[i]).length());
}
/**
*内容
*/
jxl.write.WritableFontwfcontent=newjxl.write.WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
WritableCellFormatwcfcontent=newWritableCellFormat(wfcontent);
wcfcontent.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);//BorderLineStyle边框
wcfcontent.setAlignment(Alignment.CENTRE);
CellViewcellView=newCellView();
cellView.setAutosize(true);//设置自动大小
for(inti=0;i
for(intj=0;j
sheet.setColumnView(i,cellView);//根据内容自动设置列宽
lab=newLabel(j,i+2,content[i][j],wcfcontent);//Label(col,row,str);
sheet.addCell(lab);
//sheet.setColumnView(j,newString(content[i][j]).length());
}
}
workbook.write();
workbook.close();
}
}
本文转自/17learning/archive//10/17/405065.html
13418747879
38篇文章,6W+人气,0粉丝
java导出excel设置行高列宽_Jxl导出Excel(跨行 设置列宽 设置行高 自动对齐 自定义背景等)...