aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2016-06-02 20:14:28 +0000
committerDominik Stadler <centic@apache.org>2016-06-02 20:14:28 +0000
commitbb7c632559ec5b187402d17cc0d65e16572c5d15 (patch)
treedafcf2d8f4c9ae219dc9401c0e4e0ee93b3d91b2 /src/examples
parent8811c99ac3a9d6b843c99fc6cee00533067f1402 (diff)
downloadpoi-bb7c632559ec5b187402d17cc0d65e16572c5d15.tar.gz
poi-bb7c632559ec5b187402d17cc0d65e16572c5d15.zip
Fix some Sonar issues and some IntelliJ warnings
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1746627 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java82
-rw-r--r--src/examples/src/org/apache/poi/xwpf/usermodel/SimpleTable.java234
2 files changed, 165 insertions, 151 deletions
diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java
index 71d63b1f55..869316f087 100644
--- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java
+++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java
@@ -18,6 +18,7 @@
package org.apache.poi.xssf.usermodel.examples;
import java.io.FileOutputStream;
+import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@@ -25,51 +26,56 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Outlining {
- public static void main(String[]args) throws Exception{
- Outlining o=new Outlining();
- o.groupRowColumn();
- o.collapseExpandRowColumn();
+ public static void main(String[] args) throws Exception {
+ Outlining o=new Outlining();
+ o.groupRowColumn();
+ o.collapseExpandRowColumn();
}
- private void groupRowColumn() throws Exception{
- Workbook wb = new XSSFWorkbook();
- Sheet sheet1 = wb.createSheet("new sheet");
+ private void groupRowColumn() throws Exception {
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet1 = wb.createSheet("new sheet");
- sheet1.groupRow( 5, 14 );
- sheet1.groupRow( 7, 14 );
- sheet1.groupRow( 16, 19 );
+ sheet1.groupRow( 5, 14 );
+ sheet1.groupRow( 7, 14 );
+ sheet1.groupRow( 16, 19 );
- sheet1.groupColumn( (short)4, (short)7 );
- sheet1.groupColumn( (short)9, (short)12 );
- sheet1.groupColumn( (short)10, (short)11 );
-
- FileOutputStream fileOut = new FileOutputStream("outlining.xlsx");
- wb.write(fileOut);
- fileOut.close();
+ sheet1.groupColumn( (short)4, (short)7 );
+ sheet1.groupColumn( (short)9, (short)12 );
+ sheet1.groupColumn( (short)10, (short)11 );
+ OutputStream fileOut = new FileOutputStream("outlining.xlsx");
+ try {
+ wb.write(fileOut);
+ } finally {
+ fileOut.close();
+ }
}
- private void collapseExpandRowColumn()throws Exception{
- Workbook wb2 = new XSSFWorkbook();
- Sheet sheet2 = wb2.createSheet("new sheet");
- sheet2.groupRow( 5, 14 );
- sheet2.groupRow( 7, 14 );
- sheet2.groupRow( 16, 19 );
-
- sheet2.groupColumn( (short)4, (short)7 );
- sheet2.groupColumn( (short)9, (short)12 );
- sheet2.groupColumn( (short)10, (short)11 );
-
-
- sheet2.setRowGroupCollapsed( 7, true );
- //sheet1.setRowGroupCollapsed(7,false);
-
- sheet2.setColumnGroupCollapsed( (short)4, true );
- sheet2.setColumnGroupCollapsed( (short)4, false );
-
- FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx");
- wb2.write(fileOut);
- fileOut.close();
+ private void collapseExpandRowColumn() throws Exception {
+ Workbook wb2 = new XSSFWorkbook();
+ Sheet sheet2 = wb2.createSheet("new sheet");
+ sheet2.groupRow( 5, 14 );
+ sheet2.groupRow( 7, 14 );
+ sheet2.groupRow( 16, 19 );
+
+ sheet2.groupColumn( (short)4, (short)7 );
+ sheet2.groupColumn( (short)9, (short)12 );
+ sheet2.groupColumn( (short)10, (short)11 );
+
+
+ sheet2.setRowGroupCollapsed( 7, true );
+ //sheet1.setRowGroupCollapsed(7,false);
+
+ sheet2.setColumnGroupCollapsed( (short)4, true );
+ sheet2.setColumnGroupCollapsed( (short)4, false );
+
+ OutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx");
+ try {
+ wb2.write(fileOut);
+ } finally {
+ fileOut.close();
+ }
}
}
diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/SimpleTable.java b/src/examples/src/org/apache/poi/xwpf/usermodel/SimpleTable.java
index 5d07fe2164..387ab449b0 100644
--- a/src/examples/src/org/apache/poi/xwpf/usermodel/SimpleTable.java
+++ b/src/examples/src/org/apache/poi/xwpf/usermodel/SimpleTable.java
@@ -17,6 +17,7 @@
package org.apache.poi.xwpf.usermodel;
import java.io.FileOutputStream;
+import java.io.OutputStream;
import java.math.BigInteger;
import java.util.List;
@@ -62,32 +63,37 @@ public class SimpleTable {
public static void createSimpleTable() throws Exception {
XWPFDocument doc = new XWPFDocument();
- XWPFTable table = doc.createTable(3, 3);
-
- table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
-
- // table cells have a list of paragraphs; there is an initial
- // paragraph created when the cell is created. If you create a
- // paragraph in the document to put in the cell, it will also
- // appear in the document following the table, which is probably
- // not the desired result.
- XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
-
- XWPFRun r1 = p1.createRun();
- r1.setBold(true);
- r1.setText("The quick brown fox");
- r1.setItalic(true);
- r1.setFontFamily("Courier");
- r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
- r1.setTextPosition(100);
-
- table.getRow(2).getCell(2).setText("only text");
-
- FileOutputStream out = new FileOutputStream("simpleTable.docx");
- doc.write(out);
- out.close();
-
- doc.close();
+ try {
+ XWPFTable table = doc.createTable(3, 3);
+
+ table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
+
+ // table cells have a list of paragraphs; there is an initial
+ // paragraph created when the cell is created. If you create a
+ // paragraph in the document to put in the cell, it will also
+ // appear in the document following the table, which is probably
+ // not the desired result.
+ XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
+
+ XWPFRun r1 = p1.createRun();
+ r1.setBold(true);
+ r1.setText("The quick brown fox");
+ r1.setItalic(true);
+ r1.setFontFamily("Courier");
+ r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
+ r1.setTextPosition(100);
+
+ table.getRow(2).getCell(2).setText("only text");
+
+ OutputStream out = new FileOutputStream("simpleTable.docx");
+ try {
+ doc.write(out);
+ } finally {
+ out.close();
+ }
+ } finally {
+ doc.close();
+ }
}
/**
@@ -107,92 +113,94 @@ public class SimpleTable {
public static void createStyledTable() throws Exception {
// Create a new document from scratch
XWPFDocument doc = new XWPFDocument();
- // -- OR --
- // open an existing empty document with styles already defined
- //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));
-
- // Create a new table with 6 rows and 3 columns
- int nRows = 6;
- int nCols = 3;
- XWPFTable table = doc.createTable(nRows, nCols);
-
- // Set the table style. If the style is not defined, the table style
- // will become "Normal".
- CTTblPr tblPr = table.getCTTbl().getTblPr();
- CTString styleStr = tblPr.addNewTblStyle();
- styleStr.setVal("StyledTable");
-
- // Get a list of the rows in the table
- List<XWPFTableRow> rows = table.getRows();
- int rowCt = 0;
- int colCt = 0;
- for (XWPFTableRow row : rows) {
- // get table row properties (trPr)
- CTTrPr trPr = row.getCtRow().addNewTrPr();
- // set row height; units = twentieth of a point, 360 = 0.25"
- CTHeight ht = trPr.addNewTrHeight();
- ht.setVal(BigInteger.valueOf(360));
-
- // get the cells in this row
- List<XWPFTableCell> cells = row.getTableCells();
- // add content to each cell
- for (XWPFTableCell cell : cells) {
- // get a table cell properties element (tcPr)
- CTTcPr tcpr = cell.getCTTc().addNewTcPr();
- // set vertical alignment to "center"
- CTVerticalJc va = tcpr.addNewVAlign();
- va.setVal(STVerticalJc.CENTER);
-
- // create cell color element
- CTShd ctshd = tcpr.addNewShd();
- ctshd.setColor("auto");
- ctshd.setVal(STShd.CLEAR);
- if (rowCt == 0) {
- // header row
- ctshd.setFill("A7BFDE");
- }
- else if (rowCt % 2 == 0) {
- // even row
- ctshd.setFill("D3DFEE");
- }
- else {
- // odd row
- ctshd.setFill("EDF2F8");
- }
-
- // get 1st paragraph in cell's paragraph list
- XWPFParagraph para = cell.getParagraphs().get(0);
- // create a run to contain the content
- XWPFRun rh = para.createRun();
- // style cell as desired
- if (colCt == nCols - 1) {
- // last column is 10pt Courier
- rh.setFontSize(10);
- rh.setFontFamily("Courier");
- }
- if (rowCt == 0) {
- // header row
- rh.setText("header row, col " + colCt);
- rh.setBold(true);
- para.setAlignment(ParagraphAlignment.CENTER);
- }
- else {
- // other rows
- rh.setText("row " + rowCt + ", col " + colCt);
- para.setAlignment(ParagraphAlignment.LEFT);
- }
- colCt++;
- } // for cell
- colCt = 0;
- rowCt++;
- } // for row
-
- // write the file
- FileOutputStream out = new FileOutputStream("styledTable.docx");
- doc.write(out);
- out.close();
-
- doc.close();
- }
+ try {
+ // -- OR --
+ // open an existing empty document with styles already defined
+ //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));
+
+ // Create a new table with 6 rows and 3 columns
+ int nRows = 6;
+ int nCols = 3;
+ XWPFTable table = doc.createTable(nRows, nCols);
+
+ // Set the table style. If the style is not defined, the table style
+ // will become "Normal".
+ CTTblPr tblPr = table.getCTTbl().getTblPr();
+ CTString styleStr = tblPr.addNewTblStyle();
+ styleStr.setVal("StyledTable");
+
+ // Get a list of the rows in the table
+ List<XWPFTableRow> rows = table.getRows();
+ int rowCt = 0;
+ int colCt = 0;
+ for (XWPFTableRow row : rows) {
+ // get table row properties (trPr)
+ CTTrPr trPr = row.getCtRow().addNewTrPr();
+ // set row height; units = twentieth of a point, 360 = 0.25"
+ CTHeight ht = trPr.addNewTrHeight();
+ ht.setVal(BigInteger.valueOf(360));
+
+ // get the cells in this row
+ List<XWPFTableCell> cells = row.getTableCells();
+ // add content to each cell
+ for (XWPFTableCell cell : cells) {
+ // get a table cell properties element (tcPr)
+ CTTcPr tcpr = cell.getCTTc().addNewTcPr();
+ // set vertical alignment to "center"
+ CTVerticalJc va = tcpr.addNewVAlign();
+ va.setVal(STVerticalJc.CENTER);
+
+ // create cell color element
+ CTShd ctshd = tcpr.addNewShd();
+ ctshd.setColor("auto");
+ ctshd.setVal(STShd.CLEAR);
+ if (rowCt == 0) {
+ // header row
+ ctshd.setFill("A7BFDE");
+ } else if (rowCt % 2 == 0) {
+ // even row
+ ctshd.setFill("D3DFEE");
+ } else {
+ // odd row
+ ctshd.setFill("EDF2F8");
+ }
+
+ // get 1st paragraph in cell's paragraph list
+ XWPFParagraph para = cell.getParagraphs().get(0);
+ // create a run to contain the content
+ XWPFRun rh = para.createRun();
+ // style cell as desired
+ if (colCt == nCols - 1) {
+ // last column is 10pt Courier
+ rh.setFontSize(10);
+ rh.setFontFamily("Courier");
+ }
+ if (rowCt == 0) {
+ // header row
+ rh.setText("header row, col " + colCt);
+ rh.setBold(true);
+ para.setAlignment(ParagraphAlignment.CENTER);
+ } else {
+ // other rows
+ rh.setText("row " + rowCt + ", col " + colCt);
+ para.setAlignment(ParagraphAlignment.LEFT);
+ }
+ colCt++;
+ } // for cell
+ colCt = 0;
+ rowCt++;
+ } // for row
+
+ // write the file
+ OutputStream out = new FileOutputStream("styledTable.docx");
+ try {
+ doc.write(out);
+ } finally {
+ out.close();
+ }
+ } finally {
+ doc.close();
+ }
+ }
}