1

Well, I have a String in my project that is like this:

,0,0,0,0,0,1,1,
,0,0,0,7,8,6,6,
,3,3,3,3,9,4,5,
,5,6,6,9,5,2,1,
,6,2,8,0,0,3,9,
--------------------------------------------------
Reference,-,C,A,A,G,A,T,
17-F1,.,.,.,.,.,T,C,
37-F2,1A,A,C,T,T,.,.,

And I need to convert this to a XLS file

Bruno Andrade
  • 253
  • 3
  • 8
  • 17
  • [This](http://stackoverflow.com/questions/1516144/how-to-read-and-write-excel-file-in-java) may be helpful. – pushkin Jul 31 '15 at 03:23
  • The thing is, this string is kinda a csv file, so which word supposed to be in one cell, so i'm having trouble thinking how to read the "line" and put those words into cells – Bruno Andrade Jul 31 '15 at 03:31
  • 1
    Can't you just do `str.split(",")` to split your string by commas and then each element in the resulting array will go in a cell? I'm having trouble understanding what exactly is part of the string. Is it the numbers above ---- or everything. – pushkin Jul 31 '15 at 03:35
  • I can do that, but this is one big string, and I need to split those lines too. If i do that the string will be in only one line. – Bruno Andrade Jul 31 '15 at 03:51
  • Ah I see, so there aren't newlines then. Do you know the number of columns? In that case you could still split it and then every Nth element let's say would be the beginning of the next row. – pushkin Jul 31 '15 at 03:55
  • It isnt the same number every time :\ – Bruno Andrade Jul 31 '15 at 03:57

1 Answers1

2

My awnser using Apache poi and Commons IO

Workbook wb = new HSSFWorkbook();
CreationHelper helper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");

List<String> lines = IOUtils.readLines(new StringReader(csv));

    for (int i = 0; i < lines.size(); i++) {
        String str[] = lines.get(i).split(",");
        Row row = sheet.createRow((short) i);
        for (int j = 0; j < str.length; j++) {
             row.createCell(j).setCellValue(helper.createRichTextString(str[j]));

                 }
              }

FileOutputStream fileOut = new FileOutputStream("c:\\someName.xls");
    wb.write(fileOut);
    fileOut.close();
Bruno Andrade
  • 253
  • 3
  • 8
  • 17