用easyexcel创建从8点开始到23点结束,其中每个单元格的时间间隔30分钟的表
时间: 2023-05-12 22:05:20 浏览: 77
非常感谢您的提问!以下是使用 EasyExcel 创建从8点开始到23点结束,每个单元格时间间隔为30分钟的表的代码示例:
```java
// 导入 EasyExcel 相关的包
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ExcelUtil {
public static void main(String[] args) {
// 定义表头
List<List<String>> head = new ArrayList<>();
List<String> head0 = new ArrayList<>();
head0.add("时间");
head.add(head0);
// 定义数据
List<List<Object>> data = new ArrayList<>();
for (int i = 8; i <= 23; i++) {
for (int j = 0; j < 2; j++) {
List<Object> rowData = new ArrayList<>();
String time = i + ":" + (j == 0 ? "00" : "30");
rowData.add(time);
data.add(rowData);
}
}
// 定义样式
WriteCellStyle headStyle = new WriteCellStyle();
headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headStyle.setFillPatternType(org.apache.poi.ss.usermodel.FillPatternType.SOLID_FOREGROUND);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
WriteCellStyle contentStyle = new WriteCellStyle();
contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 定义写入策略
HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headStyle, contentStyle);
// 写入 Excel 文件
String fileName = "time_table.xlsx";
ExcelWriterBuilder writerBuilder = EasyExcel.write(fileName);
ExcelWriterSheetBuilder sheetBuilder = writerBuilder.sheet();
sheetBuilder.head(head).registerWriteHandler(styleStrategy).doWrite(data);
}
}
```
希望这个代码示例能够帮助到您!
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)