java导出excel设置行高列宽_Jxl导出Excel(跨行、设置列宽、设置行高、自动对齐、自定义背景等)...

package com.exl.test;

import java.awt.Color;

import java.io.File;

import jxl.CellView;

import jxl.Workbook;

import jxl.format.Alignment;

import jxl.format.Colour;

import jxl.format.UnderlineStyle;

import jxl.write.Label;

import jxl.write.WritableCellFormat;

import jxl.write.WritableFont;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import com.exl.utils.ColourUtil;

public class Test {

public static void main(String[] args) throws Exception {

String title="报表测试";

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六行","第七行","第八行"},

};

String filePath="D:\\DesignSource\\tempT";

String fileName="NewProject.xls";

File dir=new  File(filePath);

if(!dir.isDirectory()){

dir.mkdirs();

}

File file = new File(filePath+"\\"+fileName);

WritableWorkbook workbook = Workbook.createWorkbook(file);

WritableSheet sheet = workbook.createSheet("报表统计", 0);  //单元格

/**

* title

*/

Label lab = null;

WritableFont   wf2   =   new   WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色

WritableCellFormat wcfTitle = new WritableCellFormat(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); //设置垂直对齐

CellView navCellView = new CellView();

navCellView.setAutosize(true); //设置自动大小

navCellView.setSize(18);

lab = new Label(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.WritableFont wfcNav =new jxl.write.WritableFont(WritableFont.ARIAL,12, WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);

WritableCellFormat wcfN=new WritableCellFormat(wfcNav);

Color color = 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(int i=0;i

lab = new Label(i,1,navTitle[i],wcfN); //Label(col,row,str);

sheet.addCell(lab);

sheet.setColumnView(i, new String(navTitle[i]).length());

}

/**

* 内容

*/

jxl.write.WritableFont wfcontent =new jxl.write.WritableFont(WritableFont.ARIAL,12, WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);

WritableCellFormat wcfcontent = new WritableCellFormat(wfcontent);

wcfcontent.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle边框

wcfcontent.setAlignment(Alignment.CENTRE);

CellView cellView = new CellView();

cellView.setAutosize(true); //设置自动大小

for(int i=0;i

for(int j=0;j

sheet.setColumnView(i, cellView);//根据内容自动设置列宽

lab = new Label(j,i+2,content[i][j],wcfcontent); //Label(col,row,str);

sheet.addCell(lab);

//               sheet.setColumnView(j, new String(content[i][j]).length());

}

}

workbook.write();

workbook.close();

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值