import jxl.Workbook;
import jxl.format.*;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import jxl.write.Label;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* @ClassName TestExcel
* @Author 王弈程
* @Date 2022/1/19 9:57
**/
@SpringBootTest
public class TestExcel {
@Test
public void DongAoExcel(){
//模拟开始时间和结束时间
String startTime = "2022-01-01";
String endTime = "2022-01-31";
//给时间加上或者减上几天(自己定义的时间类)
Date date = DateUtils.addDateMinut(new Date(), -1);
//将时间类型的转化为String类型的(自己定义的时间类)
String yyyyMMdd = DateUtils.dateToString2(date);
// 拼接一个Excel名字
String filename = yyyyMMdd + "奖品发放及使用日报.xls";
//文件保存路径
String path = "E:\\abc" + filename;
// Io流
File name = new File(path);
try {
// 创建写工作簿对象
WritableWorkbook workbook = Workbook.createWorkbook(name);
// 工作表
String[] Firsttitle = new String[]{"\"欢乐东奥年,集卡赢好礼\"奖品数据需求-日表-资源平台"};
String[] Secondtitle = new String[]{"时间", "话费券发放量", "现金券发放量", "现金券使用量", "东奥周边发放量"};
String[] Thirdtitle = new String[]{"抽中话费券(奖券)","抽中话费券(奖券)","","10元代扣部分使用数据", ""};
WritableSheet sheet = workbook.createSheet("分省、分奖品数据", 0);
// 设置字体;
WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"),
18,
WritableFont.BOLD,
false,
UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
//创建样式
WritableCellFormat cellFormat = new WritableCellFormat(font);
// 设置背景颜色;
Color color = Color.decode("#DFF8FF"); // 自定义的颜色
workbook.setColourRGB(Colour.LIGHT_TURQUOISE, color.getRed(),
color.getGreen(), color.getBlue());
cellFormat.setBackground(Colour.LIGHT_TURQUOISE);
// 设置边框;
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
// 设置文字居中对齐方式;
cellFormat.setAlignment(Alignment.CENTRE);
// 设置垂直居中;
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
// 给sheet电子版中设置列的宽度;
//sheet.getSettings().setDefaultColumnWidth(14);
sheet.setColumnView(0,10);
for (int i=1;i<Secondtitle.length;i++){
sheet.setColumnView(i,30);
}
// 给sheet电子版中的行设置高度;
sheet.setRowView(0,1000);
sheet.setRowView(1,1000);
sheet.setRowView(2,1400);
/*a 单元格的列号
b 单元格的行号
c 从单元格[a,b]起,向下合并到c列
d 从单元格[a,b]起,向下合并到d行*/
// 设置合并单元格
sheet.mergeCells(0,0,4,0);
sheet.mergeCells(0,1,0,2);
// 设置自动换行;
cellFormat.setWrap(true);
//表头
for (int i = 0; i < Firsttitle.length; i++) {
jxl.write.Label label = new jxl.write.Label(i, 0, Firsttitle[i], cellFormat);
sheet.addCell(label);
}
WritableFont font2 = new WritableFont(WritableFont.createFont("微软雅黑"),
14,
WritableFont.BOLD,
false,
UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat cellFormat2 = new WritableCellFormat(font2);
// 设置边框;
cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);
// 设置文字居中对齐方式;
cellFormat2.setAlignment(Alignment.CENTRE);
// 设置垂直居中;
cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
//第二行标题
for (int i=0;i<Secondtitle.length;i++){
jxl.write.Label label = new jxl.write.Label(i, 1, Secondtitle[i], cellFormat2);
sheet.addCell(label);
}
//第三行标题
for (int i=0;i< Thirdtitle.length;i++){
jxl.write.Label label = new jxl.write.Label(i, 2, Thirdtitle[i], cellFormat2);
sheet.addCell(label);
}
//数据开始的行数
int n = 3;
List<String> days = DateUtils.getDays(startTime, endTime);
for (String day : days) {
Label label0 = new Label(0, n, day,cellFormat2);
Label label1 = new Label(1, n, "100",cellFormat2);
Label label2 = new Label(2, n, "58",cellFormat2);
Label label3 = new Label(3, n, "100",cellFormat2);
Label label4 = new Label(4, n, "8",cellFormat2);
sheet.addCell(label0);
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
n++;
}
//开始执行写入操作
workbook.write();
//关闭流
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
Java生成Excel,jxl
于 2022-01-19 15:52:49 首次发布