1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
public void save(File f)
{
PrintWriter printWriter = null;
try {
String file = f.getAbsolutePath().substring(0, f.getAbsolutePath().length()-4);
int nbLines = occurences(dataTextPane.getText(), "\n");
int curLine = 0;
int nbLineInOneFile = 0;
String text = "";
int start = 0;
int end = dataTextPane.getText().indexOf("\n");
String header = dataTextPane.getText().substring(start, end+1);
int fileNb = 0;
while(curLine < nbLines)
{
while (nbLineInOneFile < NB_LINES_MAX_IN_EXCEL && end >= 0)
{
String line = dataTextPane.getText().substring(start, end+1);
// At most 3 tabs in a row...
// Do not know how to do it properly
line = line.replaceAll("\t\t", "\t");
line = line.replaceAll("\t\t", "\t");
text = text.concat(line);
start = end + 1;
end = dataTextPane.getText().indexOf("\n", start);
curLine++;
nbLineInOneFile++;
}
File fTmp = new File(file + String.valueOf(fileNb)+".xls");
// Opens the file in writing
printWriter = new PrintWriter(new FileWriter(fTmp));
// Writes the text in the file
printWriter.print(text);
// Closes the file
printWriter.close();
fileNb++;
// Repeats header at the beginning of each file
text = "".concat(header);
// Initialized to 1 to include header line
nbLineInOneFile = 1;
}
} catch (Exception ex) {
Log.println(Log.ERROR, "Error while saving the file.");
// Closes the file
printWriter.close();
}
} |
Partager