diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index 86e0c44..df720b3 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -1,5 +1,6 @@ package de.hs_mannheim.informatik.spreadsheet; +import java.io.FileNotFoundException; /** * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. @@ -8,7 +9,7 @@ package de.hs_mannheim.informatik.spreadsheet; */ public class Axel { - public static void main(String[] args) { + public static void main(String[] args) throws FileNotFoundException { Spreadsheet spr = new Spreadsheet(10,10); spr.put("A3", "123"); @@ -20,7 +21,9 @@ public class Axel { System.out.println(spr); - // TODO: You might want to put "UI loop" for entering value and formulas here. + spr.saveCsv("/tmp/test.csv"); + + // TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods. } } \ No newline at end of file diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 8bada7a..4504b06 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -2,6 +2,7 @@ package de.hs_mannheim.informatik.spreadsheet; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.PrintWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -79,6 +80,31 @@ public class Spreadsheet { // TODO: implement this } + /** + * A method for saving data to a CSV file. + * @param path The file to write. + * @return Nothing. + * @exception IOException If path does not exist. + */ + public void saveCsv(String path) throws FileNotFoundException { + PrintWriter out = new PrintWriter(path); + + for (Cell[] row : cells) { + for (Cell cell : row) { + if (!cell.getFormula().isEmpty()) + out.print("=" + cell.getFormula()); + else + out.print(cell.getValue()); + + if (cell != row[cells.length-1]) + out.print(","); + } + out.println(); + } + + out.close(); + } + /** * This method does the actual evaluation/calcluation of a specific cell * @param cellName the name of the cell to be evaluated @@ -131,7 +157,7 @@ public class Spreadsheet { Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); long res = 0; - + // TODO implement // uncomment the following to see an example how the elements of a formula can be accessed