加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 16:38:02 所属栏目:Java 来源:网络整理
导读:我正在使用poi( java)创建一个excel(.xlsx)文件.在我创建excel文件后,我看到excel文件作为“Apache POI”.有什么办法改变吗? 这是我用来创建excel文件的代码… import java.io.FileOutputStream;import org.apache.poi.ss.usermodel.Cell;import org.apache
我正在使用poi( java)创建一个excel(.xlsx)文件.在我创建excel文件后,我看到excel文件作为“Apache POI”.有什么办法改变吗?

这是我用来创建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:temptestPOIWrite.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;

    }
}

解决方法

这很简单:

HSSF:

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

XSSF:

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

玩的开心 :)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读