How to set Author name to excel file using poi

后端 未结 2 438
难免孤独
难免孤独 2021-01-01 15:08

I\'m creating an excel (.xlsx) file using poi (java). After I create the excel file I see the excel file Author as \"Apache POI\". Is there any way to change that?

H

2条回答
  •  臣服心动
    2021-01-01 15:41

    Not a direct answer but in case anyone needs to do this in NPOI (the .NET port of POI) here is the extension method I came up with ... it's basically what Olivier suggested - only implemented in C#:

    /// 
    /// Sets the author of this workbook.
    /// 
    /// 
    /// 
    public static void SetAuthor(this IWorkbook workbook, string author)
    {
        if (workbook is NPOI.XSSF.UserModel.XSSFWorkbook)
        {
            var xssfWorkbook = workbook as NPOI.XSSF.UserModel.XSSFWorkbook;
            var xmlProps = xssfWorkbook.GetProperties();
            var coreProps = xmlProps.CoreProperties;
            coreProps.Creator = author;
            return;
        }
    
        if (workbook is NPOI.HSSF.UserModel.HSSFWorkbook)
        {
            var hssfWorkbook = workbook as NPOI.HSSF.UserModel.HSSFWorkbook;
            var summaryInfo = hssfWorkbook.SummaryInformation;
    
            if (summaryInfo != null)
            {
                summaryInfo.Author = author;
                return;
            }
    
            var newDocInfo = NPOI.HPSF.PropertySetFactory.CreateDocumentSummaryInformation();
    
            var newInfo = NPOI.HPSF.PropertySetFactory.CreateSummaryInformation();
            newInfo.Author = author;
    
            hssfWorkbook.DocumentSummaryInformation = newDocInfo;
            hssfWorkbook.SummaryInformation = newInfo;
    
            return;
        }
    }
    

提交回复
热议问题