poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Beardsley <markbrd...@tiscali.co.uk>
Subject Re: How to write the metadata into the document.
Date Sat, 18 Dec 2010 15:40:01 GMT

Thanks for that, good to hear it works for you. Now, and with thanks to Aram,
the XSSF code;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.POIXMLProperties;
import java.io.*;

public class DocSummaryInfoXML {

    private XSSFWorkbook workbook = null;

    /**
     * Create a new, empty workbook, insert a sheet into it, a row into the
     * sheet and four cells into the row. Just dummy data.
     */
    public DocSummaryInfoXML() {
        XSSFSheet sheet = null;
        XSSFRow row = null;
        XSSFCell cell = null;
        this.workbook = new XSSFWorkbook();
        sheet = this.workbook.createSheet();
        row = sheet.createRow(0);
        cell = row.createCell(0);
        cell.setCellValue(1);
        cell = row.createCell(1);
        cell.setCellValue(2);
        cell = row.createCell(2);
        cell.setCellValue(3);
        cell = row.createCell(3);
        cell.setCellValue(4);
    }

    /**
     * Modify/set the name of the author. Note that this time the code is
     * a little more cautious checking for null objects. I do not think you
will
     * need to be so careful in the future and this is only test code of
course.
     *
     * @param author An instance of the String class that encapuslates the
     *        name of the document's author.
     */
    public void setAuthor(String author) {

        // Getthe douments Properties
        POIXMLProperties xmlProps = this.workbook.getProperties();
        if(xmlProps != null) {

            // ..and from that. it's core peoperties
            POIXMLProperties.CoreProperties coreProps = 
xmlProps.getCoreProperties();
            if(coreProps!= null) {

                // Then, simply set the name of the author. As with the HSSF
                // example, it i also possible to use this object to set
other
                // properties such as the creation and modification dates,
the
                // title of the document, etc...
                coreProps.setCreator(author);
            }
            else {
                System.out.println("Null core properties.");
            }
        }
        else {
            System.out.println("Null xmlProperties.");
        }
    }

    /**
     * Save the workbook away to disc.
     *
     * @param filename An instance of the String class that encapsulates the
path
     *        to and name of the workbook.
     */
    public void saveWorkbook(String filename) {
        OutputStream os = null;
        File file = null;
        try {
            file = new File(filename);
            os = new FileOutputStream(file);
            this.workbook.write(os);
        }
        catch(IOException ioEx) {
            printExceptionDetails(ioEx);
        }
        finally {
            try {
                if(os != null) {
                    os.close();
                    os = null;
                }
            }
            catch(IOException ioEx) {
                printExceptionDetails(ioEx);
            }
        }
    }

    private static final void printExceptionDetails(Exception ex) {
        System.out.println("Caught a: " + ex.getClass().getName());
        System.out.println("Message: " + ex.getMessage());
        System.out.println("Stacktrace follows:.....");
        ex.printStackTrace(System.out);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            DocSummaryInfoXML dsix = new DocSummaryInfoXML();
            dsix.setAuthor("Harry Houdini");
            dsix.saveWorkbook("C:/temp/props test.xlsx");
        }
        catch(Exception ex) {
            System.out.println(ex.getClass().getName());
            System.out.println(ex.getMessage());
            ex.printStackTrace(System.out);
        }
    }
}

Yours

Mark B
-- 
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-write-the-metadata-into-the-document-tp3309193p3310518.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message