3 Replies Latest reply: Apr 8, 2007 10:02 AM by 807599 RSS

    HSSF POI - Lack of special chars in Excel

    807599
      Hello all!

      I'm using HSSF POI library to make *.xls files. The code below should get data from a text file (saved in UTF-8), put it into cells, and save result as .xls file. When I'm trying to read a file in OpenOffice it works fine. When produced file is opened in Excel, there is no special chars (I'm using polish characters like ąźżćśę) - anybody here can help?

      Code:
      HSSFWorkbook wb;
      HSSFSheet sheet;
      wb = new HSSFWorkbook(fs);
      sheet = wb.getSheetAt(0);
      FileReader input = new FileReader( "Source file path" );
      
      BufferedReader bufRead = new BufferedReader(input);
      String contents="", line;
      
      line = bufRead.readLine();
      
      while (line != null){
         contents += line + "\n";
         line = bufRead.readLine();  
      }
      
      bufRead.close();
      
      String[] areas = contents.split("\n\n");   
      
      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFSheet sheet = wb.createSheet();
      wb.setSheetName(0,"Wyeksportowany arkusz",HSSFWorkbook.ENCODING_COMPRESSED_UNICODE);
      
      int i = 0;
      int u = 0;
      int y = 0;
               
      for(i=0;i<areas.length;i++){
      HSSFRow row = sheet.createRow((short)i);
      String[] comms = areas.split(":o:");
      u = 0;

      for(u=0;u<conts.length;u++){
      HSSFCell czell = row.createCell((short)u);
      czell.setCellType(HSSFCell.CELL_TYPE_STRING);
      czell.setCellValue(conts[u]+" ");          
      }
      coml = row.getLastCellNum();
      }
      sheet.setFitToPage(true);
      for(y=0;y<coml+1;y++){
           sheet.autoSizeColumn((short)y);
      }
      FileOutputStream fileOut = new FileOutputStream(savePath + "export.xls");

      wb.write(fileOut);
      fileOut.close();

      Sample file:
      cell data:o:next cell data:o:some other data
      
      next row:o:second cell:o:last cell