java如何设置Excel表格中第二行,第二列的文字水平居中
时间: 2023-05-17 21:04:02 浏览: 292
您可以使用 Apache POI 库来设置 Excel 表格中的文字水平居中。以下是示例代码:
```java
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelDemo {
public static void main(String[] args) throws IOException {
// 创建工作簿
Workbook workbook = WorkbookFactory.create(true);
// 创建工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 创建行
Row row = sheet.createRow(1);
// 创建单元格
Cell cell = row.createCell(1);
// 设置单元格样式
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中
// 设置单元格内容
cell.setCellValue("Hello, World!");
// 应用样式
cell.setCellStyle(style);
// 保存工作簿
FileOutputStream outputStream = new FileOutputStream("demo.xlsx");
workbook.write(outputStream);
outputStream.close();
}
}
```
这段代码将在 Excel 表格中创建一个单元格,将其内容设置为 "Hello, World!",并将其水平居中。
阅读全文
相关推荐


















