POI Excel导出样式设置

HSSFSheet sheet = workbook.createSheet("sheetName");	//创建sheet
sheet.setVerticallyCenter(true);

//下面样式可作为导出左右分栏的表格模板
sheet.setColumnWidth((short) 0, (short) 2600);// 设置列宽
sheet.setColumnWidth((short) 1, (short) 2400);
sheet.setColumnWidth((short) 2, (short) 2300);
sheet.setColumnWidth((short) 3, (short) 1600);
sheet.setColumnWidth((short) 4, (short) 1800);
sheet.setColumnWidth((short) 5, (short) 1000);// 空列设置小一些
sheet.setColumnWidth((short) 6, (short) 2600);// 设置列宽
sheet.setColumnWidth((short) 7, (short) 2400);
sheet.setColumnWidth((short) 8, (short) 2300);
sheet.setColumnWidth((short) 9, (short) 1600);
sheet.setColumnWidth((short) 10, (short) 1800);

HSSFCellStyle cellstyle = (HSSFCellStyle) workbook.createCellStyle();// 设置表头样式
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置居中

HSSFCellStyle headerStyle = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);	//设置垂直居中
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);	//设置水平居中
HSSFFont headerFont = (HSSFFont) workbook.createFont();	//创建字体样式
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	// 字体加粗
headerFont.setFontName("Times New Roman");	//设置字体类型
headerFont.setFontHeightInPoints((short) 8);	//设置字体大小
headerStyle.setFont(headerFont);	//为标题样式设置字体样式

HSSFCellStyle headerStyle1 = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式1
headerStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
headerStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont headerFont1 = (HSSFFont) workbook.createFont();
headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
headerFont1.setFontName("Times New Roman");
headerFont1.setFontHeightInPoints((short) 8);
headerStyle1.setFont(headerFont1);

HSSFCellStyle headerStyle2 = (HSSFCellStyle) workbook .createCellStyle();// 创建标题样式2
headerStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
headerStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont headerFont2 = (HSSFFont) workbook.createFont();
headerFont2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
headerFont2.setFontName("Times New Roman");
headerFont2.setFontHeightInPoints((short) 8);
headerStyle2.setFont(headerFont2);
headerStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
headerStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
headerStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
headerStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

HSSFCellStyle cell_Style = (HSSFCellStyle) workbook .createCellStyle();// 设置字体样式
cell_Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell_Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直对齐居中
cell_Style.setWrapText(true); // 设置为自动换行
HSSFFont cell_Font = (HSSFFont) workbook.createFont();
cell_Font.setFontName("宋体");
cell_Font.setFontHeightInPoints((short) 8);
cell_Style.setFont(cell_Font);
cell_Style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
cell_Style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
cell_Style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
cell_Style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

HSSFRow row = sheet.createRow((short)1);	//创建行
HSSFCell cell = row.createCell((short)1);	//创建列
cell.setCellStyle(headerStyle2);	//单元格引用样式

POI-Excel导出样式设置.java 67行 Java

  
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您好!感谢您的提问。关于POIExcel导出模板,您可以参考以下步骤: 1. 首先,确保您已经引入了POIExcel库。您可以在项目的pom.xml文件中添加如下依赖项: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个Excel模板文件,可以使用Microsoft Excel或其他电子表格软件创建。在模板中,设置表头样式等内容。 3. 在Java代码中,使用POIExcel库读取模板文件,并进行数据填充。以下是一个简单的示例: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ExcelExportTemplate { public static void main(String[] args) { String templateFilePath = "path/to/template.xlsx"; // 模板文件路径 String outputFilePath = "path/to/output.xlsx"; // 输出文件路径 try (FileInputStream fis = new FileInputStream(templateFilePath); Workbook workbook = new XSSFWorkbook(fis); FileOutputStream fos = new FileOutputStream(outputFilePath)) { Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 Row dataRow = sheet.getRow(1); // 获取数据行 Cell cell = dataRow.getCell(0); // 获取第一列单元格 cell.setCellValue("John Doe"); // 填充数据 workbook.write(fos); // 写入输出文件 } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码中,我们首先使用FileInputStream读取模板文件,然后创建XSSFWorkbook实例表示工作簿。接着,我们获取工作表和数据行,并使用setCellValue方法填充数据。最后,使用FileOutputStream将工作簿写入输出文件。 请注意,以上只是一个简单示例,您可以根据自己的需求进行更复杂的操作,例如循环填充数据、设置样式等。 希望以上信息能对您有所帮助!如有任何疑问,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值