码迷,mamicode.com
首页 > 其他好文 > 详细

Jxl操作Excel定义格式

时间:2020-04-16 16:53:57      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:cep   void   excel   line   ica   ring   方法   setw   颜色   

定义格式工具类

package net.wwwyibu.orm;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
/**
 * 定义Excel格式
 * @author 马家立
 * @version 2018年9月27日
 */
public class ExceltitlefFormat {
    private WritableCellFormat titlefFormat;    //设置字体 宋体10
    private WritableCellFormat titlefFormat1;    //设置字体 宋体加粗18
    private WritableCellFormat titlefFormat2;    //设置字体 宋体加粗13
    private WritableCellFormat titlefFormat3;    //设置字体 宋体加粗10,标记待扣分,蓝色
    private WritableCellFormat titlefFormat4;    //设置字体 宋体加粗10,标记已扣分,红色
    private WritableCellFormat titlefFormat5;    //设置字体 宋体加粗10,标记免扣分,黑色
    private WritableCellFormat titlefFormat6;    //设置字体 宋体加粗10,绿色
    public WritableCellFormat getTitlefFormat(){
        try {
            jxl.write.WritableFont titleFont = new jxl.write.WritableFont(
                    WritableFont.createFont("宋体"), 10);
            titlefFormat = new WritableCellFormat(titleFont);
            titlefFormat.setAlignment(jxl.format.Alignment.CENTRE);//设置居中
            titlefFormat.setBorder(Border.ALL, BorderLineStyle.THIN);//设置边框
            /*//设置自动换行
            WritableCellFormat writableCellFormat = new WritableCellFormat(titleFont);
            writableCellFormat.setWrap(true);*/
            titlefFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//垂直居中
            titlefFormat.setWrap(true);
            //titlefFormat.setBackground(Colour.BLUE_GREY);  //设置背景颜色
        } catch (Exception e) {
            e.printStackTrace();
        }
        return titlefFormat;
    }
    public WritableCellFormat getTitlefFormat1() {
        try {
            jxl.write.WritableFont titleFont1 = new jxl.write.WritableFont(
                    WritableFont.createFont("宋体"), 18, WritableFont.BOLD);
            titlefFormat1 = new WritableCellFormat(titleFont1);
            titlefFormat1.setAlignment(jxl.format.Alignment.CENTRE);//设置居中
            titlefFormat1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//垂直居中
            titlefFormat1.setBorder(Border.ALL, BorderLineStyle.THIN);//设置边框
        } catch (Exception e) {
            e.printStackTrace();
        }
        return titlefFormat1;
    }
    public WritableCellFormat getTitlefFormat2() {
        try {
            jxl.write.WritableFont titleFont2 = new jxl.write.WritableFont(
                    WritableFont.createFont("宋体"), 13, WritableFont.BOLD);
            titlefFormat2 = new WritableCellFormat(titleFont2);
            titlefFormat2.setAlignment(jxl.format.Alignment.CENTRE);//设置水平居中
            titlefFormat2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//垂直居中
            titlefFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);//设置边框
        } catch (Exception e) {
            e.printStackTrace();
        }
        return titlefFormat2;
    }
    public WritableCellFormat getTitlefFormat3() {
        try {
            jxl.write.WritableFont titleFont3 = new jxl.write.WritableFont(
                    WritableFont.createFont("宋体"),10,WritableFont.BOLD,
                    false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE);
            titlefFormat3 = new WritableCellFormat(titleFont3);
            titlefFormat3.setAlignment(jxl.format.Alignment.CENTRE);//设置居中
            titlefFormat3.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//垂直居中
            titlefFormat3.setBorder(Border.ALL, BorderLineStyle.THIN);//设置边框
        } catch (Exception e) {
            e.printStackTrace();
        }
        return titlefFormat3;
    }
    public WritableCellFormat getTitlefFormat4() {
        try {
            jxl.write.WritableFont titleFont4 = new jxl.write.WritableFont(
                    WritableFont.createFont("宋体"),10,WritableFont.BOLD,
                    false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
            titlefFormat4 = new WritableCellFormat(titleFont4);
            titlefFormat4.setAlignment(jxl.format.Alignment.CENTRE);//设置居中
            titlefFormat4.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//垂直居中
            titlefFormat4.setBorder(Border.ALL, BorderLineStyle.THIN);//设置边框
        } catch (Exception e) {
            e.printStackTrace();
        }
        return titlefFormat4;
    }
    public WritableCellFormat getTitlefFormat5() {
        try {
            jxl.write.WritableFont titleFont5 = new jxl.write.WritableFont(
                    WritableFont.createFont("宋体"),10,WritableFont.BOLD,
                    false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            titlefFormat5 = new WritableCellFormat(titleFont5);
            titlefFormat5.setAlignment(jxl.format.Alignment.CENTRE);//设置居中
            titlefFormat5.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//垂直居中
            titlefFormat5.setBorder(Border.ALL, BorderLineStyle.THIN);//设置边框
        } catch (Exception e) {
            e.printStackTrace();
        }
        return titlefFormat5;
    }
    public WritableCellFormat getTitlefFormat6() {
        try {
            jxl.write.WritableFont titleFont6 = new jxl.write.WritableFont(
                    WritableFont.createFont("宋体"),10,WritableFont.BOLD,
                    false,UnderlineStyle.NO_UNDERLINE,Colour.GREEN);
            titlefFormat6 = new WritableCellFormat(titleFont6);
            titlefFormat6.setAlignment(jxl.format.Alignment.CENTRE);//设置居中
            titlefFormat6.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//垂直居中
            titlefFormat6.setBorder(Border.ALL, BorderLineStyle.THIN);//设置边框
        } catch (Exception e) {
            e.printStackTrace();
        }
        return titlefFormat6;
    }
    public static void main(String[] args) {
        ExceltitlefFormat oo = new ExceltitlefFormat();
        WritableCellFormat oooCellFormat = oo.getTitlefFormat();
        System.out.println(oooCellFormat);
        System.out.println(123);
    }
}

 测试导出Excel的Main方法:

import jxl.Workbook;  
  public static void main(String[] args) {
        try {
            File file = new File("D:\\测试Exce导出.xlsx");
            if(!file.exists()) {
                file.createNewFile();
            }
            jxl.write.WritableWorkbook wwb = null;
            wwb = Workbook.createWorkbook(file);
            //0是sheet编号
            jxl.write.WritableSheet sheet = wwb.createSheet("测试Excel", 0);
            // Excel格式类
            ExceltitlefFormat ef = new ExceltitlefFormat();
            // 设置列宽(第几列,多少px),若不设置则是为默认
            sheet.setColumnView(0, 10);
            sheet.setColumnView(1, 15);
            sheet.setColumnView(2, 20);
            sheet.setColumnView(3, 25);
            sheet.setColumnView(4, 30);
            sheet.setColumnView(5, 35);
            //开始写数据
            sheet.addCell(new jxl.write.Label(0, 0,"123",ef.getTitlefFormat()));
            sheet.addCell(new jxl.write.Label(1, 0,"123",ef.getTitlefFormat1()));
            sheet.addCell(new jxl.write.Label(2, 0,"123",ef.getTitlefFormat2()));
            sheet.addCell(new jxl.write.Label(3, 0,"123",ef.getTitlefFormat3()));
            sheet.addCell(new jxl.write.Label(4, 0,"123",ef.getTitlefFormat4()));
            sheet.addCell(new jxl.write.Label(5, 0,"123",ef.getTitlefFormat5()));
            sheet.addCell(new jxl.write.Label(6, 0,"123",ef.getTitlefFormat6()));
            sheet.addCell(new jxl.write.Label(7, 0,"123",ef.getTitlefFormat()));
            wwb.write();
            wwb.close();
            System.out.println("导出成功");
        } catch (WriteException e) {
            e.printStackTrace();
            System.out.println("导出失败");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("导出失败");
        }
   }

 

Jxl操作Excel定义格式

标签:cep   void   excel   line   ica   ring   方法   setw   颜色   

原文地址:https://www.cnblogs.com/mjtabu/p/12713817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!