Discussion:
How to write the metadata into the document.
Nan Lei
2010-12-17 09:50:17 UTC
Permalink
Hi all,

I am using POI excel module to write Excel dynamically, now I have a problem
about how to write metadata into the document I write.

For example, when I generate a Workbook, how to set the "Author" info into
it.

If I don't set it, the default one is "Apache POI", and I want customize it
myself.

So I want to know how to set the metadata.For HSSF*(deal with 2003) and
XSSF*(deal with 2007) may be different. So I want to know the full
solutions.

Best Wishes!
Nan Lei
Mark Beardsley
2010-12-17 16:01:12 UTC
Permalink
For the older binary format files - HSSF - take a look at HPSF
http://poi.apache.org/hpsf/index.html. Not sure at all how to do the same
with SpreadsheetML documents but if you search back through the list I
remember seeing a discussion on this point a while back.

Hope this helps.

Yours

Mark B
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-write-the-metadata-into-the-document-tp3309193p3309616.html
Sent from the POI - User mailing list archive at Nabble.com.
Nan Lei
2010-12-18 04:35:22 UTC
Permalink
Hi Mark,
From the online doc, it is said that the summary information must be get
from a disk file, but I am using the POI API to generate the Excel, not load
from a disk file,so is there any example for me to reference?

Best Wishes!
Nan Lei
Mark Beardsley
2010-12-18 08:04:09 UTC
Permalink
No examples, sorry. Have you tried to use HSPF yet? I would put together some
very simple test code just to see wha thappens. Also, you could look into
the POIDocument class because there are methods there that allow you to set
the values of many of these values. That may be the better place to start.
Cannot rememner the names off hand but the best way to start digging is to
look at the getDocumentSummaryInformation() method defined on yje
HWPFDocument class, these will likely take you to the POIDocument classes
javadoc and you can see all of the methods defined there. We are getting
ready for our Christmas holiday here - we take over a local castle
(honestly) and, with groups of colunteers, help to manage the surrounding
woodland - so I cannot promise much help in the near furture but I will try
to put some code together to see what I can do.

Yours

Mark B
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-write-the-metadata-into-the-document-tp3309193p3310302.html
Sent from the POI - User mailing list archive at Nabble.com.
Aram Mirzadeh
2010-12-18 11:10:33 UTC
Permalink
This is from my code for XSSF:

POIXMLProperties.ExtendedProperties props =
getProperties().getExtendedProperties();
props.getUnderlyingProperties().setApplication("© 2010 TranquilPhotos.com");

The props are very limited capabilities as far as I know though. For
example I have a lot of custom fields that I cannot update via the
system. I actually have to unzip the file, and edit the custom.xml file
externally and re-zip it back up. Then I use a AutoOpen() Macro inside
of the document to update the variables.

Aram
Post by Nan Lei
Hi all,
I am using POI excel module to write Excel dynamically, now I have a problem
about how to write metadata into the document I write.
For example, when I generate a Workbook, how to set the "Author" info into
it.
If I don't set it, the default one is "Apache POI", and I want customize it
myself.
So I want to know how to set the metadata.For HSSF*(deal with 2003) and
XSSF*(deal with 2007) may be different. So I want to know the full
solutions.
Best Wishes!
Nan Lei
Nan Lei
2010-12-18 11:31:11 UTC
Permalink
Hi Aram,

Thank you for your example code, and I can set it into Excel 2007.

But for 2003, I have to work on.

Many thanks for your help.

