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,9 +52,9 @@ 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);
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ 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()) {
|
||||
|
@ -45,8 +45,8 @@ public class SoundFinder {
|
|||
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);
|
||||
}
|
||||
|
@ -126,8 +121,7 @@ public class BigExample {
|
|||
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);
|
||||
|
@ -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.
|
||||
|
@ -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();
|
||||
|
@ -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++) {
|
||||
|
@ -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);
|
||||
|
||||
|
@ -163,28 +162,15 @@ public class InCellLists {
|
|||
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");
|
||||
|
@ -45,9 +45,10 @@ public class OfficeDrawing {
|
|||
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,7 +32,7 @@ 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();
|
||||
try (HSSFWorkbook wb = new HSSFWorkbook()) {
|
||||
HSSFSheet sheet = wb.createSheet("my drawing");
|
||||
sheet.setColumnWidth(1, 256 * 27);
|
||||
HSSFRow row1 = sheet.createRow(0);
|
||||
|
@ -73,10 +73,10 @@ public class OfficeDrawingWithGraphics {
|
|||
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 {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,7 +30,7 @@ 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");
|
||||
|
@ -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,7 +28,7 @@ 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");
|
||||
|
@ -43,9 +43,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("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.
|
||||
|
@ -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);
|
||||
|
||||
|
@ -137,11 +137,11 @@ public class CalendarDemo {
|
|||
// 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();
|
||||
|
||||
wb.close();
|
||||
try (FileOutputStream out = new FileOutputStream(file)) {
|
||||
wb.write(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -43,7 +43,7 @@ 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++) {
|
||||
|
@ -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,9 +50,7 @@ public class UserDefinedFunctionExample {
|
|||
|
||||
File workbookFile = new File( args[0] ) ;
|
||||
|
||||
Workbook workbook = WorkbookFactory.create(workbookFile, null, true);
|
||||
|
||||
try {
|
||||
try (Workbook workbook = WorkbookFactory.create(workbookFile, null, true)) {
|
||||
String[] functionNames = {"calculatePayment"};
|
||||
FreeRefFunction[] functionImpls = {new CalculateMortgage()};
|
||||
|
||||
|
@ -75,8 +72,6 @@ public class UserDefinedFunctionExample {
|
|||
CellValue value = evaluator.evaluate(cell);
|
||||
|
||||
System.out.println("returns value: " + value);
|
||||
} finally {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
@ -88,8 +88,7 @@ public final class DataExtraction {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,9 +28,7 @@ import java.io.FileOutputStream;
|
|||
public final class MergePresentations {
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
XMLSlideShow ppt = new XMLSlideShow();
|
||||
|
||||
try {
|
||||
try (XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
for (String arg : args) {
|
||||
FileInputStream is = new FileInputStream(arg);
|
||||
XMLSlideShow src = new XMLSlideShow(is);
|
||||
|
@ -43,11 +41,9 @@ public final class MergePresentations {
|
|||
src.close();
|
||||
}
|
||||
|
||||
FileOutputStream out = new FileOutputStream("merged.pptx");
|
||||
try (FileOutputStream out = new FileOutputStream("merged.pptx")) {
|
||||
ppt.write(out);
|
||||
out.close();
|
||||
} finally {
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,12 +62,10 @@ 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
|
||||
|
@ -83,8 +81,7 @@ public class PieChartDemo {
|
|||
|
||||
// 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();
|
||||
|
@ -140,26 +137,16 @@ public class PieChartDemo {
|
|||
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,11 +29,10 @@ 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);
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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,7 +40,7 @@ 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()) {
|
||||
|
@ -64,7 +64,6 @@ public class Step1 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
ppt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ 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:");
|
||||
|
@ -44,7 +44,8 @@ public class Step2 {
|
|||
}
|
||||
|
||||
// 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)) {
|
||||
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++) {
|
||||
|
@ -124,11 +124,11 @@ public class CalendarDemo {
|
|||
}
|
||||
|
||||
// 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();
|
||||
|
||||
|
@ -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,9 +36,8 @@ 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();
|
||||
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
|
||||
CreationHelper creationHelper = wb.getCreationHelper();
|
||||
Sheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
@ -77,9 +76,9 @@ public class HeadersAndFooters {
|
|||
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);
|
||||
|
@ -50,10 +50,10 @@ public class NewLinesInCells {
|
|||
//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,7 +35,7 @@ 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);
|
||||
|
@ -46,17 +46,14 @@ public class Outlining {
|
|||
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);
|
||||
|
@ -73,12 +70,9 @@ public class Outlining {
|
|||
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();
|
||||
|
@ -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
|
||||
|
@ -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,7 +35,7 @@ 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();
|
||||
|
@ -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