package test.utils;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
//此类用到主要的类有
//WritableWorkbook,Workbook,File,WritableSheet,WritableFont,WritableCellFormat,Label等
public class ExcelExportTest {
//学习创建excel表格,导入导出等
public static void main(String[] args) throws IOException, Exception {
//创建Excel工作簿
WritableWorkbook workbook = Workbook.createWorkbook(new File("C:\Users\macbook\Desktop\我的测试.xls"));
//创建excel电子簿
WritableSheet sheet = workbook.createSheet("第一个sheet", 0);
//分别给2,3,4列设置不同的宽度
sheet.setColumnView(1, 40);
sheet.setColumnView(1, 30);
sheet.setColumnView(2, 50);
sheet.setColumnView(3, 50);
//给sheet电子版中所有的列设置默认的列的宽度
sheet.getSettings().setDefaultColumnWidth(30);
//设置字体
WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
//设置背景色
cellFormat1.setBackground(Colour.BLUE_GREY);
//设置边框,dashed虚线,dash_dot实线,dotted虚线,double双实线
cellFormat1.setBorder(Border.ALL, BorderLineStyle.DOUBLE);
//设置边框颜色
cellFormat1.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.SKY_BLUE);
//设置自换行
cellFormat1.setWrap(true);
//设置文字居中对齐方式
cellFormat1.setAlignment(Alignment.CENTRE);
//设置垂直居中
cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
//创建单元格
Label label1 = new Label(0,0,"第一行第一个单元格(自动换行测试)",cellFormat1);
Label label2 = new Label(1,0,"第一行第二个单元格",cellFormat1);
Label label3 = new Label(2,0,"第一行第三个单元格",cellFormat1);
Label label4 = new Label(3,0,"第一行第四个单元格",cellFormat1);
//添加到行中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
for (int i = 0; i < 10; i++) {
//【0,0】【0,1】【0,2】优先按列输出{此为矩阵输出的方法},如输出99乘法表,则第二个for循环j=i即可
//悟:类似于矩阵下标的【a,b】以及【b,a】,通过j=i第二个for,可以去掉【a,b】或【b,a】,得到上三角或者下三角
for (int j = 1; j <10 ; j++) {
Label label = new Label(i,j,""+i+"*"+j+"="+(i*j),cellFormat1);
sheet.addCell(label);
}
}
//给第二行设置背景、字体颜色、对其方式等等
//省略,后续添加
workbook.write();
workbook.close();
}
}