I unload [Russian-language] data in Excel (.xls format) from a tablet on Android. Part of the data is written in the code, part is taken from SQLite. When you open the file: in Linux (Libreoffice) - with encoding everything is fine; in Windows 7 (MS Excel) - solid krakozyabry of the type Заказ №

Online decoders with UTF8 tips -> CP-1251 and subsequent conversions in the type code

 new String(text.getBytes("windows-1251"),"utf-8") 

they did not help (it turned out only worse than пїЅпїЅпїЅпїЅпїЅпї ).

(There were also attempts to translate this way from some existing encodings to others and vice versa, but in Windows any of them is still displayed as krakozyabrami). Where am I doing something wrong?

  • And in what encoding do you write data to a file? - Sergi
  • The data in the xls file was written in the standard for Android Studio encoding (like, UTF-8?), I.e. as it is in the code StringBuilder orderInfo = new StringBuilder(); orderInfo.append("Заказ ...."); ..... StringBuilder orderInfo = new StringBuilder(); orderInfo.append("Заказ ...."); ..... StringBuilder orderInfo = new StringBuilder(); orderInfo.append("Заказ ...."); ..... then this orderInfo is then entered into the file. The database file itself is created on another side. Judging by the discovery in the test editor, there is still UTF-8 (only in it are normal letters). Although when writing to xls, the encoding of the letters is the same, which is directly from string, that the data from the database is written. - Yoko999
  • The record goes through FileWriter, the getEncoding method says "UTF8" - Yoko999
  • It feels like Excel just can't work with UTF-8. Alternatively, you can try to record in CP 1251 - Sergi
  • Tak-s ... If you open xls in Windows itself through Notepad ++, then exactly the same cracks as in Excel, are obtained if you transcode to ANSI or CP-1251. ..... - Yoko999

2 answers 2

(copied from my comment to the question)
Apparently, your version of Excel cannot recognize the file in UTF-8 encoding (which is strange).
To upload data to a file in CP1251, use: new OutputStreamWriter(new FileOutputStream(fileName), Charset.forName("CP1251"))

    It was more correct to do as described here.

    Using the org.apache.poi library :

      //New Workbook Workbook wb = new HSSFWorkbook(); //Отдельные ячейки Cell c = null; //Стилизация ячеек CellStyle cs = wb.createCellStyle(); cs.setFillForegroundColor(HSSFColor.LIME.index); cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //Лист Sheet sheet1 = null; sheet1 = wb.createSheet("myOrder"); // Строки Row row = sheet1.createRow(0); c = row.createCell(0); c.setCellValue("Item Number"); c.setCellStyle(cs); c = row.createCell(1); c.setCellValue("Quantity"); c.setCellStyle(cs); c = row.createCell(2); c.setCellValue("Price"); c.setCellStyle(cs); sheet1.setColumnWidth(0, (15 * 500)); sheet1.setColumnWidth(1, (15 * 500)); sheet1.setColumnWidth(2, (15 * 500)); // Путь к файлу File file = new File(context.getExternalFilesDir(null), fileName); FileOutputStream os = null; try { os = new FileOutputStream(file); wb.write(os); Log.w("FileUtils", "Writing file" + file); success = true; } catch (IOException e) { Log.w("FileUtils", "Error writing " + file, e); } catch (Exception e) { Log.w("FileUtils", "Failed to save file", e); } finally { try { if (null != os) os.close(); } catch (Exception ex) { } } return success; }