poi 导出excel
程序员文章站
2022-07-13 13:01:25
...
1、添加jar
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
2、编辑excel
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
/**
* Created by Steve on 2018/10/24 0024.
*/
public class XLSXFileWriter {
public static void writer() throws Exception {
Workbook workbook = new XSSFWorkbook();
// 创建shell
Sheet sheet = workbook.createSheet("南京英霸建材科技发展有限公司财务指标对比结果表");
// 创建行
Row row = sheet.createRow(0);
// 创建列
Cell cell = row.createCell(0);
cell.setCellValue("南京英霸建材科技发展有限公司财务指标对比结果表");
// 合并单元格(行,行,列,列)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
// 设置单元格样式
CellStyle cellStyle = workbook.createCellStyle();
// 设置水平居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// 设置垂直居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cell.setCellStyle(cellStyle);
row = sheet.createRow(1);
row.createCell(0).setCellValue("导出日期:2018-09-10");
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
row = sheet.createRow(2);
// 设置字体加粗
cellStyle = workbook.createCellStyle();
// 设置水平居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// 设置垂直居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Font font = workbook.createFont();
font.setBold(true);
cellStyle.setFont(font);
cell = row.createCell(0);
cell.setCellValue("指标值");
cell.setCellStyle(cellStyle);
cell = row.createCell(1);
cell.setCellValue("南京英霸建材科技发展有限公司");
cell.setCellStyle(cellStyle);
cell = row.createCell(2);
cell.setCellValue("陆特能源");
cell.setCellStyle(cellStyle);
// 设置自适应列宽
for (int i = 0; i < 3; i++) {
sheet.autoSizeColumn(i,true);
sheet.setColumnWidth(i,sheet.getColumnWidth(i)*2);
}
File file = new File("F:/var/excel/test.xlsx");
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
workbook.write(new FileOutputStream(file));
}
public static void main(String[] args) throws Exception {
writer();
System.out.println("完成");
}
}
上一篇: poi生成Excel文件
下一篇: Flex设置LinkButton的背景色