Best Wishes!
Nan Lei
Post by Aram Mirzadeh
POIXMLProperties.ExtendedProperties props =
getProperties().getExtendedProperties();
props.getUnderlyingProperties().setApplication("© 2010
TranquilPhotos.com");
The props are very limited capabilities as far as I know though. For
example I have a lot of custom fields that I cannot update via the system.
I actually have to unzip the file, and edit the custom.xml file externally
and re-zip it back up. Then I use a AutoOpen() Macro inside of the document
to update the variables.
Aram
Post by Nan Lei
Hi all,
I am using POI excel module to write Excel dynamically, now I have a problem
about how to write metadata into the document I write.
For example, when I generate a Workbook, how to set the "Author" info into
it.
If I don't set it, the default one is "Apache POI", and I want customize it
myself.
So I want to know how to set the metadata.For HSSF*(deal with 2003) and
XSSF*(deal with 2007) may be different. So I want to know the full
solutions.
Best Wishes!
Nan Lei
---------------------------------------------------------------------
Mark Beardsley
2010-12-18 12:29:39 UTC
Permalink
With thanks to Rainer - amongst many I am sure - setting the author using
HSSF is actually very straightforward;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hpsf.SummaryInformation;
import java.io.*;

/**
*
* @author win user
*/
public class DocSummaryInfo {

private HSSFWorkbook 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 DocSummaryInfo() {
HSSFSheet sheet = null;
HSSFRow row = null;
HSSFCell cell = null;
this.workbook = new HSSFWorkbook();
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.
*
* @param author An instance of the String class that encapuslates the
* name of the document's author.
*/
public void setAuthor(String author) {
// Not sure about this line. It may be possible to omit it but I
have not
// tested this out yet.
this.workbook.createInformationProperties();

// Simply get the object that manages the Summry Information for the
// document and then call the relevant method to set the suthor's
// name.
SummaryInformation summaryInfo =
this.workbook.getSummaryInformation();
summaryInfo.setAuthor(author);
}

/**
* 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 {
DocSummaryInfo dsi = new DocSummaryInfo();
dsi.setAuthor("Mark Beardsley");
dsi.saveWorkbook("C:/temp/props test.xls");
}
catch(Exception ex) {
System.out.println(ex.getClass().getName());
System.out.println(ex.getMessage());
ex.printStackTrace(System.out);
}
}
}

and the SummaryInformation class also allows you access to other properties
such as the documents title, etc.

Yours

Mark B
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-write-the-metadata-into-the-document-tp3309193p3310406.html
Sent from the POI - User mailing list archive at Nabble.com.
Nan Lei
2010-12-18 13:58:51 UTC
Permalink
Hi Mark,

Thank you for your sample code, I test it and it works.

The important line is this.workbook.createInformationProperties();

If has no this line, a java.lang.NullPointerException will be thrown, so I
think this line allow us to customerize the properties.

Many thanks for your help.

Have a good time of the Christmas week.

Best Wishes!
Nan Lei
Mark Beardsley
2010-12-18 15:40:01 UTC
Permalink
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.
Mark Beardsley
2010-12-18 13:53:08 UTC
Permalink
Hello Aram,

I have had a dig around and think that, in this case, the OP needs to call
the getCoreProperties() method of the POIXMLProperties class. That has
methods defined such as setCreator(String) which I am going to bet - and
that is bet as I have not tested anything - is the same as setAuthor() in
the hssf example I posted earlier.

Yours

Mark B
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-write-the-metadata-into-the-document-tp3309193p3310452.html
Sent from the POI - User mailing list archive at Nabble.com.
Aram Mirzadeh
2010-12-18 18:17:41 UTC
Permalink
You're 100% right, I had that somewhere else in my LIbrary jar and I
never realized it was getting called.
Post by Mark Beardsley
Hello Aram,
I have had a dig around and think that, in this case, the OP needs to call
the getCoreProperties() method of the POIXMLProperties class. That has
methods defined such as setCreator(String) which I am going to bet - and
that is bet as I have not tested anything - is the same as setAuthor() in
the hssf example I posted earlier.
Yours
Mark B
Nan Lei
2010-12-19 03:49:56 UTC
Permalink
Hi,

Another problem I met is that when I used the
sheet.setDefaultRowHeightInPoints(25f) method, when the document was
created, there is no any effect, the row height also 18f, not 25f, what's
wrong about this?

And if I set the Row Height per each row I created, there is no this
problem, so I think may be something I didn't set.

So could you give some help about the setting of default row height?

Best Wishes!
Nan Lei
Mark Beardsley
2010-12-19 12:29:31 UTC
Permalink
Sorry, no, I always set the height of individual rows and have never had the
need to call the default row height method. It might be that this is a bug
and so I think you should log it through bugzilla - or atthe lest post a
question to the dev list to see if anyone picke it up.

Sorry I cannot help.

Yours

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