+ public void saveToCsvFilename(final String filename) throws IOException {
+ BufferedWriter writer = null;
+
+ try {
+ writer = new BufferedWriter(new FileWriter(filename));
+ for (Row row: rows) {
+ List<String> list = new ArrayList<String>(row.cells.size());
+ for (Cell cell: row.cells) {
+ list.add(cell.getText());
+ }
+ writer.write(StringUtils.toCsv(list));
+ writer.write("\n");
+ }
+ } finally {
+ if (writer != null) {
+ writer.close();
+ }
+ }
+ }
+
+ /**
+ * Save contents to file in text format with lines.
+ *
+ * @param filename file to save to
+ * @throws IOException if a java.io operation throws
+ */
+ public void saveToTextFilename(final String filename) throws IOException {
+ BufferedWriter writer = null;
+
+ try {
+ writer = new BufferedWriter(new FileWriter(filename));
+
+ if ((topBorder == Border.SINGLE) && (leftBorder == Border.SINGLE)) {
+ // Emit top-left corner.
+ writer.write("\u250c");
+ }
+
+ if (topBorder == Border.SINGLE) {
+ int cellI = 0;
+ for (Cell cell: rows.get(0).cells) {
+ for (int i = 0; i < columns.get(cellI).width; i++) {
+ writer.write("\u2500");
+ }
+
+ if (columns.get(cellI).rightBorder == Border.SINGLE) {
+ if (cellI < columns.size() - 1) {
+ // Emit top tee.
+ writer.write("\u252c");
+ } else {
+ // Emit top-right corner.
+ writer.write("\u2510");
+ }
+ }
+ cellI++;
+ }
+ }
+ writer.write("\n");
+
+ int rowI = 0;
+ for (Row row: rows) {
+
+ if (leftBorder == Border.SINGLE) {
+ // Emit left border.
+ writer.write("\u2502");
+ }
+
+ int cellI = 0;
+ for (Cell cell: row.cells) {
+ writer.write(String.format("%" +
+ columns.get(cellI).width + "s", cell.getText()));
+
+ if (columns.get(cellI).rightBorder == Border.SINGLE) {
+ // Emit right border.
+ writer.write("\u2502");
+ }
+ cellI++;
+ }
+ writer.write("\n");
+
+ if (row.bottomBorder == Border.NONE) {
+ // All done, move on to the next row.
+ continue;
+ }
+
+ // Emit the bottom borders and intersections.
+ if ((leftBorder == Border.SINGLE)
+ && (row.bottomBorder != Border.NONE)
+ ) {
+ if (rowI < rows.size() - 1) {
+ if (row.bottomBorder == Border.SINGLE) {
+ // Emit left tee.
+ writer.write("\u251c");
+ } else if (row.bottomBorder == Border.DOUBLE) {
+ // Emit left tee (double).
+ writer.write("\u255e");
+ } else if (row.bottomBorder == Border.THICK) {
+ // Emit left tee (thick).
+ writer.write("\u251d");
+ }
+ }
+
+ if (rowI == rows.size() - 1) {
+ if (row.bottomBorder == Border.SINGLE) {
+ // Emit left bottom corner.
+ writer.write("\u2514");
+ } else if (row.bottomBorder == Border.DOUBLE) {
+ // Emit left bottom corner (double).
+ writer.write("\u2558");
+ } else if (row.bottomBorder == Border.THICK) {
+ // Emit left bottom corner (thick).
+ writer.write("\u2515");
+ }
+ }
+ }
+
+ cellI = 0;
+ for (Cell cell: row.cells) {
+
+ for (int i = 0; i < columns.get(cellI).width; i++) {
+ if (row.bottomBorder == Border.SINGLE) {
+ writer.write("\u2500");
+ }
+ if (row.bottomBorder == Border.DOUBLE) {
+ writer.write("\u2550");
+ }
+ if (row.bottomBorder == Border.THICK) {
+ writer.write("\u2501");
+ }
+ }
+
+ if ((rowI < rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.SINGLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right tee.
+ writer.write("\u2524");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.DOUBLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right tee (double).
+ writer.write("\u2561");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.THICK)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right tee (thick).
+ writer.write("\u2525");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.SINGLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right bottom corner.
+ writer.write("\u2518");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.DOUBLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right bottom corner (double).
+ writer.write("\u255b");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.THICK)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right bottom corner (thick).
+ writer.write("\u2519");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.SINGLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit intersection.
+ writer.write("\u253c");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.DOUBLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit intersection (double).
+ writer.write("\u256a");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.THICK)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit intersection (thick).
+ writer.write("\u253f");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.SINGLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit bottom tee.
+ writer.write("\u2534");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.DOUBLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit bottom tee (double).
+ writer.write("\u2567");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.THICK)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit bottom tee (thick).
+ writer.write("\u2537");
+ }
+
+ cellI++;
+ }
+
+ writer.write("\n");
+ rowI++;
+ }
+ } finally {
+ if (writer != null) {
+ writer.close();
+ }
+ }