目录
推荐博客
java生成Excel文件_Oliviajingxiang的博客-CSDN博客_java生成excel文件
实战
一、添加依赖
<!-- 使用 xls 格式-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- 使用 xlsx 格式-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
二、创建excel文件列格式
package com.upxcy.exceldemo;
import com.alibaba.excel.annotation.ExcelProperty;
public class MarketDataExcel {
@ExcelProperty("指数代码")
private String indexCode;
@ExcelProperty("日期")
private String date;
@ExcelProperty("点位")
private double close;
public MarketDataExcel() {
}
public MarketDataExcel(String indexCode, String date, double close) {
this.indexCode = indexCode;
this.date = date;
this.close = close;
}
public String getIndexCode() {
return indexCode;
}
public void setIndexCode(String indexCode) {
this.indexCode = indexCode;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public double getClose() {
return close;
}
public void setClose(double close) {
this.close = close;
}
}
三、设置表头和内容样式
package com.upxcy.exceldemo;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;
public class CellStyleStrategy {
/**
* 设置表头和内容样式
*/
public static HorizontalCellStyleStrategy styleStrategy() {
//表头样式策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置表头居中对齐
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
headWriteCellStyle.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.index);
headWriteCellStyle.setRightBorderColor(IndexedColors.GREY_25_PERCENT.index);
headWriteCellStyle.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.index);
headWriteCellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.index);
WriteFont headWriteFont = new WriteFont();
headWriteFont.setBold(false);
headWriteFont.setFontName("Arial");
headWriteFont.setFontHeightInPoints((short) 10);
headWriteCellStyle.setWriteFont(headWriteFont);
//2 内容样式策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
//内容大小
contentWriteFont.setFontName("Arial");
contentWriteFont.setFontHeightInPoints((short) 10);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// //设置自动换行
// contentWriteCellStyle.setWrapped(true);
// //设置垂直居中
// contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// //头默认了FillPatternType所以可以不指定
// contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// //设置水平居中
// contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//
// //设置边框样式
// contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
// contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
// contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
// contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
return new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle);
}
}
四、执行
public static void main(String[] args) {
ArrayList<MarketDataExcel> marketDataExcels = new ArrayList<>();
EasyExcel.write("D:\\excelTest.xlsx",MarketDataExcel.class)
.registerWriteHandler(CellStyleStrategy.styleStrategy())
.sheet("Market Data").doWrite(marketDataExcels);
}
删除Excel一行
java使用poi操作excel删除一整行_年轻就是天堂的博客-CSDN博客_java删除excel行
PS:
//xls 格式
HSSFWorkbook workbook = new HSSFWorkbook(is);
//xlsx 格式
XSSFWorkbook workbook = new XSSFWorkbook(is);