本文概述
Apache POI允许我们为excel文档设置标题。它提供Sheet接口的getHeader()方法并返回Header类型的实例。
让我们看一个示例,其中我们将为文档创建和设置标题。
Apache POI Excel标头示例
package poiexample;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class HeaderExample {
public static void main(String[] args) {
try (OutputStream fileOut = new FileOutputStream("srcmini.xls")) {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet");
// Creating Header
Header header = sheet.getHeader();
header.setCenter("Center Header");
header.setLeft ("Left Header");
header.setRight ("Right align Header");
// Creating Row
Row row = sheet.createRow(4); // Creating a row
Cell cell = row.createCell(1); // Creating a cell
cell.setCellValue("Hello, srcmini!");
wb.write(fileOut);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
输出:
此输出是文件的printview。
评论前必须登录!
注册