java poi设置excel表头居中
时间: 2023-11-01 18:08:55 浏览: 171
您可以使用以下代码将Excel表头居中:
```
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("表头");
cell.setCellStyle(style);
```
其中,`workbook`是你创建的Excel工作簿对象,`sheet`是你创建的工作表对象。相关问题:
相关问题
JAVA语言POI导出EXCEL设置首行标题合并单元格再加上标题居中, 加粗,并且所有单元格加上边框,请给出完整示例
好的,以下是一个完整的示例代码,实现了首行标题合并单元格、标题居中、加粗和单元格边框的要求:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelExportExample {
public static void main(String[] args) {
// 创建工作簿和活动表
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("学生成绩单");
// 设置标题
Row titleRow = sheet.createRow(0);
titleRow.createCell(0).setCellValue("学生成绩单");
sheet.addMergedRegion(CellRangeAddress.valueOf("A1:C1"));
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HorizontalAlignment.CENTER);
Font titleFont = workbook.createFont();
titleFont.setBold(true);
titleStyle.setFont(titleFont);
titleRow.getCell(0).setCellStyle(titleStyle);
// 设置表头
Row headerRow = sheet.createRow(1);
headerRow.createCell(0).setCellValue("姓名");
headerRow.createCell(1).setCellValue("语文");
headerRow.createCell(2).setCellValue("数学");
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
headerRow.getCell(i).setCellStyle(headerStyle);
}
// 设置学生成绩
Row dataRow1 = sheet.createRow(2);
dataRow1.createCell(0).setCellValue("张三");
dataRow1.createCell(1).setCellValue(90);
dataRow1.createCell(2).setCellValue(80);
Row dataRow2 = sheet.createRow(3);
dataRow2.createCell(0).setCellValue("李四");
dataRow2.createCell(1).setCellValue(85);
dataRow2.createCell(2).setCellValue(92);
Row dataRow3 = sheet.createRow(4);
dataRow3.createCell(0).setCellValue("王五");
dataRow3.createCell(1).setCellValue(88);
dataRow3.createCell(2).setCellValue(87);
// 设置单元格边框
CellStyle dataStyle = workbook.createCellStyle();
dataStyle.setAlignment(HorizontalAlignment.CENTER);
dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
dataStyle.setBorderTop(BorderStyle.THIN);
dataStyle.setBorderRight(BorderStyle.THIN);
dataStyle.setBorderBottom(BorderStyle.THIN);
dataStyle.setBorderLeft(BorderStyle.THIN);
for (int i = 2; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
row.getCell(j).setCellStyle(dataStyle);
}
}
// 保存文件
try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在上面的代码中,我们首先创建工作簿和活动表,然后使用 `sheet.addMergedRegion()` 将 A1 到 C1 的单元格合并成一个单元格,并使用 `CellStyle` 和 `Font` 分别设置居中和加粗样式。接着,我们设置了表头和学生成绩,并使用循环为表头和学生成绩的单元格设置了边框和填充颜色样式。最后,我们使用 `workbook.write()` 将工作簿保存到本地。
阅读全文