mirror of https://github.com/apache/poi.git
Adjust many examples for Java 8: try-with-resource, multi-catch and other code cleanup.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1809354 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4ac6800974
commit
ad5232f8f8
|
@ -69,7 +69,7 @@ public class OOXMLPasswordsTry implements Closeable {
|
|||
|
||||
// Try each password in turn, reporting progress
|
||||
String valid = null;
|
||||
String password = null;
|
||||
String password;
|
||||
while ((password = r.readLine()) != null) {
|
||||
if (isValid(password)) {
|
||||
valid = password;
|
||||
|
|
|
@ -123,9 +123,9 @@ public class CopyCompare
|
|||
final CopyFile cf = new CopyFile(copyFileName);
|
||||
r.registerListener(cf);
|
||||
r.setNotifyEmptyDirectories(true);
|
||||
FileInputStream fis = new FileInputStream(originalFileName);
|
||||
try (FileInputStream fis = new FileInputStream(originalFileName)) {
|
||||
r.read(fis);
|
||||
fis.close();
|
||||
}
|
||||
|
||||
/* Write the new POIFS to disk. */
|
||||
cf.close();
|
||||
|
@ -183,7 +183,7 @@ public class CopyCompare
|
|||
for (final Entry e1 : d1) {
|
||||
final String n1 = e1.getName();
|
||||
if (!d2.hasEntry(n1)) {
|
||||
msg.append("Document \"" + n1 + "\" exists only in the source.\n");
|
||||
msg.append("Document \"").append(n1).append("\" exists only in the source.\n");
|
||||
equal = false;
|
||||
break;
|
||||
}
|
||||
|
@ -194,8 +194,7 @@ public class CopyCompare
|
|||
} else if (e1.isDocumentEntry() && e2.isDocumentEntry()) {
|
||||
equal = equal((DocumentEntry) e1, (DocumentEntry) e2, msg);
|
||||
} else {
|
||||
msg.append("One of \"" + e1 + "\" and \"" + e2 + "\" is a " +
|
||||
"document while the other one is a directory.\n");
|
||||
msg.append("One of \"").append(e1).append("\" and \"").append(e2).append("\" is a ").append("document while the other one is a directory.\n");
|
||||
equal = false;
|
||||
}
|
||||
}
|
||||
|
@ -208,8 +207,7 @@ public class CopyCompare
|
|||
try {
|
||||
e1 = d1.getEntry(n2);
|
||||
} catch (FileNotFoundException ex) {
|
||||
msg.append("Document \"" + e2 + "\" exitsts, document \"" +
|
||||
e1 + "\" does not.\n");
|
||||
msg.append("Document \"").append(e2).append("\" exitsts, document \"").append(e1).append("\" does not.\n");
|
||||
equal = false;
|
||||
break;
|
||||
}
|
||||
|
@ -243,9 +241,7 @@ public class CopyCompare
|
|||
throws NoPropertySetStreamException, MarkUnsupportedException,
|
||||
UnsupportedEncodingException, IOException
|
||||
{
|
||||
final DocumentInputStream dis1 = new DocumentInputStream(d1);
|
||||
final DocumentInputStream dis2 = new DocumentInputStream(d2);
|
||||
try {
|
||||
try (DocumentInputStream dis1 = new DocumentInputStream(d1); DocumentInputStream dis2 = new DocumentInputStream(d2)) {
|
||||
if (PropertySet.isPropertySetStream(dis1) &&
|
||||
PropertySet.isPropertySetStream(dis2)) {
|
||||
final PropertySet ps1 = PropertySetFactory.create(dis1);
|
||||
|
@ -265,9 +261,6 @@ public class CopyCompare
|
|||
}
|
||||
} while (i1 > -1);
|
||||
}
|
||||
} finally {
|
||||
dis2.close();
|
||||
dis1.close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -339,11 +332,7 @@ public class CopyCompare
|
|||
* copy it unmodified to the destination POIFS. */
|
||||
copy(poiFs, path, name, stream);
|
||||
}
|
||||
} catch (MarkUnsupportedException ex) {
|
||||
t = ex;
|
||||
} catch (IOException ex) {
|
||||
t = ex;
|
||||
} catch (WritingNotSupportedException ex) {
|
||||
} catch (MarkUnsupportedException | WritingNotSupportedException | IOException ex) {
|
||||
t = ex;
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ public class ReadTitle
|
|||
@Override
|
||||
public void processPOIFSReaderEvent(final POIFSReaderEvent event)
|
||||
{
|
||||
SummaryInformation si = null;
|
||||
SummaryInformation si;
|
||||
try
|
||||
{
|
||||
si = (SummaryInformation)
|
||||
|
|
|
@ -187,11 +187,7 @@ public class WriteAuthorAndTitle
|
|||
* copy it unmodified to the destination POIFS. */
|
||||
copy(poiFs, event.getPath(), event.getName(), stream);
|
||||
}
|
||||
} catch (MarkUnsupportedException ex) {
|
||||
t = ex;
|
||||
} catch (IOException ex) {
|
||||
t = ex;
|
||||
} catch (WritingNotSupportedException ex) {
|
||||
} catch (MarkUnsupportedException | WritingNotSupportedException | IOException ex) {
|
||||
t = ex;
|
||||
}
|
||||
|
||||
|
|
|
@ -79,8 +79,7 @@ public class WriteTitle
|
|||
ms.setProperty(p);
|
||||
|
||||
/* Create the POI file system the property set is to be written to. */
|
||||
final POIFSFileSystem poiFs = new POIFSFileSystem();
|
||||
|
||||
try (final POIFSFileSystem poiFs = new POIFSFileSystem()) {
|
||||
/* For writing the property set into a POI file system it has to be
|
||||
* handed over to the POIFS.createDocument() method as an input stream
|
||||
* which produces the bytes making out the property set stream. */
|
||||
|
@ -92,10 +91,9 @@ public class WriteTitle
|
|||
poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME);
|
||||
|
||||
/* Write the whole POI file system to a disk file. */
|
||||
FileOutputStream fos = new FileOutputStream(fileName);
|
||||
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
||||
poiFs.writeFilesystem(fos);
|
||||
fos.close();
|
||||
poiFs.close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ import org.apache.poi.sl.usermodel.VerticalAlignment;
|
|||
public final class ApacheconEU08 {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
SlideShow<?,?> ppt = new HSLFSlideShow();
|
||||
try (SlideShow<?,?> ppt = new HSLFSlideShow()) {
|
||||
// SlideShow<?,?> ppt = new XMLSlideShow();
|
||||
ppt.setPageSize(new Dimension(720, 540));
|
||||
|
||||
|
@ -69,10 +69,10 @@ public final class ApacheconEU08 {
|
|||
slide12(ppt);
|
||||
|
||||
String ext = ppt.getClass().getName().contains("HSLF") ? "ppt" : "pptx";
|
||||
FileOutputStream out = new FileOutputStream("apachecon_eu_08."+ext);
|
||||
try (FileOutputStream out = new FileOutputStream("apachecon_eu_08." + ext)) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void slide1(SlideShow<?,?> ppt) throws IOException {
|
||||
|
|
|
@ -32,9 +32,7 @@ import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
|
|||
public final class BulletsDemo {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
HSLFSlideShow ppt = new HSLFSlideShow();
|
||||
try {
|
||||
try (HSLFSlideShow ppt = new HSLFSlideShow()) {
|
||||
HSLFSlide slide = ppt.createSlide();
|
||||
|
||||
HSLFTextBox shape = new HSLFTextBox();
|
||||
|
@ -54,11 +52,9 @@ public final class BulletsDemo {
|
|||
shape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300)); //position of the text box in the slide
|
||||
slide.addShape(shape);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("bullets.ppt");
|
||||
try (FileOutputStream out = new FileOutputStream("bullets.ppt")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,9 +32,7 @@ import org.apache.poi.hslf.usermodel.HSLFTextBox;
|
|||
public abstract class CreateHyperlink {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSLFSlideShow ppt = new HSLFSlideShow();
|
||||
|
||||
try {
|
||||
try (HSLFSlideShow ppt = new HSLFSlideShow()) {
|
||||
HSLFSlide slideA = ppt.createSlide();
|
||||
ppt.createSlide();
|
||||
HSLFSlide slideC = ppt.createSlide();
|
||||
|
@ -56,11 +54,9 @@ public abstract class CreateHyperlink {
|
|||
HSLFHyperlink link2 = textBox2.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();
|
||||
link2.linkToSlide(slideC);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("hyperlink.ppt");
|
||||
try (FileOutputStream out = new FileOutputStream("hyperlink.ppt")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,10 +52,10 @@ public final class DataExtraction {
|
|||
|
||||
//extract all sound files embedded in this presentation
|
||||
HSLFSoundData[] sound = ppt.getSoundData();
|
||||
for (int i = 0; i < sound.length; i++) {
|
||||
String type = sound[i].getSoundType(); //*.wav
|
||||
String name = sound[i].getSoundName(); //typically file name
|
||||
byte[] data = sound[i].getData(); //raw bytes
|
||||
for (HSLFSoundData aSound : sound) {
|
||||
String type = aSound.getSoundType(); //*.wav
|
||||
String name = aSound.getSoundName(); //typically file name
|
||||
byte[] data = aSound.getData(); //raw bytes
|
||||
|
||||
//save the sound on disk
|
||||
FileOutputStream out = new FileOutputStream(name + type);
|
||||
|
|
|
@ -39,9 +39,7 @@ public final class Graphics2DDemo {
|
|||
* A simple bar chart demo
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
HSLFSlideShow ppt = new HSLFSlideShow();
|
||||
|
||||
try {
|
||||
try (HSLFSlideShow ppt = new HSLFSlideShow()) {
|
||||
//bar chart data. The first value is the bar color, the second is the width
|
||||
Object[] def = new Object[]{
|
||||
Color.yellow, 40,
|
||||
|
@ -54,21 +52,21 @@ public final class Graphics2DDemo {
|
|||
|
||||
HSLFGroupShape group = new HSLFGroupShape();
|
||||
//define position of the drawing in the slide
|
||||
Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
|
||||
Rectangle bounds = new Rectangle(200, 100, 350, 300);
|
||||
group.setAnchor(bounds);
|
||||
group.setInteriorAnchor(new java.awt.Rectangle(0, 0, 100, 100));
|
||||
group.setInteriorAnchor(new Rectangle(0, 0, 100, 100));
|
||||
slide.addShape(group);
|
||||
Graphics2D graphics = new PPGraphics2D(group);
|
||||
|
||||
//draw a simple bar graph
|
||||
int x = 10, y = 10;
|
||||
graphics.setFont(new Font("Arial", Font.BOLD, 10));
|
||||
for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {
|
||||
for (int i = 0, idx = 1; i < def.length; i += 2, idx++) {
|
||||
graphics.setColor(Color.black);
|
||||
int width = ((Integer)def[i+1]).intValue();
|
||||
graphics.drawString("Q" + idx, x-5, y+10);
|
||||
graphics.drawString(width + "%", x + width+3, y + 10);
|
||||
graphics.setColor((Color)def[i]);
|
||||
int width = ((Integer) def[i + 1]).intValue();
|
||||
graphics.drawString("Q" + idx, x - 5, y + 10);
|
||||
graphics.drawString(width + "%", x + width + 3, y + 10);
|
||||
graphics.setColor((Color) def[i]);
|
||||
graphics.fill(new Rectangle(x, y, width, 10));
|
||||
y += 15;
|
||||
}
|
||||
|
@ -77,11 +75,9 @@ public final class Graphics2DDemo {
|
|||
graphics.draw(group.getInteriorAnchor());
|
||||
graphics.drawString("Performance", x + 30, y + 10);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
|
||||
try (FileOutputStream out = new FileOutputStream("hslf-graphics.ppt")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,9 +27,7 @@ import org.apache.poi.hslf.usermodel.HSLFSlideShow;
|
|||
*/
|
||||
public abstract class HeadersFootersDemo {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSLFSlideShow ppt = new HSLFSlideShow();
|
||||
|
||||
try {
|
||||
try (HSLFSlideShow ppt = new HSLFSlideShow()) {
|
||||
HeadersFooters slideHeaders = ppt.getSlideHeadersFooters();
|
||||
slideHeaders.setFootersText("Created by POI-HSLF");
|
||||
slideHeaders.setSlideNumberVisible(true);
|
||||
|
@ -41,11 +39,9 @@ public abstract class HeadersFootersDemo {
|
|||
|
||||
ppt.createSlide();
|
||||
|
||||
FileOutputStream out = new FileOutputStream("headers_footers.ppt");
|
||||
try (FileOutputStream out = new FileOutputStream("headers_footers.ppt")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -37,8 +37,8 @@ import org.apache.poi.hslf.usermodel.HSLFTextRun;
|
|||
public final class Hyperlinks {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
FileInputStream is = new FileInputStream(args[i]);
|
||||
for (String arg : args) {
|
||||
FileInputStream is = new FileInputStream(arg);
|
||||
HSLFSlideShow ppt = new HSLFSlideShow(is);
|
||||
is.close();
|
||||
|
||||
|
@ -64,7 +64,7 @@ public final class Hyperlinks {
|
|||
System.out.println("- reading hyperlinks from the slide's shapes");
|
||||
for (HSLFShape sh : slide.getShapes()) {
|
||||
if (sh instanceof HSLFSimpleShape) {
|
||||
HSLFHyperlink link = ((HSLFSimpleShape)sh).getHyperlink();
|
||||
HSLFHyperlink link = ((HSLFSimpleShape) sh).getHyperlink();
|
||||
if (link != null) {
|
||||
System.out.println(toStr(link, null));
|
||||
}
|
||||
|
|
|
@ -30,23 +30,23 @@ import org.apache.poi.hslf.usermodel.HSLFSoundData;
|
|||
*/
|
||||
public class SoundFinder {
|
||||
public static void main(String[] args) throws IOException {
|
||||
FileInputStream fis = new FileInputStream(args[0]);
|
||||
HSLFSlideShow ppt = new HSLFSlideShow(fis);
|
||||
try (FileInputStream fis = new FileInputStream(args[0])) {
|
||||
try (HSLFSlideShow ppt = new HSLFSlideShow(fis)) {
|
||||
HSLFSoundData[] sounds = ppt.getSoundData();
|
||||
|
||||
for (HSLFSlide slide : ppt.getSlides()) {
|
||||
for (HSLFShape shape : slide.getShapes()) {
|
||||
int soundRef = getSoundReference(shape);
|
||||
if(soundRef == -1) continue;
|
||||
if (soundRef == -1) continue;
|
||||
|
||||
|
||||
System.out.println("Slide["+slide.getSlideNumber()+"], shape["+shape.getShapeId()+"], soundRef: "+soundRef);
|
||||
System.out.println("Slide[" + slide.getSlideNumber() + "], shape[" + shape.getShapeId() + "], soundRef: " + soundRef);
|
||||
System.out.println(" " + sounds[soundRef].getSoundName());
|
||||
System.out.println(" " + sounds[soundRef].getSoundType());
|
||||
}
|
||||
}
|
||||
ppt.close();
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -54,18 +54,14 @@ public final class TableDemo {
|
|||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
HSLFSlideShow ppt = new HSLFSlideShow();
|
||||
|
||||
try {
|
||||
try (HSLFSlideShow ppt = new HSLFSlideShow()) {
|
||||
HSLFSlide slide = ppt.createSlide();
|
||||
create1stTable(slide);
|
||||
create2ndTable(slide);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("hslf-table.ppt");
|
||||
try (FileOutputStream out = new FileOutputStream("hslf-table.ppt")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -157,12 +157,12 @@ public class Msg2txt {
|
|||
if(args.length <= 0) {
|
||||
System.err.println("No files names provided");
|
||||
} else {
|
||||
for(int i = 0; i < args.length; i++) {
|
||||
for (String arg : args) {
|
||||
try {
|
||||
Msg2txt processor = new Msg2txt(args[i]);
|
||||
Msg2txt processor = new Msg2txt(arg);
|
||||
processor.processMessage();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Could not process "+args[i]+": "+e);
|
||||
System.err.println("Could not process " + arg + ": " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -251,10 +251,10 @@ public class AddDimensionedImage {
|
|||
String imageFile, double reqImageWidthMM, double reqImageHeightMM,
|
||||
int resizeBehaviour) throws FileNotFoundException, IOException,
|
||||
IllegalArgumentException {
|
||||
HSSFClientAnchor anchor = null;
|
||||
HSSFPatriarch patriarch = null;
|
||||
ClientAnchorDetail rowClientAnchorDetail = null;
|
||||
ClientAnchorDetail colClientAnchorDetail = null;
|
||||
HSSFClientAnchor anchor;
|
||||
HSSFPatriarch patriarch;
|
||||
ClientAnchorDetail rowClientAnchorDetail;
|
||||
ClientAnchorDetail colClientAnchorDetail;
|
||||
|
||||
// Validate the resizeBehaviour parameter.
|
||||
if((resizeBehaviour != AddDimensionedImage.EXPAND_COLUMN) &&
|
||||
|
@ -334,9 +334,9 @@ public class AddDimensionedImage {
|
|||
private ClientAnchorDetail fitImageToColumns(HSSFSheet sheet, int colNumber,
|
||||
double reqImageWidthMM, int resizeBehaviour) {
|
||||
|
||||
double colWidthMM = 0.0D;
|
||||
double colCoordinatesPerMM = 0.0D;
|
||||
int pictureWidthCoordinates = 0;
|
||||
double colWidthMM;
|
||||
double colCoordinatesPerMM;
|
||||
int pictureWidthCoordinates;
|
||||
ClientAnchorDetail colClientAnchorDetail = null;
|
||||
|
||||
// Get the colum's width in millimetres
|
||||
|
@ -417,21 +417,19 @@ public class AddDimensionedImage {
|
|||
*/
|
||||
private ClientAnchorDetail fitImageToRows(HSSFSheet sheet, int rowNumber,
|
||||
double reqImageHeightMM, int resizeBehaviour) {
|
||||
HSSFRow row = null;
|
||||
double rowHeightMM = 0.0D;
|
||||
double rowCoordinatesPerMM = 0.0D;
|
||||
int pictureHeightCoordinates = 0;
|
||||
double rowCoordinatesPerMM;
|
||||
int pictureHeightCoordinates;
|
||||
ClientAnchorDetail rowClientAnchorDetail = null;
|
||||
|
||||
// Get the row and it's height
|
||||
row = sheet.getRow(rowNumber);
|
||||
HSSFRow row = sheet.getRow(rowNumber);
|
||||
if(row == null) {
|
||||
// Create row if it does not exist.
|
||||
row = sheet.createRow(rowNumber);
|
||||
}
|
||||
|
||||
// Get the row's height in millimetres
|
||||
rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;
|
||||
double rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;
|
||||
|
||||
// Check that the row's height will accomodate the image at the required
|
||||
// dimensions. If the height of the row is LESS than the required height
|
||||
|
@ -494,13 +492,13 @@ public class AddDimensionedImage {
|
|||
private ClientAnchorDetail calculateColumnLocation(HSSFSheet sheet,
|
||||
int startingColumn,
|
||||
double reqImageWidthMM) {
|
||||
ClientAnchorDetail anchorDetail = null;
|
||||
ClientAnchorDetail anchorDetail;
|
||||
double totalWidthMM = 0.0D;
|
||||
double colWidthMM = 0.0D;
|
||||
double overlapMM = 0.0D;
|
||||
double coordinatePositionsPerMM = 0.0D;
|
||||
double overlapMM;
|
||||
double coordinatePositionsPerMM;
|
||||
int toColumn = startingColumn;
|
||||
int inset = 0;
|
||||
int inset;
|
||||
|
||||
// Calculate how many columns the image will have to
|
||||
// span in order to be presented at the required size.
|
||||
|
@ -593,14 +591,14 @@ public class AddDimensionedImage {
|
|||
*/
|
||||
private ClientAnchorDetail calculateRowLocation(HSSFSheet sheet,
|
||||
int startingRow, double reqImageHeightMM) {
|
||||
ClientAnchorDetail clientAnchorDetail = null;
|
||||
HSSFRow row = null;
|
||||
ClientAnchorDetail clientAnchorDetail;
|
||||
HSSFRow row;
|
||||
double rowHeightMM = 0.0D;
|
||||
double totalRowHeightMM = 0.0D;
|
||||
double overlapMM = 0.0D;
|
||||
double rowCoordinatesPerMM = 0.0D;
|
||||
double overlapMM;
|
||||
double rowCoordinatesPerMM;
|
||||
int toRow = startingRow;
|
||||
int inset = 0;
|
||||
int inset;
|
||||
|
||||
// Step through the rows in the sheet and accumulate a total of their
|
||||
// heights.
|
||||
|
@ -672,10 +670,10 @@ public class AddDimensionedImage {
|
|||
* interrupted.
|
||||
*/
|
||||
private byte[] imageToBytes(String imageFilename) throws IOException {
|
||||
File imageFile = null;
|
||||
File imageFile;
|
||||
FileInputStream fis = null;
|
||||
ByteArrayOutputStream bos = null;
|
||||
int read = 0;
|
||||
ByteArrayOutputStream bos;
|
||||
int read;
|
||||
try {
|
||||
imageFile = new File(imageFilename);
|
||||
fis = new FileInputStream(imageFile);
|
||||
|
@ -718,10 +716,10 @@ public class AddDimensionedImage {
|
|||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String imageFile = null;
|
||||
String outputFile = null;
|
||||
String imageFile;
|
||||
String outputFile;
|
||||
FileOutputStream fos = null;
|
||||
HSSFSheet sheet = null;
|
||||
HSSFSheet sheet;
|
||||
try {
|
||||
if(args.length < 2){
|
||||
System.err.println("Usage: AddDimensionedImage imageFile outputFile");
|
||||
|
@ -730,25 +728,15 @@ public class AddDimensionedImage {
|
|||
imageFile = args[0];
|
||||
outputFile = args[1];
|
||||
|
||||
HSSFWorkbook workbook = new HSSFWorkbook();
|
||||
try {
|
||||
try (HSSFWorkbook workbook = new HSSFWorkbook()) {
|
||||
sheet = workbook.createSheet("Picture Test");
|
||||
new AddDimensionedImage().addImageToSheet("A1", sheet,
|
||||
imageFile, 125, 125,
|
||||
AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
|
||||
fos = new FileOutputStream(outputFile);
|
||||
workbook.write(fos);
|
||||
} finally {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
catch(FileNotFoundException fnfEx) {
|
||||
System.out.println("Caught an: " + fnfEx.getClass().getName());
|
||||
System.out.println("Message: " + fnfEx.getMessage());
|
||||
System.out.println("Stacktrace follows...........");
|
||||
fnfEx.printStackTrace(System.out);
|
||||
}
|
||||
catch(IOException ioEx) {
|
||||
} catch(IOException ioEx) {
|
||||
System.out.println("Caught an: " + ioEx.getClass().getName());
|
||||
System.out.println("Message: " + ioEx.getMessage());
|
||||
System.out.println("Stacktrace follows...........");
|
||||
|
|
|
@ -36,7 +36,7 @@ import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|||
*/
|
||||
public class Alignment {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
HSSFRow row = sheet.createRow(2);
|
||||
createCell(wb, row, 0, HorizontalAlignment.CENTER);
|
||||
|
@ -48,11 +48,10 @@ public class Alignment {
|
|||
createCell(wb, row, 6, HorizontalAlignment.RIGHT);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -39,8 +39,7 @@ public class BigExample {
|
|||
int rownum;
|
||||
|
||||
// create a new workbook
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try {
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
// create a new sheet
|
||||
HSSFSheet s = wb.createSheet();
|
||||
// declare a row object reference
|
||||
|
@ -88,21 +87,18 @@ public class BigExample {
|
|||
// set the sheet name to HSSF Test
|
||||
wb.setSheetName(0, "HSSF Test");
|
||||
// create a sheet with 300 rows (0-299)
|
||||
for (rownum = 0; rownum < 300; rownum++)
|
||||
{
|
||||
for (rownum = 0; rownum < 300; rownum++) {
|
||||
// create a row
|
||||
r = s.createRow(rownum);
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
if ((rownum % 2) == 0) {
|
||||
// make the row height bigger (in twips - 1/20 of a point)
|
||||
r.setHeight((short) 0x249);
|
||||
}
|
||||
|
||||
//r.setRowNum(( short ) rownum);
|
||||
// create 50 cells (0-49) (the += 2 becomes apparent later
|
||||
for (int cellnum = 0; cellnum < 50; cellnum += 2)
|
||||
{
|
||||
for (int cellnum = 0; cellnum < 50; cellnum += 2) {
|
||||
// create a numeric cell
|
||||
c = r.createCell(cellnum);
|
||||
// do some goofy math to demonstrate decimals
|
||||
|
@ -111,8 +107,7 @@ public class BigExample {
|
|||
+ ((double) cellnum / 10000)));
|
||||
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
if ((rownum % 2) == 0) {
|
||||
// set this cell to the first cell style we defined
|
||||
c.setCellStyle(cs);
|
||||
}
|
||||
|
@ -123,11 +118,10 @@ public class BigExample {
|
|||
// set the cell's string value to "TEST"
|
||||
c.setCellValue("TEST");
|
||||
// make this column a bit wider
|
||||
s.setColumnWidth(cellnum + 1, (int)((50 * 8) / ((double) 1 / 20)));
|
||||
s.setColumnWidth(cellnum + 1, (int) ((50 * 8) / ((double) 1 / 20)));
|
||||
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
if ((rownum % 2) == 0) {
|
||||
// set this to the white on red cell style
|
||||
// we defined above
|
||||
c.setCellStyle(cs2);
|
||||
|
@ -148,7 +142,7 @@ public class BigExample {
|
|||
cs3.setBorderBottom(BorderStyle.THICK);
|
||||
|
||||
//create 50 cells
|
||||
for (int cellnum =0; cellnum < 50; cellnum++) {
|
||||
for (int cellnum = 0; cellnum < 50; cellnum++) {
|
||||
//create a blank type cell (no value)
|
||||
c = r.createCell(cellnum);
|
||||
// set it to the thick black border style
|
||||
|
@ -166,14 +160,11 @@ public class BigExample {
|
|||
//end deleted sheet
|
||||
|
||||
// create a new file
|
||||
FileOutputStream out = new FileOutputStream("workbook.xls");
|
||||
|
||||
try (FileOutputStream out = new FileOutputStream("workbook.xls")) {
|
||||
// write the workbook to the output stream
|
||||
// close our file (don't blow out our file handles
|
||||
wb.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,8 +33,7 @@ import org.apache.poi.ss.usermodel.BorderStyle;
|
|||
*/
|
||||
public class Borders {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try {
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
|
@ -57,11 +56,9 @@ public class Borders {
|
|||
cell.setCellStyle(style);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,9 +39,7 @@ import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
|
|||
public class CellComments {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try {
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");
|
||||
|
||||
// Create the drawing patriarch. This is the top level container for all shapes including cell comments.
|
||||
|
@ -52,7 +50,7 @@ public class CellComments {
|
|||
cell1.setCellValue(new HSSFRichTextString("Hello, World"));
|
||||
|
||||
//anchor defines size and position of the comment in worksheet
|
||||
HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
|
||||
HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
|
||||
|
||||
// set text in the comment
|
||||
comment1.setString(new HSSFRichTextString("We can set comments in POI"));
|
||||
|
@ -69,7 +67,7 @@ public class CellComments {
|
|||
cell2.setCellValue(36.6);
|
||||
|
||||
|
||||
HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 8, (short) 6, 11));
|
||||
HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 8, (short) 6, 11));
|
||||
//modify background color of the comment
|
||||
comment2.setFillColor(204, 236, 255);
|
||||
|
||||
|
@ -78,7 +76,7 @@ public class CellComments {
|
|||
//apply custom font to the text in the comment
|
||||
HSSFFont font = wb.createFont();
|
||||
font.setFontName("Arial");
|
||||
font.setFontHeightInPoints((short)10);
|
||||
font.setFontHeightInPoints((short) 10);
|
||||
font.setBold(true);
|
||||
font.setColor(HSSFColorPredefined.RED.getIndex());
|
||||
string.applyFont(font);
|
||||
|
@ -88,7 +86,7 @@ public class CellComments {
|
|||
|
||||
comment2.setAuthor("Bill Gates");
|
||||
|
||||
/**
|
||||
/*
|
||||
* The second way to assign comment to a cell is to implicitly specify its row and column.
|
||||
* Note, it is possible to set row and column of a non-existing cell.
|
||||
* It works, the comment is visible.
|
||||
|
@ -96,11 +94,9 @@ public class CellComments {
|
|||
comment2.setRow(6);
|
||||
comment2.setColumn(1);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("poi_comment.xls");
|
||||
try (FileOutputStream out = new FileOutputStream("poi_comment.xls")) {
|
||||
wb.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,8 +28,7 @@ import org.apache.poi.ss.usermodel.CellType;
|
|||
|
||||
public class CellTypes {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try {
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
HSSFRow row = sheet.createRow(2);
|
||||
row.createCell(0).setCellValue(1.1);
|
||||
|
@ -39,11 +38,9 @@ public class CellTypes {
|
|||
row.createCell(4).setCellType(CellType.ERROR);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import java.io.IOException;
|
|||
*/
|
||||
public class CreateCells {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
|
@ -47,10 +47,9 @@ public class CreateCells {
|
|||
row.createCell(3).setCellValue(true);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|||
*/
|
||||
public class CreateDateCells {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
|
@ -54,10 +54,9 @@ public class CreateDateCells {
|
|||
cell.setCellStyle(cellStyle);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ public class EmbeddedObjects {
|
|||
@SuppressWarnings("unused")
|
||||
public static void main(String[] args) throws Exception {
|
||||
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(args[0]));
|
||||
HSSFWorkbook workbook = new HSSFWorkbook(fs);
|
||||
try (HSSFWorkbook workbook = new HSSFWorkbook(fs)) {
|
||||
for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
|
||||
//the OLE2 Class Name of the object
|
||||
String oleName = obj.getOLE2ClassName();
|
||||
|
@ -47,7 +47,7 @@ public class EmbeddedObjects {
|
|||
} else if (oleName.equals("Presentation")) {
|
||||
document = new HSLFSlideShow(dn);
|
||||
} else {
|
||||
if(dn != null){
|
||||
if (dn != null) {
|
||||
// The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is
|
||||
for (Entry entry : dn) {
|
||||
String name = entry.getName();
|
||||
|
@ -62,6 +62,6 @@ public class EmbeddedObjects {
|
|||
document.close();
|
||||
}
|
||||
}
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,9 +31,7 @@ import java.io.InputStream;
|
|||
/**
|
||||
* This example shows how to use the event API for reading a file.
|
||||
*/
|
||||
public class EventExample
|
||||
implements HSSFListener
|
||||
{
|
||||
public class EventExample implements HSSFListener {
|
||||
private SSTRecord sstrec;
|
||||
|
||||
/**
|
||||
|
@ -97,11 +95,11 @@ public class EventExample
|
|||
{
|
||||
// create a new file input stream with the input file specified
|
||||
// at the command line
|
||||
FileInputStream fin = new FileInputStream(args[0]);
|
||||
try (FileInputStream fin = new FileInputStream(args[0])) {
|
||||
// create a new org.apache.poi.poifs.filesystem.Filesystem
|
||||
POIFSFileSystem poifs = new POIFSFileSystem(fin);
|
||||
try (POIFSFileSystem poifs = new POIFSFileSystem(fin)) {
|
||||
// get the Workbook (excel part) stream in a InputStream
|
||||
InputStream din = poifs.createDocumentInputStream("Workbook");
|
||||
try (InputStream din = poifs.createDocumentInputStream("Workbook")) {
|
||||
// construct out HSSFRequest object
|
||||
HSSFRequest req = new HSSFRequest();
|
||||
// lazy listen for ALL records with the listener shown above
|
||||
|
@ -110,11 +108,9 @@ public class EventExample
|
|||
HSSFEventFactory factory = new HSSFEventFactory();
|
||||
// process our events based on the document input stream
|
||||
factory.processEvents(req, din);
|
||||
// once all the events are processed close our file input stream
|
||||
fin.close();
|
||||
// and our document input stream (don't want to leak these!)
|
||||
din.close();
|
||||
poifs.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("done.");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import org.apache.poi.ss.usermodel.FillPatternType;
|
|||
*/
|
||||
public class FrillsAndFills {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
|
@ -56,10 +56,9 @@ public class FrillsAndFills {
|
|||
cell.setCellStyle(style);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,11 +47,8 @@ public final class HSSFReadWrite {
|
|||
* creates an {@link HSSFWorkbook} with the specified OS filename.
|
||||
*/
|
||||
private static HSSFWorkbook readFile(String filename) throws IOException {
|
||||
FileInputStream fis = new FileInputStream(filename);
|
||||
try {
|
||||
try (FileInputStream fis = new FileInputStream(filename)) {
|
||||
return new HSSFWorkbook(fis); // NOSONAR - should not be closed here
|
||||
} finally {
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,8 +57,7 @@ public final class HSSFReadWrite {
|
|||
* rows/cells.
|
||||
*/
|
||||
private static void testCreateSampleSheet(String outputFilename) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try {
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet s = wb.createSheet();
|
||||
HSSFCellStyle cs = wb.createCellStyle();
|
||||
HSSFCellStyle cs2 = wb.createCellStyle();
|
||||
|
@ -125,14 +121,9 @@ public final class HSSFReadWrite {
|
|||
wb.removeSheetAt(1);
|
||||
|
||||
// end deleted sheet
|
||||
FileOutputStream out = new FileOutputStream(outputFilename);
|
||||
try {
|
||||
try (FileOutputStream out = new FileOutputStream(outputFilename)) {
|
||||
wb.write(out);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,9 +157,7 @@ public final class HSSFReadWrite {
|
|||
try {
|
||||
if (args.length < 2) {
|
||||
|
||||
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
|
||||
|
||||
try {
|
||||
try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) {
|
||||
System.out.println("Data dump:\n");
|
||||
|
||||
for (int k = 0; k < wb.getNumberOfSheets(); k++) {
|
||||
|
@ -187,7 +176,7 @@ public final class HSSFReadWrite {
|
|||
HSSFCell cell = row.getCell(c);
|
||||
String value;
|
||||
|
||||
if(cell != null) {
|
||||
if (cell != null) {
|
||||
switch (cell.getCellType()) {
|
||||
|
||||
case FORMULA:
|
||||
|
@ -223,8 +212,6 @@ public final class HSSFReadWrite {
|
|||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
} else if (args.length == 2) {
|
||||
if (args[1].toLowerCase(Locale.ROOT).equals("write")) {
|
||||
|
@ -236,23 +223,16 @@ public final class HSSFReadWrite {
|
|||
+ " ms generation time");
|
||||
} else {
|
||||
System.out.println("readwrite test");
|
||||
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
|
||||
try {
|
||||
FileOutputStream stream = new FileOutputStream(args[1]);
|
||||
try {
|
||||
try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) {
|
||||
try (FileOutputStream stream = new FileOutputStream(args[1])) {
|
||||
wb.write(stream);
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
} else if (args.length == 3 && args[2].equalsIgnoreCase("modify1")) {
|
||||
// delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
|
||||
|
||||
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
|
||||
try {
|
||||
try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) {
|
||||
HSSFSheet sheet = wb.getSheetAt(0);
|
||||
|
||||
for (int k = 0; k < 25; k++) {
|
||||
|
@ -269,14 +249,9 @@ public final class HSSFReadWrite {
|
|||
HSSFCell cell = row.getCell(3);
|
||||
cell.setCellValue("MODIFIED CELL!!!!!");
|
||||
|
||||
FileOutputStream stream = new FileOutputStream(args[1]);
|
||||
try {
|
||||
try (FileOutputStream stream = new FileOutputStream(args[1])) {
|
||||
wb.write(stream);
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -31,7 +31,7 @@ import org.apache.poi.ss.usermodel.CellType;
|
|||
*/
|
||||
public class HyperlinkFormula {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
HSSFRow row = sheet.createRow(0);
|
||||
|
||||
|
@ -39,9 +39,9 @@ public class HyperlinkFormula {
|
|||
cell.setCellType(CellType.FORMULA);
|
||||
cell.setCellFormula("HYPERLINK(\"http://127.0.0.1:8080/toto/truc/index.html?test=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\", \"test\")");
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ import org.apache.poi.ss.usermodel.Font;
|
|||
public class Hyperlinks {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFCreationHelper helper = wb.getCreationHelper();
|
||||
|
||||
//cell style for hyperlinks
|
||||
|
@ -89,9 +89,9 @@ public class Hyperlinks {
|
|||
cell.setHyperlink(link);
|
||||
cell.setCellStyle(hlink_style);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("hssf-links.xls");
|
||||
try (FileOutputStream out = new FileOutputStream("hssf-links.xls")) {
|
||||
wb.write(out);
|
||||
out.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,8 +67,7 @@ public class InCellLists {
|
|||
* the Excel spreadsheet file this code will create.
|
||||
*/
|
||||
public void demonstrateMethodCalls(String outputFilename) throws IOException {
|
||||
HSSFWorkbook workbook = new HSSFWorkbook();
|
||||
try {
|
||||
try (HSSFWorkbook workbook = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = workbook.createSheet("In Cell Lists");
|
||||
HSSFRow row = sheet.createRow(0);
|
||||
|
||||
|
@ -89,7 +88,7 @@ public class InCellLists {
|
|||
this.listInCell(workbook, listItems, cell);
|
||||
// The row height and cell width are set here to ensure that the
|
||||
// list may be seen.
|
||||
row.setHeight((short)1100);
|
||||
row.setHeight((short) 1100);
|
||||
sheet.setColumnWidth(0, 9500);
|
||||
|
||||
// Create a cell at A3 and insert a numbered list into that cell.
|
||||
|
@ -100,7 +99,7 @@ public class InCellLists {
|
|||
listItems.add("List Item Five.");
|
||||
listItems.add("List Item Six.");
|
||||
this.numberedListInCell(workbook, listItems, cell, 1, 2);
|
||||
row.setHeight((short)1550);
|
||||
row.setHeight((short) 1550);
|
||||
|
||||
// Create a cell at A4 and insert a numbered list into that cell.
|
||||
// Note that a couple of items have been added to the listItems
|
||||
|
@ -112,7 +111,7 @@ public class InCellLists {
|
|||
listItems.add("List Item Nine.");
|
||||
listItems.add("List Item Ten.");
|
||||
this.bulletedListInCell(workbook, listItems, cell);
|
||||
row.setHeight((short)2550);
|
||||
row.setHeight((short) 2550);
|
||||
|
||||
// Insert a plain, multi-level list into cell A5. Note that
|
||||
// the major difference here is that the list items are passed as
|
||||
|
@ -143,7 +142,7 @@ public class InCellLists {
|
|||
listItems.add("ML List Item Four - Sub Item Three.");
|
||||
multiLevelListItems.add(new MultiLevelListItem("List Item Four.", listItems));
|
||||
this.multiLevelListInCell(workbook, multiLevelListItems, cell);
|
||||
row.setHeight((short)2800);
|
||||
row.setHeight((short) 2800);
|
||||
|
||||
// Insert a numbered multi-level list into cell A6. Note that the
|
||||
// same ArrayList as constructed for the above plain multi-level
|
||||
|
@ -152,7 +151,7 @@ public class InCellLists {
|
|||
cell = row.createCell(0);
|
||||
this.multiLevelNumberedListInCell(workbook, multiLevelListItems,
|
||||
cell, 1, 1, 1, 2);
|
||||
row.setHeight((short)2800);
|
||||
row.setHeight((short) 2800);
|
||||
|
||||
// Insert a numbered multi-level list into cell A7. Note that the
|
||||
// same ArrayList as constructed for the plain multi-level list
|
||||
|
@ -160,31 +159,18 @@ public class InCellLists {
|
|||
row = sheet.createRow(6);
|
||||
cell = row.createCell(0);
|
||||
this.multiLevelBulletedListInCell(workbook, multiLevelListItems, cell);
|
||||
row.setHeight((short)2800);
|
||||
row.setHeight((short) 2800);
|
||||
|
||||
// Save the completed workbook
|
||||
FileOutputStream fos = new FileOutputStream(new File(outputFilename));
|
||||
try {
|
||||
try (FileOutputStream fos = new FileOutputStream(new File(outputFilename))) {
|
||||
workbook.write(fos);
|
||||
} finally {
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
catch(FileNotFoundException fnfEx) {
|
||||
System.out.println("Caught a: " + fnfEx.getClass().getName());
|
||||
System.out.println("Message: " + fnfEx.getMessage());
|
||||
System.out.println("Stacktrace follows...........");
|
||||
fnfEx.printStackTrace(System.out);
|
||||
}
|
||||
catch(IOException ioEx) {
|
||||
} catch (IOException ioEx) {
|
||||
System.out.println("Caught a: " + ioEx.getClass().getName());
|
||||
System.out.println("Message: " + ioEx.getMessage());
|
||||
System.out.println("Stacktrace follows...........");
|
||||
ioEx.printStackTrace(System.out);
|
||||
}
|
||||
finally {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,7 +31,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
|
|||
*/
|
||||
public class MergedCells {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
HSSFRow row = sheet.createRow(1);
|
||||
|
@ -41,9 +41,9 @@ public class MergedCells {
|
|||
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,31 +33,27 @@ import org.apache.poi.ss.usermodel.CellType;
|
|||
*/
|
||||
public class NewLinesInCells {
|
||||
public static void main( String[] args ) throws IOException {
|
||||
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet s = wb.createSheet();
|
||||
HSSFRow r = null;
|
||||
HSSFCell c = null;
|
||||
HSSFCellStyle cs = wb.createCellStyle();
|
||||
HSSFFont f2 = wb.createFont();
|
||||
|
||||
cs = wb.createCellStyle();
|
||||
HSSFCellStyle cs = wb.createCellStyle();
|
||||
|
||||
cs.setFont(f2);
|
||||
// Word Wrap MUST be turned on
|
||||
cs.setWrapText(true);
|
||||
|
||||
r = s.createRow(2);
|
||||
HSSFRow r = s.createRow(2);
|
||||
r.setHeight((short) 0x349);
|
||||
c = r.createCell(2);
|
||||
HSSFCell c = r.createCell(2);
|
||||
c.setCellType(CellType.STRING);
|
||||
c.setCellValue("Use \n with word wrap on to create a new line");
|
||||
c.setCellStyle(cs);
|
||||
s.setColumnWidth(2, (int) ((50 * 8) / ((double) 1 / 20)));
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,16 +28,17 @@ import org.apache.poi.ss.util.WorkbookUtil;
|
|||
*/
|
||||
public abstract class NewSheet {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
wb.createSheet("new sheet");
|
||||
// create with default name
|
||||
wb.createSheet();
|
||||
final String name = "second sheet";
|
||||
// setting sheet name later
|
||||
wb.setSheetName(1, WorkbookUtil.createSafeSheetName(name));
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,10 +29,10 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|||
*/
|
||||
public class NewWorkbook {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ import java.io.*;
|
|||
public class OfficeDrawing {
|
||||
public static void main(String[] args) throws IOException {
|
||||
// Create the workbook and sheets.
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet1 = wb.createSheet("new sheet");
|
||||
HSSFSheet sheet2 = wb.createSheet("second sheet");
|
||||
HSSFSheet sheet3 = wb.createSheet("third sheet");
|
||||
|
@ -38,16 +38,17 @@ public class OfficeDrawing {
|
|||
HSSFSheet sheet5 = wb.createSheet("fifth sheet");
|
||||
|
||||
// Draw stuff in them
|
||||
drawSheet1( sheet1 );
|
||||
drawSheet2( sheet2 );
|
||||
drawSheet3( sheet3 );
|
||||
drawSheet4( sheet4, wb );
|
||||
drawSheet5( sheet5, wb );
|
||||
drawSheet1(sheet1);
|
||||
drawSheet2(sheet2);
|
||||
drawSheet3(sheet3);
|
||||
drawSheet4(sheet4, wb);
|
||||
drawSheet5(sheet5, wb);
|
||||
|
||||
// Write the file out.
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void drawSheet1( HSSFSheet sheet1 )
|
||||
|
|
|
@ -32,8 +32,8 @@ public class OfficeDrawingWithGraphics {
|
|||
public static void main( String[] args ) throws IOException {
|
||||
// Create a workbook with one sheet and size the first three somewhat
|
||||
// larger so we can fit the chemical structure diagram in.
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
HSSFSheet sheet = wb.createSheet( "my drawing" );
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("my drawing");
|
||||
sheet.setColumnWidth(1, 256 * 27);
|
||||
HSSFRow row1 = sheet.createRow(0);
|
||||
row1.setHeightInPoints(10 * 15f);
|
||||
|
@ -56,27 +56,27 @@ public class OfficeDrawingWithGraphics {
|
|||
EscherGraphics g;
|
||||
EscherGraphics2d g2d;
|
||||
// Anchor entirely within one cell.
|
||||
a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
|
||||
group = patriarch.createGroup( a );
|
||||
group.setCoordinates( 0, 0, 320, 276 );
|
||||
a = new HSSFClientAnchor(0, 0, 1023, 255, (short) 1, 0, (short) 1, 0);
|
||||
group = patriarch.createGroup(a);
|
||||
group.setCoordinates(0, 0, 320, 276);
|
||||
float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / Math.abs(group.getY2() - group.getY1());
|
||||
g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
|
||||
g2d = new EscherGraphics2d( g );
|
||||
drawStar( g2d );
|
||||
g = new EscherGraphics(group, wb, Color.black, verticalPointsPerPixel);
|
||||
g2d = new EscherGraphics2d(g);
|
||||
drawStar(g2d);
|
||||
|
||||
a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 1, (short) 1, 1 );
|
||||
group = patriarch.createGroup( a );
|
||||
group.setCoordinates( 0, 0, 640, 276 );
|
||||
a = new HSSFClientAnchor(0, 0, 1023, 255, (short) 1, 1, (short) 1, 1);
|
||||
group = patriarch.createGroup(a);
|
||||
group.setCoordinates(0, 0, 640, 276);
|
||||
verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / Math.abs(group.getY2() - group.getY1());
|
||||
// verticalPixelsPerPoint = (float)Math.abs(group.getY2() - group.getY1()) / a.getAnchorHeightInPoints(sheet);
|
||||
g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
|
||||
g2d = new EscherGraphics2d( g );
|
||||
drawStar( g2d );
|
||||
g = new EscherGraphics(group, wb, Color.black, verticalPointsPerPixel);
|
||||
g2d = new EscherGraphics2d(g);
|
||||
drawStar(g2d);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream out = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(out);
|
||||
out.close();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void drawStar( EscherGraphics2d g2d )
|
||||
|
|
|
@ -37,14 +37,11 @@ public class Outlines implements Closeable {
|
|||
throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
POILogger LOGGER = POILogFactory.getLogger(Outlines.class);
|
||||
for (int i=1; i<=13; i++) {
|
||||
Outlines o = new Outlines();
|
||||
try {
|
||||
String log = (String)Outlines.class.getDeclaredMethod("test"+i).invoke(o);
|
||||
String filename = "outline"+i+".xls";
|
||||
try (Outlines o = new Outlines()) {
|
||||
String log = (String) Outlines.class.getDeclaredMethod("test" + i).invoke(o);
|
||||
String filename = "outline" + i + ".xls";
|
||||
o.writeOut(filename);
|
||||
LOGGER.log(POILogger.INFO, filename+" written. "+log);
|
||||
} finally {
|
||||
o.close();
|
||||
LOGGER.log(POILogger.INFO, filename + " written. " + log);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,11 +50,8 @@ public class Outlines implements Closeable {
|
|||
private final HSSFSheet sheet1 = wb.createSheet("new sheet");
|
||||
|
||||
public void writeOut(String filename) throws IOException {
|
||||
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||
try {
|
||||
try (FileOutputStream fileOut = new FileOutputStream(filename)) {
|
||||
wb.write(fileOut);
|
||||
} finally {
|
||||
fileOut.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -36,12 +36,7 @@ import org.apache.poi.ss.usermodel.CellType;
|
|||
*/
|
||||
public class ReadWriteWorkbook {
|
||||
public static void main(String[] args) throws IOException {
|
||||
FileInputStream fileIn = null;
|
||||
FileOutputStream fileOut = null;
|
||||
|
||||
try
|
||||
{
|
||||
fileIn = new FileInputStream("workbook.xls");
|
||||
try (FileInputStream fileIn = new FileInputStream("workbook.xls")) {
|
||||
POIFSFileSystem fs = new POIFSFileSystem(fileIn);
|
||||
HSSFWorkbook wb = new HSSFWorkbook(fs);
|
||||
HSSFSheet sheet = wb.getSheetAt(0);
|
||||
|
@ -55,13 +50,9 @@ public class ReadWriteWorkbook {
|
|||
cell.setCellValue("a test");
|
||||
|
||||
// Write the output to a file
|
||||
fileOut = new FileOutputStream("workbookout.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbookout.xls")) {
|
||||
wb.write(fileOut);
|
||||
} finally {
|
||||
if (fileOut != null)
|
||||
fileOut.close();
|
||||
if (fileIn != null)
|
||||
fileIn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,13 +30,13 @@ import org.apache.poi.ss.util.CellRangeAddress;
|
|||
|
||||
public class RepeatingRowsAndColumns {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet1 = wb.createSheet("first sheet");
|
||||
HSSFSheet sheet2 = wb.createSheet("second sheet");
|
||||
HSSFSheet sheet3 = wb.createSheet("third sheet");
|
||||
|
||||
HSSFFont boldFont = wb.createFont();
|
||||
boldFont.setFontHeightInPoints((short)22);
|
||||
boldFont.setFontHeightInPoints((short) 22);
|
||||
boldFont.setBold(true);
|
||||
|
||||
HSSFCellStyle boldStyle = wb.createCellStyle();
|
||||
|
@ -56,9 +56,9 @@ public class RepeatingRowsAndColumns {
|
|||
sheet3.setRepeatingColumns(cra);
|
||||
sheet3.setRepeatingRows(cra);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,24 +28,24 @@ import org.apache.poi.ss.usermodel.Sheet;
|
|||
|
||||
public class SplitAndFreezePanes {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet1 = wb.createSheet("new sheet");
|
||||
HSSFSheet sheet2 = wb.createSheet("second sheet");
|
||||
HSSFSheet sheet3 = wb.createSheet("third sheet");
|
||||
HSSFSheet sheet4 = wb.createSheet("fourth sheet");
|
||||
|
||||
// Freeze just one row
|
||||
sheet1.createFreezePane( 0, 1, 0, 1 );
|
||||
sheet1.createFreezePane(0, 1, 0, 1);
|
||||
// Freeze just one column
|
||||
sheet2.createFreezePane( 1, 0, 1, 0 );
|
||||
sheet2.createFreezePane(1, 0, 1, 0);
|
||||
// Freeze the columns and rows (forget about scrolling position of the lower right quadrant).
|
||||
sheet3.createFreezePane( 2, 2 );
|
||||
sheet3.createFreezePane(2, 2);
|
||||
// Create a split with the lower left side being the active quadrant
|
||||
sheet4.createSplitPane( 2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT );
|
||||
sheet4.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|||
*/
|
||||
public class WorkingWithFonts {
|
||||
public static void main(String[] args) throws IOException {
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
|
@ -40,7 +40,7 @@ public class WorkingWithFonts {
|
|||
|
||||
// Create a new font and alter it.
|
||||
HSSFFont font = wb.createFont();
|
||||
font.setFontHeightInPoints((short)24);
|
||||
font.setFontHeightInPoints((short) 24);
|
||||
font.setFontName("Courier New");
|
||||
font.setItalic(true);
|
||||
font.setStrikeout(true);
|
||||
|
@ -55,9 +55,9 @@ public class WorkingWithFonts {
|
|||
cell.setCellStyle(style);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,15 +32,14 @@ import java.io.FileOutputStream;
|
|||
*/
|
||||
public class ZoomSheet
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws IOException
|
||||
{
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
public static void main(String[] args) throws IOException {
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet1 = wb.createSheet("new sheet");
|
||||
sheet1.setZoom(75); // 75 percent magnification
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
|
||||
try (FileOutputStream fileOut = new FileOutputStream("workbook.xls")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -216,13 +216,9 @@ public final class Word2Forrest
|
|||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
InputStream is = new FileInputStream(args[0]);
|
||||
OutputStream out = new FileOutputStream("test.xml");
|
||||
try {
|
||||
try (InputStream is = new FileInputStream(args[0]);
|
||||
OutputStream out = new FileOutputStream("test.xml")) {
|
||||
new Word2Forrest(new HWPFDocument(is), out);
|
||||
} finally {
|
||||
out.close();
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class AligningCells {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
|
||||
Sheet sheet = wb.createSheet();
|
||||
Row row = sheet.createRow(2);
|
||||
|
@ -48,11 +48,10 @@ public class AligningCells {
|
|||
createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
|
||||
|
||||
// Write the output to a file
|
||||
OutputStream fileOut = new FileOutputStream("ss-example-align.xlsx");
|
||||
try (OutputStream fileOut = new FileOutputStream("ss-example-align.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -51,16 +51,16 @@ public class CalendarDemo {
|
|||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
boolean xlsx = true;
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if(args[i].charAt(0) == '-'){
|
||||
xlsx = args[i].equals("-xlsx");
|
||||
for (String arg : args) {
|
||||
if (arg.charAt(0) == '-') {
|
||||
xlsx = arg.equals("-xlsx");
|
||||
} else {
|
||||
calendar.set(Calendar.YEAR, Integer.parseInt(args[i]));
|
||||
calendar.set(Calendar.YEAR, Integer.parseInt(arg));
|
||||
}
|
||||
}
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
|
||||
Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook();
|
||||
try (Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook()) {
|
||||
|
||||
Map<String, CellStyle> styles = createStyles(wb);
|
||||
|
||||
|
@ -80,8 +80,8 @@ public class CalendarDemo {
|
|||
|
||||
//the following three statements are required only for HSSF
|
||||
sheet.setAutobreaks(true);
|
||||
printSetup.setFitHeight((short)1);
|
||||
printSetup.setFitWidth((short)1);
|
||||
printSetup.setFitHeight((short) 1);
|
||||
printSetup.setFitWidth((short) 1);
|
||||
|
||||
//the header row: centered text in 48pt font
|
||||
Row headerRow = sheet.createRow(0);
|
||||
|
@ -95,29 +95,29 @@ public class CalendarDemo {
|
|||
Row monthRow = sheet.createRow(1);
|
||||
for (int i = 0; i < days.length; i++) {
|
||||
//set column widths, the width is measured in units of 1/256th of a character width
|
||||
sheet.setColumnWidth(i*2, 5*256); //the column is 5 characters wide
|
||||
sheet.setColumnWidth(i*2 + 1, 13*256); //the column is 13 characters wide
|
||||
sheet.addMergedRegion(new CellRangeAddress(1, 1, i*2, i*2+1));
|
||||
Cell monthCell = monthRow.createCell(i*2);
|
||||
sheet.setColumnWidth(i * 2, 5 * 256); //the column is 5 characters wide
|
||||
sheet.setColumnWidth(i * 2 + 1, 13 * 256); //the column is 13 characters wide
|
||||
sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2, i * 2 + 1));
|
||||
Cell monthCell = monthRow.createCell(i * 2);
|
||||
monthCell.setCellValue(days[i]);
|
||||
monthCell.setCellStyle(styles.get("month"));
|
||||
}
|
||||
|
||||
int cnt = 1, day=1;
|
||||
int cnt = 1, day = 1;
|
||||
int rownum = 2;
|
||||
for (int j = 0; j < 6; j++) {
|
||||
Row row = sheet.createRow(rownum++);
|
||||
row.setHeightInPoints(100);
|
||||
for (int i = 0; i < days.length; i++) {
|
||||
Cell dayCell_1 = row.createCell(i*2);
|
||||
Cell dayCell_2 = row.createCell(i*2 + 1);
|
||||
Cell dayCell_1 = row.createCell(i * 2);
|
||||
Cell dayCell_2 = row.createCell(i * 2 + 1);
|
||||
|
||||
int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
|
||||
if(cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
|
||||
if (cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
|
||||
dayCell_1.setCellValue(day);
|
||||
calendar.set(Calendar.DAY_OF_MONTH, ++day);
|
||||
|
||||
if(i == 0 || i == days.length-1) {
|
||||
if (i == 0 || i == days.length - 1) {
|
||||
dayCell_1.setCellStyle(styles.get("weekend_left"));
|
||||
dayCell_2.setCellStyle(styles.get("weekend_right"));
|
||||
} else {
|
||||
|
@ -130,18 +130,18 @@ public class CalendarDemo {
|
|||
}
|
||||
cnt++;
|
||||
}
|
||||
if(calendar.get(Calendar.MONTH) > month) break;
|
||||
if (calendar.get(Calendar.MONTH) > month) break;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the output to a file
|
||||
String file = "calendar.xls";
|
||||
if(wb instanceof XSSFWorkbook) file += "x";
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
wb.write(out);
|
||||
out.close();
|
||||
if (wb instanceof XSSFWorkbook) file += "x";
|
||||
|
||||
wb.close();
|
||||
try (FileOutputStream out = new FileOutputStream(file)) {
|
||||
wb.write(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -43,17 +43,17 @@ public class CellStyleDetails {
|
|||
throw new IllegalArgumentException("Filename must be given");
|
||||
}
|
||||
|
||||
Workbook wb = WorkbookFactory.create(new File(args[0]));
|
||||
try (Workbook wb = WorkbookFactory.create(new File(args[0]))) {
|
||||
DataFormatter formatter = new DataFormatter();
|
||||
|
||||
for(int sn=0; sn<wb.getNumberOfSheets(); sn++) {
|
||||
for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
|
||||
Sheet sheet = wb.getSheetAt(sn);
|
||||
System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());
|
||||
|
||||
for(Row row : sheet) {
|
||||
for (Row row : sheet) {
|
||||
System.out.println(" Row " + row.getRowNum());
|
||||
|
||||
for(Cell cell : row) {
|
||||
for (Cell cell : row) {
|
||||
CellReference ref = new CellReference(cell);
|
||||
System.out.print(" " + ref.formatAsString());
|
||||
System.out.print(" (" + cell.getColumnIndex() + ") ");
|
||||
|
@ -63,14 +63,14 @@ public class CellStyleDetails {
|
|||
System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
|
||||
System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
|
||||
|
||||
Font font = wb.getFontAt( style.getFontIndex() );
|
||||
Font font = wb.getFontAt(style.getFontIndex());
|
||||
System.out.print("Font=" + font.getFontName() + " ");
|
||||
System.out.print("FontColor=");
|
||||
if(font instanceof HSSFFont) {
|
||||
System.out.print( renderColor( ((HSSFFont)font).getHSSFColor((HSSFWorkbook)wb)) );
|
||||
if (font instanceof HSSFFont) {
|
||||
System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
|
||||
}
|
||||
if(font instanceof XSSFFont) {
|
||||
System.out.print( renderColor( ((XSSFFont)font).getXSSFColor()) );
|
||||
if (font instanceof XSSFFont) {
|
||||
System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
@ -80,8 +80,7 @@ public class CellStyleDetails {
|
|||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static String renderColor(Color color) {
|
||||
|
|
|
@ -74,7 +74,7 @@ public class SettingExternalFunction {
|
|||
|
||||
public static void main( String[] args ) throws IOException {
|
||||
|
||||
Workbook wb = new XSSFWorkbook(); // or new HSSFWorkbook()
|
||||
try (Workbook wb = new XSSFWorkbook()) { // or new HSSFWorkbook()
|
||||
|
||||
// register the add-in
|
||||
wb.addToolPack(new BloombergAddIn());
|
||||
|
@ -85,11 +85,9 @@ public class SettingExternalFunction {
|
|||
row.createCell(1).setCellFormula("BDH(\"goog us equity\",\"EBIT\",\"1/1/2005\",\"12/31/2009\",\"per=cy\",\"curr=USD\") ");
|
||||
row.createCell(2).setCellFormula("BDS(\"goog us equity\",\"top_20_holders_public_filings\") ");
|
||||
|
||||
FileOutputStream out = new FileOutputStream("bloomberg-demo.xlsx");
|
||||
try (FileOutputStream out = new FileOutputStream("bloomberg-demo.xlsx")) {
|
||||
wb.write(out);
|
||||
out.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.apache.poi.ss.examples.formula;
|
||||
|
||||
import java.io.File ;
|
||||
import java.io.FileInputStream ;
|
||||
|
||||
import org.apache.poi.ss.formula.functions.FreeRefFunction ;
|
||||
import org.apache.poi.ss.formula.udf.DefaultUDFFinder ;
|
||||
|
@ -51,32 +50,28 @@ public class UserDefinedFunctionExample {
|
|||
|
||||
File workbookFile = new File( args[0] ) ;
|
||||
|
||||
Workbook workbook = WorkbookFactory.create(workbookFile, null, true);
|
||||
try (Workbook workbook = WorkbookFactory.create(workbookFile, null, true)) {
|
||||
String[] functionNames = {"calculatePayment"};
|
||||
FreeRefFunction[] functionImpls = {new CalculateMortgage()};
|
||||
|
||||
try {
|
||||
String[] functionNames = { "calculatePayment" } ;
|
||||
FreeRefFunction[] functionImpls = { new CalculateMortgage() } ;
|
||||
|
||||
UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ;
|
||||
UDFFinder udfToolpack = new DefaultUDFFinder(functionNames, functionImpls);
|
||||
|
||||
// register the user-defined function in the workbook
|
||||
workbook.addToolPack(udfToolpack);
|
||||
|
||||
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
|
||||
|
||||
CellReference cr = new CellReference( args[1] ) ;
|
||||
String sheetName = cr.getSheetName() ;
|
||||
Sheet sheet = workbook.getSheet( sheetName ) ;
|
||||
int rowIdx = cr.getRow() ;
|
||||
int colIdx = cr.getCol() ;
|
||||
Row row = sheet.getRow( rowIdx ) ;
|
||||
Cell cell = row.getCell( colIdx ) ;
|
||||
CellReference cr = new CellReference(args[1]);
|
||||
String sheetName = cr.getSheetName();
|
||||
Sheet sheet = workbook.getSheet(sheetName);
|
||||
int rowIdx = cr.getRow();
|
||||
int colIdx = cr.getCol();
|
||||
Row row = sheet.getRow(rowIdx);
|
||||
Cell cell = row.getCell(colIdx);
|
||||
|
||||
CellValue value = evaluator.evaluate( cell ) ;
|
||||
CellValue value = evaluator.evaluate(cell);
|
||||
|
||||
System.out.println("returns value: " + value ) ;
|
||||
} finally {
|
||||
workbook.close();
|
||||
System.out.println("returns value: " + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ public final class DataExtraction {
|
|||
}
|
||||
|
||||
FileInputStream is = new FileInputStream(args[0]);
|
||||
XMLSlideShow ppt = new XMLSlideShow(is);
|
||||
try (XMLSlideShow ppt = new XMLSlideShow(is)) {
|
||||
is.close();
|
||||
|
||||
// Get the document's embedded files.
|
||||
|
@ -51,7 +51,7 @@ public final class DataExtraction {
|
|||
String type = p.getContentType();
|
||||
// typically file name
|
||||
String name = p.getPartName().getName();
|
||||
out.println("Embedded file ("+type+"): "+name);
|
||||
out.println("Embedded file (" + type + "): " + name);
|
||||
|
||||
InputStream pIs = p.getInputStream();
|
||||
// make sense of the part data
|
||||
|
@ -63,7 +63,7 @@ public final class DataExtraction {
|
|||
for (XSLFPictureData data : ppt.getPictureData()) {
|
||||
String type = data.getContentType();
|
||||
String name = data.getFileName();
|
||||
out.println("Picture ("+type+"): "+name);
|
||||
out.println("Picture (" + type + "): " + name);
|
||||
|
||||
InputStream pIs = data.getInputStream();
|
||||
// make sense of the image data
|
||||
|
@ -72,15 +72,15 @@ public final class DataExtraction {
|
|||
|
||||
// size of the canvas in points
|
||||
Dimension pageSize = ppt.getPageSize();
|
||||
out.println("Pagesize: "+pageSize);
|
||||
out.println("Pagesize: " + pageSize);
|
||||
|
||||
for(XSLFSlide slide : ppt.getSlides()) {
|
||||
for(XSLFShape shape : slide){
|
||||
if(shape instanceof XSLFTextShape) {
|
||||
XSLFTextShape txShape = (XSLFTextShape)shape;
|
||||
for (XSLFSlide slide : ppt.getSlides()) {
|
||||
for (XSLFShape shape : slide) {
|
||||
if (shape instanceof XSLFTextShape) {
|
||||
XSLFTextShape txShape = (XSLFTextShape) shape;
|
||||
out.println(txShape.getText());
|
||||
} else if (shape instanceof XSLFPictureShape){
|
||||
XSLFPictureShape pShape = (XSLFPictureShape)shape;
|
||||
} else if (shape instanceof XSLFPictureShape) {
|
||||
XSLFPictureShape pShape = (XSLFPictureShape) shape;
|
||||
XSLFPictureData pData = pShape.getPictureData();
|
||||
out.println(pData.getFileName());
|
||||
} else {
|
||||
|
@ -88,8 +88,7 @@ public final class DataExtraction {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,26 +28,22 @@ import java.io.FileOutputStream;
|
|||
public final class MergePresentations {
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
for (String arg : args){
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
for (String arg : args) {
|
||||
FileInputStream is = new FileInputStream(arg);
|
||||
XMLSlideShow src = new XMLSlideShow(is);
|
||||
is.close();
|
||||
|
||||
for(XSLFSlide srcSlide : src.getSlides()){
|
||||
for (XSLFSlide srcSlide : src.getSlides()) {
|
||||
ppt.createSlide().importContent(srcSlide);
|
||||
}
|
||||
|
||||
src.close();
|
||||
}
|
||||
|
||||
FileOutputStream out = new FileOutputStream("merged.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("merged.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,29 +62,26 @@ public class PieChartDemo {
|
|||
return;
|
||||
}
|
||||
|
||||
BufferedReader modelReader = new BufferedReader(new FileReader(args[1]));
|
||||
XMLSlideShow pptx = null;
|
||||
try {
|
||||
try (BufferedReader modelReader = new BufferedReader(new FileReader(args[1]))) {
|
||||
String chartTitle = modelReader.readLine(); // first line is chart title
|
||||
|
||||
pptx = new XMLSlideShow(new FileInputStream(args[0]));
|
||||
try (XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(args[0]))) {
|
||||
XSLFSlide slide = pptx.getSlides().get(0);
|
||||
|
||||
// find chart in the slide
|
||||
XSLFChart chart = null;
|
||||
for(POIXMLDocumentPart part : slide.getRelations()){
|
||||
if(part instanceof XSLFChart){
|
||||
for (POIXMLDocumentPart part : slide.getRelations()) {
|
||||
if (part instanceof XSLFChart) {
|
||||
chart = (XSLFChart) part;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(chart == null) throw new IllegalStateException("chart not found in the template");
|
||||
if (chart == null) throw new IllegalStateException("chart not found in the template");
|
||||
|
||||
// embedded Excel workbook that holds the chart data
|
||||
POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
try {
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = wb.createSheet();
|
||||
|
||||
CTChart ctChart = chart.getCTChart();
|
||||
|
@ -116,7 +113,7 @@ public class PieChartDemo {
|
|||
int idx = 0;
|
||||
int rownum = 1;
|
||||
String ln;
|
||||
while((ln = modelReader.readLine()) != null){
|
||||
while ((ln = modelReader.readLine()) != null) {
|
||||
String[] vals = ln.split("\\s+");
|
||||
CTNumVal numVal = numData.addNewPt();
|
||||
numVal.setIdx(idx);
|
||||
|
@ -134,32 +131,22 @@ public class PieChartDemo {
|
|||
numData.getPtCount().setVal(idx);
|
||||
strData.getPtCount().setVal(idx);
|
||||
|
||||
String numDataRange = new CellRangeAddress(1, rownum-1, 1, 1).formatAsString(sheet.getSheetName(), true);
|
||||
String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
|
||||
val.getNumRef().setF(numDataRange);
|
||||
String axisDataRange = new CellRangeAddress(1, rownum-1, 0, 0).formatAsString(sheet.getSheetName(), true);
|
||||
String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
|
||||
cat.getStrRef().setF(axisDataRange);
|
||||
|
||||
// updated the embedded workbook with the data
|
||||
OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
|
||||
try {
|
||||
try (OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream()) {
|
||||
wb.write(xlsOut);
|
||||
} finally {
|
||||
xlsOut.close();
|
||||
}
|
||||
|
||||
// save the result
|
||||
OutputStream out = new FileOutputStream("pie-chart-demo-output.pptx");
|
||||
try {
|
||||
try (OutputStream out = new FileOutputStream("pie-chart-demo-output.pptx")) {
|
||||
pptx.write(out);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
} finally {
|
||||
if (pptx != null) pptx.close();
|
||||
modelReader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,17 +29,16 @@ import java.io.IOException;
|
|||
public class Tutorial1 {
|
||||
|
||||
public static void main(String[] args) throws IOException{
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
// XSLFSlide#createSlide() with no arguments creates a blank slide
|
||||
/*XSLFSlide blankSlide =*/ ppt.createSlide();
|
||||
/*XSLFSlide blankSlide =*/
|
||||
ppt.createSlide();
|
||||
|
||||
|
||||
XSLFSlideMaster master = ppt.getSlideMasters().get(0);
|
||||
|
||||
XSLFSlideLayout layout1 = master.getLayout(SlideLayout.TITLE);
|
||||
XSLFSlide slide1 = ppt.createSlide(layout1) ;
|
||||
XSLFSlide slide1 = ppt.createSlide(layout1);
|
||||
XSLFTextShape[] ph1 = slide1.getPlaceholders();
|
||||
XSLFTextShape titlePlaceholder1 = ph1[0];
|
||||
titlePlaceholder1.setText("This is a title");
|
||||
|
@ -47,7 +46,7 @@ public class Tutorial1 {
|
|||
subtitlePlaceholder1.setText("this is a subtitle");
|
||||
|
||||
XSLFSlideLayout layout2 = master.getLayout(SlideLayout.TITLE_AND_CONTENT);
|
||||
XSLFSlide slide2 = ppt.createSlide(layout2) ;
|
||||
XSLFSlide slide2 = ppt.createSlide(layout2);
|
||||
XSLFTextShape[] ph2 = slide2.getPlaceholders();
|
||||
XSLFTextShape titlePlaceholder2 = ph2[0];
|
||||
titlePlaceholder2.setText("This is a title");
|
||||
|
@ -64,11 +63,9 @@ public class Tutorial1 {
|
|||
p3.setIndentLevel(2);
|
||||
p3.addNewTextRun().setText("Level3 text");
|
||||
|
||||
FileOutputStream out = new FileOutputStream("slides.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("slides.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,9 +30,7 @@ import java.io.IOException;
|
|||
public class Tutorial2 {
|
||||
|
||||
public static void main(String[] args) throws IOException{
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
XSLFSlide slide1 = ppt.createSlide();
|
||||
XSLFTextBox shape1 = slide1.createTextBox();
|
||||
// initial height of the text box is 100 pt but
|
||||
|
@ -75,11 +73,9 @@ public class Tutorial2 {
|
|||
// resize the shape to fit text
|
||||
shape1.resizeToFitText();
|
||||
|
||||
FileOutputStream out = new FileOutputStream("text.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("text.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,9 +31,7 @@ import org.apache.poi.sl.usermodel.Placeholder;
|
|||
public class Tutorial3 {
|
||||
|
||||
public static void main(String[] args) throws IOException{
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
XSLFSlide slide = ppt.createSlide();
|
||||
|
||||
XSLFTextShape titleShape = slide.createTextBox();
|
||||
|
@ -41,11 +39,9 @@ public class Tutorial3 {
|
|||
titleShape.setText("This is a slide title");
|
||||
titleShape.setAnchor(new Rectangle(50, 50, 400, 100));
|
||||
|
||||
FileOutputStream out = new FileOutputStream("title.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("title.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,9 +33,7 @@ import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
|
|||
public class Tutorial4 {
|
||||
|
||||
public static void main(String[] args) throws IOException{
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
// XSLFSlide#createSlide() with no arguments creates a blank slide
|
||||
XSLFSlide slide = ppt.createSlide();
|
||||
|
||||
|
@ -47,12 +45,12 @@ public class Tutorial4 {
|
|||
XSLFTableRow headerRow = tbl.addRow();
|
||||
headerRow.setHeight(50);
|
||||
// header
|
||||
for(int i = 0; i < numColumns; i++) {
|
||||
for (int i = 0; i < numColumns; i++) {
|
||||
XSLFTableCell th = headerRow.addCell();
|
||||
XSLFTextParagraph p = th.addNewTextParagraph();
|
||||
p.setTextAlign(TextAlign.CENTER);
|
||||
XSLFTextRun r = p.addNewTextRun();
|
||||
r.setText("Header " + (i+1));
|
||||
r.setText("Header " + (i + 1));
|
||||
r.setBold(true);
|
||||
r.setFontColor(Color.white);
|
||||
th.setFillColor(new Color(79, 129, 189));
|
||||
|
@ -64,17 +62,17 @@ public class Tutorial4 {
|
|||
|
||||
// rows
|
||||
|
||||
for(int rownum = 0; rownum < numRows; rownum ++){
|
||||
for (int rownum = 0; rownum < numRows; rownum++) {
|
||||
XSLFTableRow tr = tbl.addRow();
|
||||
tr.setHeight(50);
|
||||
// header
|
||||
for(int i = 0; i < numColumns; i++) {
|
||||
for (int i = 0; i < numColumns; i++) {
|
||||
XSLFTableCell cell = tr.addCell();
|
||||
XSLFTextParagraph p = cell.addNewTextParagraph();
|
||||
XSLFTextRun r = p.addNewTextRun();
|
||||
|
||||
r.setText("Cell " + (i+1));
|
||||
if(rownum % 2 == 0)
|
||||
r.setText("Cell " + (i + 1));
|
||||
if (rownum % 2 == 0)
|
||||
cell.setFillColor(new Color(208, 216, 232));
|
||||
else
|
||||
cell.setFillColor(new Color(233, 247, 244));
|
||||
|
@ -82,11 +80,9 @@ public class Tutorial4 {
|
|||
}
|
||||
}
|
||||
|
||||
FileOutputStream out = new FileOutputStream("table.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("table.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,21 +31,18 @@ import org.apache.poi.sl.usermodel.PictureData.PictureType;
|
|||
public class Tutorial5 {
|
||||
|
||||
public static void main(String[] args) throws IOException{
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
XSLFSlide slide = ppt.createSlide();
|
||||
|
||||
File img = new File(System.getProperty("POI.testdata.path", "test-data"), "slideshow/clock.jpg");
|
||||
XSLFPictureData pictureData = ppt.addPicture(img, PictureType.PNG);
|
||||
|
||||
/*XSLFPictureShape shape =*/ slide.createPicture(pictureData);
|
||||
/*XSLFPictureShape shape =*/
|
||||
slide.createPicture(pictureData);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("images.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("images.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,9 +29,7 @@ import java.io.IOException;
|
|||
public class Tutorial6 {
|
||||
|
||||
public static void main(String[] args) throws IOException{
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
XSLFSlide slide1 = ppt.createSlide();
|
||||
XSLFSlide slide2 = ppt.createSlide();
|
||||
|
||||
|
@ -50,12 +48,9 @@ public class Tutorial6 {
|
|||
link2.linkToSlide(slide2); // link address
|
||||
|
||||
|
||||
|
||||
FileOutputStream out = new FileOutputStream("hyperlinks.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("hyperlinks.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,9 +32,7 @@ import org.apache.poi.sl.usermodel.AutoNumberingScheme;
|
|||
public class Tutorial7 {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
XSLFSlide slide = ppt.createSlide();
|
||||
XSLFTextBox shape = slide.createTextBox();
|
||||
shape.setAnchor(new Rectangle(50, 50, 400, 200));
|
||||
|
@ -80,11 +78,9 @@ public class Tutorial7 {
|
|||
|
||||
shape.resizeToFitText();
|
||||
|
||||
FileOutputStream out = new FileOutputStream("list.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("list.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,18 +40,18 @@ public class Step1 {
|
|||
}
|
||||
|
||||
FileInputStream fis = new FileInputStream(args[0]);
|
||||
XMLSlideShow ppt = new XMLSlideShow(fis);
|
||||
try (XMLSlideShow ppt = new XMLSlideShow(fis)) {
|
||||
fis.close();
|
||||
|
||||
for(XSLFSlide slide : ppt.getSlides()){
|
||||
for (XSLFSlide slide : ppt.getSlides()) {
|
||||
System.out.println("Title: " + slide.getTitle());
|
||||
|
||||
for(XSLFShape shape : slide.getShapes()){
|
||||
if(shape instanceof XSLFTextShape) {
|
||||
XSLFTextShape tsh = (XSLFTextShape)shape;
|
||||
for(XSLFTextParagraph p : tsh){
|
||||
for (XSLFShape shape : slide.getShapes()) {
|
||||
if (shape instanceof XSLFTextShape) {
|
||||
XSLFTextShape tsh = (XSLFTextShape) shape;
|
||||
for (XSLFTextParagraph p : tsh) {
|
||||
System.out.println("Paragraph level: " + p.getIndentLevel());
|
||||
for(XSLFTextRun r : p){
|
||||
for (XSLFTextRun r : p) {
|
||||
System.out.println(r.getRawText());
|
||||
System.out.println(" bold: " + r.isBold());
|
||||
System.out.println(" italic: " + r.isItalic());
|
||||
|
@ -64,7 +64,6 @@ public class Step1 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,18 +33,19 @@ import java.io.FileOutputStream;
|
|||
*/
|
||||
public class Step2 {
|
||||
public static void main(String[] args) throws Exception{
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
|
||||
// first see what slide layouts are available by default
|
||||
System.out.println("Available slide layouts:");
|
||||
for(XSLFSlideMaster master : ppt.getSlideMasters()){
|
||||
for(XSLFSlideLayout layout : master.getSlideLayouts()){
|
||||
for (XSLFSlideMaster master : ppt.getSlideMasters()) {
|
||||
for (XSLFSlideLayout layout : master.getSlideLayouts()) {
|
||||
System.out.println(layout.getType());
|
||||
}
|
||||
}
|
||||
|
||||
// blank slide
|
||||
/*XSLFSlide blankSlide =*/ ppt.createSlide();
|
||||
/*XSLFSlide blankSlide =*/
|
||||
ppt.createSlide();
|
||||
|
||||
XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
|
||||
|
||||
|
@ -68,10 +69,9 @@ public class Step2 {
|
|||
body2.addNewTextParagraph().addNewTextRun().setText("Third paragraph");
|
||||
|
||||
|
||||
|
||||
FileOutputStream out = new FileOutputStream("step2.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("step2.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,8 +42,7 @@ import org.xml.sax.helpers.XMLReaderFactory;
|
|||
*/
|
||||
public class FromHowTo {
|
||||
public void processFirstSheet(String filename) throws Exception {
|
||||
OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
|
||||
try {
|
||||
try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) {
|
||||
XSSFReader r = new XSSFReader(pkg);
|
||||
SharedStringsTable sst = r.getSharedStringsTable();
|
||||
|
||||
|
@ -54,14 +53,11 @@ public class FromHowTo {
|
|||
InputSource sheetSource = new InputSource(sheet2);
|
||||
parser.parse(sheetSource);
|
||||
sheet2.close();
|
||||
} finally {
|
||||
pkg.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void processAllSheets(String filename) throws Exception {
|
||||
OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
|
||||
try {
|
||||
try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) {
|
||||
XSSFReader r = new XSSFReader(pkg);
|
||||
SharedStringsTable sst = r.getSharedStringsTable();
|
||||
|
||||
|
@ -76,8 +72,6 @@ public class FromHowTo {
|
|||
sheet.close();
|
||||
System.out.println("");
|
||||
}
|
||||
} finally {
|
||||
pkg.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ public class Outlining {
|
|||
}
|
||||
|
||||
private void collapseRow() throws IOException {
|
||||
SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
|
||||
try (SXSSFWorkbook wb2 = new SXSSFWorkbook(100)) {
|
||||
SXSSFSheet sheet2 = wb2.createSheet("new sheet");
|
||||
|
||||
int rowCount = 20;
|
||||
|
@ -44,13 +44,11 @@ public class Outlining {
|
|||
|
||||
sheet2.setRowGroupCollapsed(4, true);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx");
|
||||
try {
|
||||
try (FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx")) {
|
||||
wb2.write(fileOut);
|
||||
} finally {
|
||||
fileOut.close();
|
||||
wb2.dispose();
|
||||
wb2.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl;
|
|||
public class AligningCells {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
|
||||
XSSFSheet sheet = wb.createSheet();
|
||||
XSSFRow row = sheet.createRow(2);
|
||||
|
@ -68,11 +68,10 @@ public class AligningCells {
|
|||
centerAcrossSelection(wb, row, 1, 3, VerticalAlignment.CENTER);
|
||||
|
||||
// Write the output to a file
|
||||
OutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
|
||||
try (OutputStream fileOut = new FileOutputStream("xssf-align.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -86,7 +86,7 @@ public class BigGridDemo {
|
|||
// Step 1. Create a template file. Setup sheets and workbook-level objects such as
|
||||
// cell styles, number formats, etc.
|
||||
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = wb.createSheet("Big Grid");
|
||||
|
||||
Map<String, XSSFCellStyle> styles = createStyles(wb);
|
||||
|
@ -105,11 +105,10 @@ public class BigGridDemo {
|
|||
fw.close();
|
||||
|
||||
//Step 3. Substitute the template entry with the generated data
|
||||
FileOutputStream out = new FileOutputStream("big-grid.xlsx");
|
||||
try (FileOutputStream out = new FileOutputStream("big-grid.xlsx")) {
|
||||
substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out);
|
||||
out.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -194,28 +193,23 @@ public class BigGridDemo {
|
|||
* @param out the stream to write the result to
|
||||
*/
|
||||
private static void substitute(File zipfile, File tmpfile, String entry, OutputStream out) throws IOException {
|
||||
ZipFile zip = ZipHelper.openZipFile(zipfile);
|
||||
try {
|
||||
ZipOutputStream zos = new ZipOutputStream(out);
|
||||
|
||||
try (ZipFile zip = ZipHelper.openZipFile(zipfile)) {
|
||||
try (ZipOutputStream zos = new ZipOutputStream(out)) {
|
||||
Enumeration<? extends ZipEntry> en = zip.entries();
|
||||
while (en.hasMoreElements()) {
|
||||
ZipEntry ze = en.nextElement();
|
||||
if(!ze.getName().equals(entry)){
|
||||
if (!ze.getName().equals(entry)) {
|
||||
zos.putNextEntry(new ZipEntry(ze.getName()));
|
||||
InputStream is = zip.getInputStream(ze);
|
||||
try (InputStream is = zip.getInputStream(ze)) {
|
||||
copyStream(is, zos);
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
zos.putNextEntry(new ZipEntry(entry));
|
||||
InputStream is = new FileInputStream(tmpfile);
|
||||
try (InputStream is = new FileInputStream(tmpfile)) {
|
||||
copyStream(is, zos);
|
||||
is.close();
|
||||
|
||||
zos.close();
|
||||
} finally {
|
||||
zip.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ public class CalendarDemo {
|
|||
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
Map<String, XSSFCellStyle> styles = createStyles(wb);
|
||||
|
||||
for (int month = 0; month < 12; month++) {
|
||||
|
@ -84,29 +84,29 @@ public class CalendarDemo {
|
|||
XSSFRow monthRow = sheet.createRow(1);
|
||||
for (int i = 0; i < days.length; i++) {
|
||||
//for compatibility with HSSF we have to set column width in units of 1/256th of a character width
|
||||
sheet.setColumnWidth(i*2, 5*256); //the column is 5 characters wide
|
||||
sheet.setColumnWidth(i*2 + 1, 13*256); //the column is 13 characters wide
|
||||
sheet.addMergedRegion(new CellRangeAddress(1, 1, i*2, i*2+1));
|
||||
XSSFCell monthCell = monthRow.createCell(i*2);
|
||||
sheet.setColumnWidth(i * 2, 5 * 256); //the column is 5 characters wide
|
||||
sheet.setColumnWidth(i * 2 + 1, 13 * 256); //the column is 13 characters wide
|
||||
sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2, i * 2 + 1));
|
||||
XSSFCell monthCell = monthRow.createCell(i * 2);
|
||||
monthCell.setCellValue(days[i]);
|
||||
monthCell.setCellStyle(styles.get("month"));
|
||||
}
|
||||
|
||||
int cnt = 1, day=1;
|
||||
int cnt = 1, day = 1;
|
||||
int rownum = 2;
|
||||
for (int j = 0; j < 6; j++) {
|
||||
XSSFRow row = sheet.createRow(rownum++);
|
||||
row.setHeightInPoints(100);
|
||||
for (int i = 0; i < days.length; i++) {
|
||||
XSSFCell dayCell_1 = row.createCell(i*2);
|
||||
XSSFCell dayCell_2 = row.createCell(i*2 + 1);
|
||||
XSSFCell dayCell_1 = row.createCell(i * 2);
|
||||
XSSFCell dayCell_2 = row.createCell(i * 2 + 1);
|
||||
|
||||
int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
|
||||
if(cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
|
||||
if (cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
|
||||
dayCell_1.setCellValue(day);
|
||||
calendar.set(Calendar.DAY_OF_MONTH, ++day);
|
||||
|
||||
if(i == 0 || i == days.length-1) {
|
||||
if (i == 0 || i == days.length - 1) {
|
||||
dayCell_1.setCellStyle(styles.get("weekend_left"));
|
||||
dayCell_2.setCellStyle(styles.get("weekend_right"));
|
||||
} else {
|
||||
|
@ -119,16 +119,16 @@ public class CalendarDemo {
|
|||
}
|
||||
cnt++;
|
||||
}
|
||||
if(calendar.get(Calendar.MONTH) > month) break;
|
||||
if (calendar.get(Calendar.MONTH) > month) break;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream out = new FileOutputStream("calendar-"+year+".xlsx");
|
||||
try (FileOutputStream out = new FileOutputStream("calendar-" + year + ".xlsx")) {
|
||||
wb.write(out);
|
||||
out.close();
|
||||
}
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -44,7 +44,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
*/
|
||||
public class CellComments {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
|
||||
CreationHelper factory = wb.getCreationHelper();
|
||||
|
||||
|
@ -71,7 +71,7 @@ public class CellComments {
|
|||
//apply custom font to the text in the comment
|
||||
Font font = wb.createFont();
|
||||
font.setFontName("Arial");
|
||||
font.setFontHeightInPoints((short)14);
|
||||
font.setFontHeightInPoints((short) 14);
|
||||
font.setBold(true);
|
||||
font.setColor(IndexedColors.RED.getIndex());
|
||||
str2.applyFont(font);
|
||||
|
@ -80,11 +80,9 @@ public class CellComments {
|
|||
comment2.setAuthor("Apache POI");
|
||||
comment2.setAddress(new CellAddress("C3"));
|
||||
|
||||
String fname = "comments.xlsx";
|
||||
FileOutputStream out = new FileOutputStream(fname);
|
||||
try (FileOutputStream out = new FileOutputStream("comments.xlsx")) {
|
||||
wb.write(out);
|
||||
out.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,16 +36,15 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
*/
|
||||
public class CreateCell {
|
||||
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
public static void main(String[] args) throws IOException {
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
CreationHelper creationHelper = wb.getCreationHelper();
|
||||
Sheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
Row row = sheet.createRow((short)0);
|
||||
Row row = sheet.createRow((short) 0);
|
||||
// Create a cell and put a value in it.
|
||||
Cell cell = row.createCell((short)0);
|
||||
Cell cell = row.createCell((short) 0);
|
||||
cell.setCellValue(1);
|
||||
|
||||
//numeric value
|
||||
|
@ -81,9 +80,9 @@ public class CreateCell {
|
|||
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class CreatePivotTable {
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException {
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = wb.createSheet();
|
||||
|
||||
//Create some data to build the pivot table on
|
||||
|
@ -55,10 +55,10 @@ public class CreatePivotTable {
|
|||
//Add filter on forth column
|
||||
pivotTable.addReportFilter(3);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void setCellData(XSSFSheet sheet){
|
||||
|
|
|
@ -38,7 +38,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class CreatePivotTable2 {
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException {
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = wb.createSheet();
|
||||
|
||||
//Create some data to build the pivot table on
|
||||
|
@ -60,10 +60,10 @@ public class CreatePivotTable2 {
|
|||
//Add filter on forth column
|
||||
pivotTable.addReportFilter(4);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable2.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable2.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void setCellData(XSSFSheet sheet){
|
||||
|
|
|
@ -36,7 +36,7 @@ public class CreateTable {
|
|||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
Workbook wb = new XSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = (XSSFSheet) wb.createSheet();
|
||||
|
||||
// Create
|
||||
|
@ -49,7 +49,7 @@ public class CreateTable {
|
|||
table.getCTTable().getTableStyleInfo().setName("TableStyleMedium2");
|
||||
|
||||
// Style the table
|
||||
XSSFTableStyleInfo style = (XSSFTableStyleInfo)table.getStyle();
|
||||
XSSFTableStyleInfo style = (XSSFTableStyleInfo) table.getStyle();
|
||||
style.setName("TableStyleMedium2");
|
||||
style.setShowColumnStripes(false);
|
||||
style.setShowRowStripes(true);
|
||||
|
@ -61,16 +61,16 @@ public class CreateTable {
|
|||
// Set the values for the table
|
||||
XSSFRow row;
|
||||
XSSFCell cell;
|
||||
for(int i=0; i<3; i++) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
// Create row
|
||||
row = sheet.createRow(i);
|
||||
for(int j=0; j<3; j++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
// Create cell
|
||||
cell = row.createCell(j);
|
||||
if(i == 0) {
|
||||
cell.setCellValue("Column"+(j+1));
|
||||
if (i == 0) {
|
||||
cell.setCellValue("Column" + (j + 1));
|
||||
} else {
|
||||
cell.setCellValue((i+1)*(j+1));
|
||||
cell.setCellValue((i + 1) * (j + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -85,9 +85,9 @@ public class CreateTable {
|
|||
table.setCellReferences(reference);
|
||||
|
||||
// Save
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ public class CreateUserDefinedDataFormats {
|
|||
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("format sheet");
|
||||
CellStyle style;
|
||||
DataFormat format = wb.createDataFormat();
|
||||
|
@ -58,11 +58,10 @@ public class CreateUserDefinedDataFormats {
|
|||
style.setDataFormat(format.getFormat("#,##0.0000"));
|
||||
cell.setCellStyle(style);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,9 +29,8 @@ import java.io.ByteArrayOutputStream;
|
|||
public class CustomXMLMapping {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
OPCPackage pkg = OPCPackage.open(args[0]);
|
||||
XSSFWorkbook wb = new XSSFWorkbook(pkg);
|
||||
|
||||
try (OPCPackage pkg = OPCPackage.open(args[0]);
|
||||
XSSFWorkbook wb = new XSSFWorkbook(pkg)) {
|
||||
for (XSSFMap map : wb.getCustomXMLMappings()) {
|
||||
XSSFExportToXml exporter = new XSSFExportToXml(map);
|
||||
|
||||
|
@ -40,7 +39,6 @@ public class CustomXMLMapping {
|
|||
String xml = os.toString("UTF-8");
|
||||
System.out.println(xml);
|
||||
}
|
||||
pkg.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,10 +32,10 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|||
*/
|
||||
public class EmbeddedObjects {
|
||||
public static void main(String[] args) throws Exception {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(args[0]);
|
||||
try (XSSFWorkbook workbook = new XSSFWorkbook(args[0])) {
|
||||
for (PackagePart pPart : workbook.getAllEmbedds()) {
|
||||
String contentType = pPart.getContentType();
|
||||
InputStream is = pPart.getInputStream();
|
||||
try (InputStream is = pPart.getInputStream()) {
|
||||
Closeable document;
|
||||
if (contentType.equals("application/vnd.ms-excel")) {
|
||||
// Excel Workbook - either binary or OpenXML
|
||||
|
@ -60,8 +60,8 @@ public class EmbeddedObjects {
|
|||
document = is;
|
||||
}
|
||||
document.close();
|
||||
is.close();
|
||||
}
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,7 +34,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
*/
|
||||
public class FillsAndColors {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
|
@ -57,9 +57,9 @@ public class FillsAndColors {
|
|||
cell.setCellStyle(style);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class FitSheetToOnePage {
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("format sheet");
|
||||
PrintSetup ps = sheet.getPrintSetup();
|
||||
|
||||
|
@ -38,9 +38,9 @@ public class FitSheetToOnePage {
|
|||
|
||||
// Create various cells and rows for spreadsheet.
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("fitSheetToOnePage.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("fitSheetToOnePage.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,9 +28,8 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
|
||||
public class HeadersAndFooters {
|
||||
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("first-header - format sheet");
|
||||
sheet.createRow(0).createCell(0).setCellValue(123);
|
||||
|
||||
|
@ -41,45 +40,45 @@ public class HeadersAndFooters {
|
|||
footer.setRight("Page &P of &N");
|
||||
|
||||
|
||||
Header firstHeader=((XSSFSheet)sheet).getFirstHeader();
|
||||
Header firstHeader = ((XSSFSheet) sheet).getFirstHeader();
|
||||
//&F == workbook file name
|
||||
firstHeader.setLeft("&F ......... first header");
|
||||
|
||||
for(int i=0;i<100;i=i+10){
|
||||
for (int i = 0; i < 100; i = i + 10) {
|
||||
sheet.createRow(i).createCell(0).setCellValue(123);
|
||||
}
|
||||
|
||||
|
||||
XSSFSheet sheet2 = (XSSFSheet)wb.createSheet("odd header-even footer");
|
||||
Header oddHeader=sheet2.getOddHeader();
|
||||
XSSFSheet sheet2 = (XSSFSheet) wb.createSheet("odd header-even footer");
|
||||
Header oddHeader = sheet2.getOddHeader();
|
||||
//&B == bold
|
||||
//&E == double underline
|
||||
//&D == date
|
||||
oddHeader.setCenter("&B &E oddHeader &D ");
|
||||
|
||||
Footer evenFooter=sheet2.getEvenFooter();
|
||||
Footer evenFooter = sheet2.getEvenFooter();
|
||||
evenFooter.setRight("even footer &P");
|
||||
sheet2.createRow(10).createCell(0).setCellValue("Second sheet with an oddHeader and an evenFooter");
|
||||
|
||||
for(int i=0;i<200;i=i+10){
|
||||
for (int i = 0; i < 200; i = i + 10) {
|
||||
sheet2.createRow(i).createCell(0).setCellValue(123);
|
||||
}
|
||||
|
||||
XSSFSheet sheet3 = (XSSFSheet)wb.createSheet("odd header- odd footer");
|
||||
XSSFSheet sheet3 = (XSSFSheet) wb.createSheet("odd header- odd footer");
|
||||
sheet3.createRow(10).createCell(0).setCellValue("Third sheet with oddHeader and oddFooter");
|
||||
Header oddH=sheet3.getOddHeader();
|
||||
Header oddH = sheet3.getOddHeader();
|
||||
//&C == centered
|
||||
oddH.setCenter("centered oddHeader");
|
||||
oddH.setLeft("left ");
|
||||
oddH.setRight("right ");
|
||||
|
||||
Footer oddF=sheet3.getOddFooter();
|
||||
Footer oddF = sheet3.getOddFooter();
|
||||
oddF.setLeft("Page &P");
|
||||
oddF.setRight("Pages &N ");
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("headerFooter.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("headerFooter.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,9 +35,8 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
*/
|
||||
public class HyperlinkExample {
|
||||
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
CreationHelper createHelper = wb.getCreationHelper();
|
||||
|
||||
//cell style for hyperlinks
|
||||
|
@ -89,10 +88,9 @@ public class HyperlinkExample {
|
|||
cell.setHyperlink(link2);
|
||||
cell.setCellStyle(hlink_style);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
|
||||
try (FileOutputStream out = new FileOutputStream("hyperinks.xlsx")) {
|
||||
wb.write(out);
|
||||
out.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class IterateCells {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
|
||||
try (Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]))) {
|
||||
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
|
||||
Sheet sheet = wb.getSheetAt(i);
|
||||
System.out.println(wb.getSheetName(i));
|
||||
|
@ -43,6 +43,6 @@ public class IterateCells {
|
|||
}
|
||||
}
|
||||
}
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class LineChart {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
Sheet sheet = wb.createSheet("linechart");
|
||||
final int NUM_OF_ROWS = 3;
|
||||
final int NUM_OF_COLUMNS = 10;
|
||||
|
@ -85,9 +85,9 @@ public class LineChart {
|
|||
chart.plot(data, bottomAxis, leftAxis);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
*/
|
||||
public class MergingCells {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
Row row = sheet.createRow((short) 1);
|
||||
|
@ -43,9 +43,9 @@ public class MergingCells {
|
|||
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class NewLinesInCells {
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet();
|
||||
|
||||
Row row = sheet.createRow(2);
|
||||
|
@ -45,15 +45,15 @@ public class NewLinesInCells {
|
|||
cell.setCellStyle(cs);
|
||||
|
||||
//increase row height to accomodate two lines of text
|
||||
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
|
||||
row.setHeightInPoints((2 * sheet.getDefaultRowHeightInPoints()));
|
||||
|
||||
//adjust column width to fit the content
|
||||
sheet.autoSizeColumn(2);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,50 +35,44 @@ public class Outlining {
|
|||
|
||||
|
||||
private void groupRowColumn() throws IOException {
|
||||
Workbook wb = new XSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
Sheet sheet1 = wb.createSheet("new sheet");
|
||||
|
||||
sheet1.groupRow( 5, 14 );
|
||||
sheet1.groupRow( 7, 14 );
|
||||
sheet1.groupRow( 16, 19 );
|
||||
sheet1.groupRow(5, 14);
|
||||
sheet1.groupRow(7, 14);
|
||||
sheet1.groupRow(16, 19);
|
||||
|
||||
sheet1.groupColumn( (short)4, (short)7 );
|
||||
sheet1.groupColumn( (short)9, (short)12 );
|
||||
sheet1.groupColumn( (short)10, (short)11 );
|
||||
sheet1.groupColumn((short) 4, (short) 7);
|
||||
sheet1.groupColumn((short) 9, (short) 12);
|
||||
sheet1.groupColumn((short) 10, (short) 11);
|
||||
|
||||
OutputStream fileOut = new FileOutputStream("outlining.xlsx");
|
||||
try {
|
||||
try (OutputStream fileOut = new FileOutputStream("outlining.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
} finally {
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void collapseExpandRowColumn() throws IOException {
|
||||
Workbook wb2 = new XSSFWorkbook();
|
||||
try (Workbook wb2 = new XSSFWorkbook()) {
|
||||
Sheet sheet2 = wb2.createSheet("new sheet");
|
||||
sheet2.groupRow( 5, 14 );
|
||||
sheet2.groupRow( 7, 14 );
|
||||
sheet2.groupRow( 16, 19 );
|
||||
sheet2.groupRow(5, 14);
|
||||
sheet2.groupRow(7, 14);
|
||||
sheet2.groupRow(16, 19);
|
||||
|
||||
sheet2.groupColumn( (short)4, (short)7 );
|
||||
sheet2.groupColumn( (short)9, (short)12 );
|
||||
sheet2.groupColumn( (short)10, (short)11 );
|
||||
sheet2.groupColumn((short) 4, (short) 7);
|
||||
sheet2.groupColumn((short) 9, (short) 12);
|
||||
sheet2.groupColumn((short) 10, (short) 11);
|
||||
|
||||
|
||||
sheet2.setRowGroupCollapsed( 7, true );
|
||||
sheet2.setRowGroupCollapsed(7, true);
|
||||
//sheet1.setRowGroupCollapsed(7,false);
|
||||
|
||||
sheet2.setColumnGroupCollapsed( (short)4, true );
|
||||
sheet2.setColumnGroupCollapsed( (short)4, false );
|
||||
sheet2.setColumnGroupCollapsed((short) 4, true);
|
||||
sheet2.setColumnGroupCollapsed((short) 4, false);
|
||||
|
||||
OutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx");
|
||||
try {
|
||||
try (OutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx")) {
|
||||
wb2.write(fileOut);
|
||||
} finally {
|
||||
fileOut.close();
|
||||
wb2.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class ScatterChart {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
Sheet sheet = wb.createSheet("Sheet 1");
|
||||
final int NUM_OF_ROWS = 3;
|
||||
final int NUM_OF_COLUMNS = 10;
|
||||
|
@ -86,9 +86,9 @@ public class ScatterChart {
|
|||
chart.plot(data, bottomAxis, leftAxis);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public abstract class SelectedSheet {
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
|
||||
wb.createSheet("row sheet");
|
||||
wb.createSheet("another sheet");
|
||||
|
@ -36,11 +36,10 @@ public abstract class SelectedSheet {
|
|||
|
||||
// Create various cells and rows for spreadsheet.
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("selectedSheet.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("selectedSheet.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class ShiftRows {
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("Sheet1");
|
||||
|
||||
Row row1 = sheet.createRow(1);
|
||||
|
@ -52,11 +52,9 @@ public class ShiftRows {
|
|||
// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
|
||||
sheet.shiftRows(5, 10, -4);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("shiftRows.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("shiftRows.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
*/
|
||||
public class SplitAndFreezePanes {
|
||||
public static void main(String[]args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
Sheet sheet1 = wb.createSheet("new sheet");
|
||||
Sheet sheet2 = wb.createSheet("second sheet");
|
||||
Sheet sheet3 = wb.createSheet("third sheet");
|
||||
|
@ -44,9 +44,9 @@ public class SplitAndFreezePanes {
|
|||
// Create a split with the lower left side being the active quadrant
|
||||
sheet4.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT);
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("splitFreezePane.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("splitFreezePane.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,13 +29,12 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class WorkbookProperties {
|
||||
|
||||
public static void main(String[]args) throws IOException {
|
||||
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
|
||||
workbook.createSheet("Workbook Properties");
|
||||
|
||||
POIXMLProperties props = workbook.getProperties();
|
||||
|
||||
/**
|
||||
/*
|
||||
* Extended properties are a predefined set of metadata properties
|
||||
* that are specifically applicable to Office Open XML documents.
|
||||
* Extended properties consist of 24 simple properties and 3 complex properties stored in the
|
||||
|
@ -45,7 +44,7 @@ public class WorkbookProperties {
|
|||
ext.getUnderlyingProperties().setCompany("Apache Software Foundation");
|
||||
ext.getUnderlyingProperties().setTemplate("XSSF");
|
||||
|
||||
/**
|
||||
/*
|
||||
* Custom properties enable users to define custom metadata properties.
|
||||
*/
|
||||
|
||||
|
@ -55,11 +54,9 @@ public class WorkbookProperties {
|
|||
cust.addProperty("Price", 45.50);
|
||||
cust.addProperty("Available", true);
|
||||
|
||||
FileOutputStream out = new FileOutputStream("workbook.xlsx");
|
||||
try (FileOutputStream out = new FileOutputStream("workbook.xlsx")) {
|
||||
workbook.write(out);
|
||||
out.close();
|
||||
workbook.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,7 +34,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
*/
|
||||
public class WorkingWithBorders {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("borders");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
|
@ -57,9 +57,9 @@ public class WorkingWithBorders {
|
|||
cell.setCellStyle(style);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
*/
|
||||
public class WorkingWithFonts {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
Sheet sheet = wb.createSheet("Fonts");
|
||||
|
||||
Font font0 = wb.createFont();
|
||||
|
@ -42,28 +42,28 @@ public class WorkingWithFonts {
|
|||
style0.setFont(font0);
|
||||
|
||||
Font font1 = wb.createFont();
|
||||
font1.setFontHeightInPoints((short)14);
|
||||
font1.setFontHeightInPoints((short) 14);
|
||||
font1.setFontName("Courier New");
|
||||
font1.setColor(IndexedColors.RED.getIndex());
|
||||
CellStyle style1 = wb.createCellStyle();
|
||||
style1.setFont(font1);
|
||||
|
||||
Font font2 = wb.createFont();
|
||||
font2.setFontHeightInPoints((short)16);
|
||||
font2.setFontHeightInPoints((short) 16);
|
||||
font2.setFontName("Arial");
|
||||
font2.setColor(IndexedColors.GREEN.getIndex());
|
||||
CellStyle style2 = wb.createCellStyle();
|
||||
style2.setFont(font2);
|
||||
|
||||
Font font3 = wb.createFont();
|
||||
font3.setFontHeightInPoints((short)18);
|
||||
font3.setFontHeightInPoints((short) 18);
|
||||
font3.setFontName("Times New Roman");
|
||||
font3.setColor(IndexedColors.LAVENDER.getIndex());
|
||||
CellStyle style3 = wb.createCellStyle();
|
||||
style3.setFont(font3);
|
||||
|
||||
Font font4 = wb.createFont();
|
||||
font4.setFontHeightInPoints((short)18);
|
||||
font4.setFontHeightInPoints((short) 18);
|
||||
font4.setFontName("Wingdings");
|
||||
font4.setColor(IndexedColors.GOLD.getIndex());
|
||||
CellStyle style4 = wb.createCellStyle();
|
||||
|
@ -99,9 +99,9 @@ public class WorkingWithFonts {
|
|||
cell5.setCellStyle(style5);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,9 +30,9 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||
public class WorkingWithPageSetup {
|
||||
|
||||
public static void main(String[]args) throws Exception {
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
|
||||
/**
|
||||
/*
|
||||
* It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the Workbook object.
|
||||
*
|
||||
* This function Contains 5 parameters:
|
||||
|
@ -74,10 +74,9 @@ public class WorkingWithPageSetup {
|
|||
wb.setPrintArea(0, 1, 2, 0, 3);
|
||||
|
||||
|
||||
FileOutputStream fileOut = new FileOutputStream("xssf-printsetup.xlsx");
|
||||
try (FileOutputStream fileOut = new FileOutputStream("xssf-printsetup.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,8 +39,7 @@ public class WorkingWithPictures {
|
|||
public static void main(String[] args) throws IOException {
|
||||
|
||||
//create a new workbook
|
||||
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
CreationHelper helper = wb.getCreationHelper();
|
||||
|
||||
//add a picture in this workbook.
|
||||
|
@ -66,18 +65,12 @@ public class WorkingWithPictures {
|
|||
|
||||
//save workbook
|
||||
String file = "picture.xls";
|
||||
if(wb instanceof XSSFWorkbook)
|
||||
{
|
||||
if (wb instanceof XSSFWorkbook) {
|
||||
file += "x"; // NOSONAR
|
||||
}
|
||||
OutputStream fileOut = new FileOutputStream(file);
|
||||
try {
|
||||
try (OutputStream fileOut = new FileOutputStream(file)) {
|
||||
wb.write(fileOut);
|
||||
} finally {
|
||||
fileOut.close();
|
||||
}
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,9 +28,7 @@ import java.io.OutputStream;
|
|||
public class WorkingWithRichText {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||
try {
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
XSSFSheet sheet = wb.createSheet();
|
||||
XSSFRow row = sheet.createRow(2);
|
||||
|
||||
|
@ -55,14 +53,9 @@ public class WorkingWithRichText {
|
|||
cell.setCellValue(rt);
|
||||
|
||||
// Write the output to a file
|
||||
OutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx");
|
||||
try {
|
||||
try (OutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx")) {
|
||||
wb.write(fileOut);
|
||||
} finally {
|
||||
fileOut.close();
|
||||
}
|
||||
} finally {
|
||||
wb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ import org.apache.poi.xwpf.usermodel.XWPFRun;
|
|||
public class BetterHeaderFooterExample {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
|
||||
XWPFParagraph p = doc.createParagraph();
|
||||
|
||||
|
@ -48,9 +48,9 @@ public class BetterHeaderFooterExample {
|
|||
XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT);
|
||||
foot.createParagraph().createRun().setText("footer");
|
||||
|
||||
OutputStream os = new FileOutputStream(new File("header2.docx"));
|
||||
try (OutputStream os = new FileOutputStream(new File("header2.docx"))) {
|
||||
doc.write(os);
|
||||
os.close();
|
||||
doc.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblLayoutType;
|
|||
public class HeaderFooterTable {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
|
||||
// Create a header with a 1 row, 3 column table
|
||||
// changes made for issue 57366 allow a new header or footer
|
||||
|
@ -56,7 +56,7 @@ public class HeaderFooterTable {
|
|||
tbl.setCellMargins(pad, pad, pad, pad);
|
||||
|
||||
// Set table width to 6.5 inches in 1440ths of a point
|
||||
tbl.setWidth((int)(6.5 * 1440));
|
||||
tbl.setWidth((int) (6.5 * 1440));
|
||||
// Can not yet set table or cell width properly, tables default to
|
||||
// autofit layout, and this requires fixed layout
|
||||
CTTbl ctTbl = tbl.getCTTbl();
|
||||
|
@ -97,9 +97,9 @@ public class HeaderFooterTable {
|
|||
r = p.createRun();
|
||||
r.setText("footer text");
|
||||
|
||||
OutputStream os = new FileOutputStream(new File("headertable.docx"));
|
||||
try (OutputStream os = new FileOutputStream(new File("headertable.docx"))) {
|
||||
doc.write(os);
|
||||
doc.close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ import org.apache.poi.xwpf.usermodel.XWPFRun;
|
|||
public class SimpleDocument {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
|
||||
XWPFParagraph p1 = doc.createParagraph();
|
||||
p1.setAlignment(ParagraphAlignment.CENTER);
|
||||
|
@ -122,9 +122,9 @@ public class SimpleDocument {
|
|||
r5.setText("The pangs of despised love, the law's delay,"
|
||||
+ "The insolence of office and the spurns" + ".......");
|
||||
|
||||
FileOutputStream out = new FileOutputStream("simple.docx");
|
||||
try (FileOutputStream out = new FileOutputStream("simple.docx")) {
|
||||
doc.write(out);
|
||||
out.close();
|
||||
doc.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,10 +35,8 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
|
|||
*/
|
||||
public class SimpleDocumentWithHeader {
|
||||
|
||||
private static XWPFParagraph[] pars;
|
||||
|
||||
public static void main(String[] args) {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
public static void main(String[] args) throws IOException {
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
|
||||
XWPFParagraph p = doc.createParagraph();
|
||||
|
||||
|
@ -51,7 +49,7 @@ public class SimpleDocumentWithHeader {
|
|||
CTP ctP = CTP.Factory.newInstance();
|
||||
CTText t = ctP.addNewR().addNewT();
|
||||
t.setStringValue("header");
|
||||
pars = new XWPFParagraph[1];
|
||||
XWPFParagraph[] pars = new XWPFParagraph[1];
|
||||
p = new XWPFParagraph(ctP, doc);
|
||||
pars[0] = p;
|
||||
|
||||
|
@ -64,13 +62,9 @@ public class SimpleDocumentWithHeader {
|
|||
pars[0] = new XWPFParagraph(ctP, doc);
|
||||
hfPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars);
|
||||
|
||||
try {
|
||||
OutputStream os = new FileOutputStream(new File("header.docx"));
|
||||
try (OutputStream os = new FileOutputStream(new File("header.docx"))) {
|
||||
doc.write(os);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -35,25 +35,25 @@ import org.apache.poi.xwpf.usermodel.XWPFRun;
|
|||
public class SimpleImages {
|
||||
|
||||
public static void main(String[] args) throws IOException, InvalidFormatException {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
XWPFParagraph p = doc.createParagraph();
|
||||
|
||||
XWPFRun r = p.createRun();
|
||||
|
||||
for(String imgFile : args) {
|
||||
for (String imgFile : args) {
|
||||
int format;
|
||||
|
||||
if(imgFile.endsWith(".emf")) format = XWPFDocument.PICTURE_TYPE_EMF;
|
||||
else if(imgFile.endsWith(".wmf")) format = XWPFDocument.PICTURE_TYPE_WMF;
|
||||
else if(imgFile.endsWith(".pict")) format = XWPFDocument.PICTURE_TYPE_PICT;
|
||||
else if(imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) format = XWPFDocument.PICTURE_TYPE_JPEG;
|
||||
else if(imgFile.endsWith(".png")) format = XWPFDocument.PICTURE_TYPE_PNG;
|
||||
else if(imgFile.endsWith(".dib")) format = XWPFDocument.PICTURE_TYPE_DIB;
|
||||
else if(imgFile.endsWith(".gif")) format = XWPFDocument.PICTURE_TYPE_GIF;
|
||||
else if(imgFile.endsWith(".tiff")) format = XWPFDocument.PICTURE_TYPE_TIFF;
|
||||
else if(imgFile.endsWith(".eps")) format = XWPFDocument.PICTURE_TYPE_EPS;
|
||||
else if(imgFile.endsWith(".bmp")) format = XWPFDocument.PICTURE_TYPE_BMP;
|
||||
else if(imgFile.endsWith(".wpg")) format = XWPFDocument.PICTURE_TYPE_WPG;
|
||||
if (imgFile.endsWith(".emf")) format = XWPFDocument.PICTURE_TYPE_EMF;
|
||||
else if (imgFile.endsWith(".wmf")) format = XWPFDocument.PICTURE_TYPE_WMF;
|
||||
else if (imgFile.endsWith(".pict")) format = XWPFDocument.PICTURE_TYPE_PICT;
|
||||
else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) format = XWPFDocument.PICTURE_TYPE_JPEG;
|
||||
else if (imgFile.endsWith(".png")) format = XWPFDocument.PICTURE_TYPE_PNG;
|
||||
else if (imgFile.endsWith(".dib")) format = XWPFDocument.PICTURE_TYPE_DIB;
|
||||
else if (imgFile.endsWith(".gif")) format = XWPFDocument.PICTURE_TYPE_GIF;
|
||||
else if (imgFile.endsWith(".tiff")) format = XWPFDocument.PICTURE_TYPE_TIFF;
|
||||
else if (imgFile.endsWith(".eps")) format = XWPFDocument.PICTURE_TYPE_EPS;
|
||||
else if (imgFile.endsWith(".bmp")) format = XWPFDocument.PICTURE_TYPE_BMP;
|
||||
else if (imgFile.endsWith(".wpg")) format = XWPFDocument.PICTURE_TYPE_WPG;
|
||||
else {
|
||||
System.err.println("Unsupported picture: " + imgFile +
|
||||
". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
|
||||
|
@ -66,10 +66,10 @@ public class SimpleImages {
|
|||
r.addBreak(BreakType.PAGE);
|
||||
}
|
||||
|
||||
FileOutputStream out = new FileOutputStream("images.docx");
|
||||
try (FileOutputStream out = new FileOutputStream("images.docx")) {
|
||||
doc.write(out);
|
||||
out.close();
|
||||
doc.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -69,9 +69,7 @@ public class SimpleTable {
|
|||
}
|
||||
|
||||
public static void createSimpleTable() throws Exception {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
|
||||
try {
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
XWPFTable table = doc.createTable(3, 3);
|
||||
|
||||
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
|
||||
|
@ -93,14 +91,9 @@ public class SimpleTable {
|
|||
|
||||
table.getRow(2).getCell(2).setText("only text");
|
||||
|
||||
OutputStream out = new FileOutputStream("simpleTable.docx");
|
||||
try {
|
||||
try (OutputStream out = new FileOutputStream("simpleTable.docx")) {
|
||||
doc.write(out);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
} finally {
|
||||
doc.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,14 +108,11 @@ public class SimpleTable {
|
|||
* I make no claims that this is the "right" way to do it, but it worked
|
||||
* for me. Given the scarcity of XWPF examples, I thought this may prove
|
||||
* instructive and give you ideas for your own solutions.
|
||||
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void createStyledTable() throws Exception {
|
||||
// Create a new document from scratch
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
|
||||
try {
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
// -- OR --
|
||||
// open an existing empty document with styles already defined
|
||||
//XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));
|
||||
|
@ -201,14 +191,9 @@ public class SimpleTable {
|
|||
} // for row
|
||||
|
||||
// write the file
|
||||
OutputStream out = new FileOutputStream("styledTable.docx");
|
||||
try {
|
||||
try (OutputStream out = new FileOutputStream("styledTable.docx")) {
|
||||
doc.write(out);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
} finally {
|
||||
doc.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue