2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > jxl 创建表格(包括去掉网格线 字体设置 单元格设置 对齐方式等设置)

jxl 创建表格(包括去掉网格线 字体设置 单元格设置 对齐方式等设置)

时间:2023-02-20 21:52:10

相关推荐

jxl 创建表格(包括去掉网格线 字体设置 单元格设置 对齐方式等设置)

效果图:

代码如下:

[java]

importjava.io.File;

importjava.io.IOException;

importjxl.format.Alignment;

importjxl.format.Border;

importjxl.format.BorderLineStyle;

importjxl.format.Colour;

importjxl.format.UnderlineStyle;

importjxl.write.Label;

importjxl.write.NumberFormats;

importjxl.write.WritableCellFormat;

importjxl.write.WritableFont;

importjxl.write.WritableSheet;

importjxl.write.WritableWorkbook;

importjxl.write.WriteException;

importjxl.write.biff.RowsExceededException;

publicclassJxlTable{

privatefinalstaticJxlTablejxlTable=newJxlTable();

publicstaticJxlTablegetInstance(){

returnjxlTable;

}

publicJxlTable(){}

/**

*根据输入的内容创建一个表格

*要求:

*表头表格线为粗线,表体表格线为细线;

*表头背景色为黄色且表头字体加粗居中显示,表体为无色;

*表头以及表体内容可以按照一定的格式输入;

*

*保留一个sheet且sheet的背景为无网格线;

*

*@return创建成功:true;创建失败:false;

*/

publicbooleancreateTable(Stringheader,String[]body,StringfilePath){

booleancreateFlag=true;

WritableWorkbookbook;

try{

//根据路径生成excel文件

book=Workbook.createWorkbook(newFile(filePath));

//创建一个sheet名为"表格"

WritableSheetsheet=book.createSheet("表格",0);

//设置NO列宽度

sheet.setColumnView(1,5);

//去掉整个sheet中的网格线

sheet.getSettings().setShowGridLines(false);

LabeltempLabel=null;

//表头输出

String[]headerArr=header.split(",");

intheaderLen=headerArr.length;

//循环写入表头内容

for(inti=0;i<headerLen;i++){

tempLabel=newLabel(1+i,1,headerArr[i],getHeaderCellStyle());

sheet.addCell(tempLabel);

}

//表体输出

intbodyLen=body.length;

//循环写入表体内容

for(intj=0;j<bodyLen;j++){

String[]bodyTempArr=body[j].split(",");

for(intk=0;k<bodyTempArr.length;k++){

WritableCellFormattempCellFormat=null;

/*

*表体内容的对齐设置

*这里将序号NO以及年龄居中对齐,姓名以及性别默认对齐方式

*/

tempCellFormat=getBodyCellStyle();

if(tempCellFormat!=null){

if(k==0||k==(bodyTempArr.length-1)){

tempCellFormat.setAlignment(Alignment.CENTRE);

}

}

tempLabel=newLabel(1+k,2+j,bodyTempArr[k],tempCellFormat);

sheet.addCell(tempLabel);

}

}

book.write();

book.close();

}catch(IOExceptione){

createFlag=false;

System.out.println("EXCEL创建失败!");

e.printStackTrace();

}catch(RowsExceededExceptione){

createFlag=false;

System.out.println("EXCEL单元设置创建失败!");

e.printStackTrace();

}catch(WriteExceptione){

createFlag=false;

System.out.println("EXCEL写入失败!");

e.printStackTrace();

}

returncreateFlag;

}

/**

*表头单元格样式的设定

*/

publicWritableCellFormatgetHeaderCellStyle(){

/*

*WritableFont.createFont("宋体"):设置字体为宋体

*10:设置字体大小

*WritableFont.BOLD:设置字体加粗(BOLD:加粗NO_BOLD:不加粗)

*false:设置非斜体

*UnderlineStyle.NO_UNDERLINE:没有下划线

*/

WritableFontfont=newWritableFont(WritableFont.createFont("宋体"),

10,

WritableFont.BOLD,

false,

UnderlineStyle.NO_UNDERLINE);

WritableCellFormatheaderFormat=newWritableCellFormat(NumberFormats.TEXT);

try{

//添加字体设置

headerFormat.setFont(font);

//设置单元格背景色:表头为黄色

headerFormat.setBackground(Colour.YELLOW);

//设置表头表格边框样式

//整个表格线为粗线、黑色

headerFormat.setBorder(Border.ALL,BorderLineStyle.THICK,Colour.BLACK);

//表头内容水平居中显示

headerFormat.setAlignment(Alignment.CENTRE);

}catch(WriteExceptione){

System.out.println("表头单元格样式设置失败!");

}

returnheaderFormat;

}

/**

*表头单元格样式的设定

*/

publicWritableCellFormatgetBodyCellStyle(){

/*

*WritableFont.createFont("宋体"):设置字体为宋体

*10:设置字体大小

*WritableFont.NO_BOLD:设置字体非加粗(BOLD:加粗NO_BOLD:不加粗)

*false:设置非斜体

*UnderlineStyle.NO_UNDERLINE:没有下划线

*/

WritableFontfont=newWritableFont(WritableFont.createFont("宋体"),

10,

WritableFont.NO_BOLD,

false,

UnderlineStyle.NO_UNDERLINE);

WritableCellFormatbodyFormat=newWritableCellFormat(font);

try{

//设置单元格背景色:表体为白色

bodyFormat.setBackground(Colour.WHITE);

//设置表头表格边框样式

//整个表格线为细线、黑色

bodyFormat.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);

}catch(WriteExceptione){

System.out.println("表体单元格样式设置失败!");

}

returnbodyFormat;

}

publicstaticvoidmain(String[]args){

Stringheader="NO,姓名,性别,年龄";

String[]body=newString[4];

body[0]="1,欧阳锋,男,68";

body[1]="2,黄药师,男,67";

body[2]="3,洪七公,男,70";

body[3]="4,郭靖,男,32";

StringfilePath="e:/test.xls";

JxlTabletestJxl=JxlTable.getInstance();

booleanflag=testJxl.createTable(header,body,filePath);

if(flag){

System.out.println("表格创建成功!!");

}

}

}

在编程中寻找快乐,在快乐中自由编程!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。