我需要通过Java中的apache poi更改生成的excel的作者姓名。目前,apache生成的所有文件的作者名称均为“ Apache POI”,我需要对其进行更改。谁可以帮我这个事?
答案 0 :(得分:1)
HSSFWorkbook
是具有POIDocument的SummaryInformation。
XSSFWorkbook
是具有POIXMLDocument-POIXMLProperties的POIXMLProperties.CoreProperties。
为XSSF
和HSSF
设置作者(aka创建者)的代码:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
class CreateExcelAuthor {
public static void main(String[] args) throws Exception {
String author = "Axel Richter";
Workbook workbook = new XSSFWorkbook();
//Workbook workbook = new HSSFWorkbook();
workbook.createSheet();
if (workbook instanceof XSSFWorkbook) {
((XSSFWorkbook)workbook).getProperties().getCoreProperties().setCreator(author);
} else if (workbook instanceof HSSFWorkbook) {
((HSSFWorkbook)workbook).createInformationProperties();
((HSSFWorkbook)workbook).getSummaryInformation().setAuthor(author);
}
String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
try (FileOutputStream out = new FileOutputStream(fileName) ) {
workbook.write(out);
}
workbook.close();
}
}