java 根据指定模板导出excel

yue_hun yue_hun 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
package com;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtil {
public static String outFile = "D:\\tmp\\t\\cpmodel.xlsx";
public static String modelUrl = "D:\\tmp\\t\\model.xlsx";
public static int startNum = 7;
public static void main(String[] args) {
try {
Workbook workbook = null;
try {
workbook = new XSSFWorkbook(modelUrl);
} catch (Exception ex) {
workbook = new HSSFWorkbook(new FileInputStream(modelUrl));
}
Sheet sheet = workbook.getSheetAt(0);
// 设置细边框
CellStyle cellStyle= workbook.createCellStyle();
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);

// 设置自动换行
cellStyle.setWrapText(true);
for (int i = startNum; i < 20; i++) {
Row row = sheet.createRow(i); // 在索引0的位置开始创建行(最顶端的行)
for (int j = 0; j < 6; j++) {
Cell cell = row.createCell(j); // 在索引0的位置开始创建单元格(左上端)
cell.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置单元格的类型为字符串;
cell.setCellValue(j + "==" + j); // 在单元格输入一些内容;
cell.setCellStyle(cellStyle);
}
}
FileOutputStream out = new FileOutputStream(outFile); // 创建文件输出流
workbook.write(out);
out.flush();
out.close();
System.out.println("文件生成...");

} catch (Exception e) {
System.out.println("已运行 xlCreate():" + e);
}
}

}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
yue_hun
Java 51,407 成员 86,127 内容