java 如何使用poi将作者姓名设置为excel文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11143303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:57:14  来源:igfitidea点击:

How to set Author name to excel file using poi

javaapache-poi

提问by user1430989

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?

我正在使用 poi (java) 创建一个 excel (.xlsx) 文件。创建 excel 文件后,我将 excel 文件作者视为“Apache POI”。有什么办法可以改变吗?

Here is the code I'm using to create excel file...

这是我用来创建excel文件的代码......

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class CreateExcelFile {

    public static void main(String[] args) {
        /** Name of excel file that we are going to create **/
        String fileName = "C:\temp\testPOIWrite.xlsx";
        writeDataToExcelFile(fileName);
    }

    /** This method writes data to new excel file **/
    private static void writeDataToExcelFile(String fileName) {

        String[][] excelData = preapreDataToWriteToExcel();

        XSSFWorkbook myWorkBook = new XSSFWorkbook();
        Sheet mySheet = myWorkBook.createSheet();
        Row myRow = null;
        Cell myCell = null;

        for (int rowNum = 0; rowNum < excelData[0].length; rowNum++) {
            myRow = mySheet.createRow(rowNum);

            for (int cellNum = 0; cellNum < 4; cellNum++) {
                myCell = myRow.createCell(cellNum);
                myCell.setCellValue(excelData[rowNum][cellNum]);
            }
        }

        try {
            FileOutputStream out = new FileOutputStream(fileName);
            myWorkBook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /** Prepare some demo data as excel file content **/
    public static String[][] preapreDataToWriteToExcel() {
        String[][] excelData = new String[4][4];
        excelData[0][0] = "First Name";
        excelData[0][1] = "Last Name";
        excelData[0][2] = "Telephone";
        excelData[0][3] = "Address";

        excelData[1][0] = "Kushal";
        excelData[1][1] = "Paudyal";
        excelData[1][2] = "000-000-0000";
        excelData[1][3] = "IL,USA";

        excelData[2][0] = "Randy";
        excelData[2][1] = "Ram Robinson";
        excelData[2][2] = "111-111-1111";
        excelData[2][3] = "TX, USA";

        excelData[3][0] = "Phil";
        excelData[3][1] = "Collins";
        excelData[3][2] = "222-222-2222";
        excelData[3][3] = "NY, USA";

        return excelData;

    }
}

回答by Olivier Coilland

It's pretty straightforward:

这很简单:

HSSF:

高铁:

SummaryInformation summaryInfo = workbook.getSummaryInformation();
summaryInfo.setAuthor(author);

XSSF:

XSSF:

POIXMLProperties xmlProps = workbook.getProperties();    
POIXMLProperties.CoreProperties coreProps =  xmlProps.getCoreProperties();
coreProps.setCreator(author);

Have fun :)

玩得开心 :)

回答by mfeineis

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#:

不是直接的答案,但如果有人需要在 NPOI(POI 的 .NET 端口)中执行此操作,这是我想出的扩展方法......这基本上是 Olivier 建议的 - 仅在 C# 中实现:

/// <summary>
/// Sets the author of this workbook.
/// </summary>
/// <param name="workbook"></param>
/// <param name="author"></param>
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;
    }
}