1

I using jexcelapi for android.

I write string "Hello jExcelAPI!", but when I read I see Chinese symbols.

How I can fix it?

Thanks in advance.

P.S. Maybe it help: I dont know what need give in ws.setCharacterSet(cs);

enter image description here

package ru.elvigl.hello;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class HellojExcelAPIActivity extends Activity {

    final File file = new File("/sdcard/Folder/File.xls");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void btnExport_Click(View v) throws IOException, RowsExceededException, WriteException {
        WorkbookSettings ws = new WorkbookSettings();

        //ws.setCharacterSet(cs);
        ws.setEncoding("Cp1251");
        ws.setExcelDisplayLanguage("RU");
        ws.setExcelRegionalSettings("RU");
        ws.setLocale(new Locale("ru", "RU"));

        WritableWorkbook workbook = Workbook.createWorkbook(file, ws); 
        WritableSheet sheet = workbook.createSheet("First Sheet", 0); 

        Label label = new Label(0, 0, "Hello jExcelAPI!");
        sheet.addCell(label); 

        workbook.write();
        workbook.close(); 
    }

    public void btnImport_Click(View v) throws BiffException, IOException {

        WorkbookSettings ws = new WorkbookSettings();

        //ws.setCharacterSet(cs);
        ws.setEncoding("Cp1251");
        ws.setExcelDisplayLanguage("RU");
        ws.setExcelRegionalSettings("RU");
        ws.setLocale(new Locale("ru", "RU"));

        Workbook workbook = Workbook.getWorkbook(file, ws); 
        Sheet sheet = workbook.getSheet(0); 
        Cell a1 = sheet.getCell(0,0); 
        String str = a1.getContents();

        TextView tv = (TextView) findViewById(R.id.tvCellValue);
        tv.setText(str);
    }
}
Elvis
  • 831
  • 7
  • 9

2 Answers2

1

Here is the documentation for the class "WorkbookSettings": http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/WorkbookSettings.html It appears that the method "setCharacterSet" is only used for reading (importing) a spreadsheet, not exporting if that helps. As for the Chinese characters, I think they are displaying due to your Locale. I would use these constants to correctly instantiate your Locale object with the proper language and country constructor parameters: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Locale.html

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
  • I know "setCharacterSet" is only used for reading. I change "ws.setLocale(new Locale("ru", "RU"));" to "ws.setLocale(Locale.US);" but this not help. – Elvis Dec 08 '11 at 01:06
  • Are you trying to fix the Chinese characters or are you trying to make an import work? Take a look at the "Constructor Summary" in the second link I sent. You are using a country constant for a language parameter. I think you want this: new Locale(Locale.ENGLISH, Locale.US) – JustBeingHelpful Dec 08 '11 at 01:07
  • also make sure to import the namespace/library where Locale resides or use the fully qualified class name: java.util.Locale – JustBeingHelpful Dec 08 '11 at 01:14
  • 1
    Thanks for answers! I can't fix encoding :-( **1)** I trying make export and import work. **2)** When I export and open in Excel on PC - encoding on PC is correct. **3)** This is incorrect code `new java.util.Locale(Locale.ENGLISH, Locale.US)`, because Locale don't have that constructor. **4)** I using fully qualified class name: `java.util.Locale` – Elvis Dec 08 '11 at 07:25
  • You need to double check the version of Java you're using. Perhaps the version you're using doesn't have the second parameter. Or you could upgrade the JRE. Try doing this: new java.util.Local(Locale.ENGLISH) ... OR ... new java.util.Local() – JustBeingHelpful Dec 08 '11 at 15:00
0

I do not know what to do :-(

new java.util.Local(Locale.ENGLISH) is incorrect constructor.

I used the jdk-6u29, now I using jdk-7u1.

In Eclipse I using:

  • "UTF-8" text file encoding
  • Compiler compilance level 1.6

I create same application on Android and PC, in them i using jexcel_android.jar

For the word Hello:

On PC byte[] b = a1.getString().getBytes("UTF-8");

  • [72, 101, 108, 108, 111]

On Android byte[] b = a1.getString().getBytes("UTF-8");

  • [-28, -96, -128, -26, -108, -128, -26, -80, -128, -26, -80, -128, -26, -68, -128]

If open created files on hex viewer

enter image description here

PC code below:

package ru.elvigl.jexcelwin;

import java.io.File;
import java.io.IOException;

import jxl.LabelCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class MainClass {

    final static File file = new File("D:/File.xls");

    public static void main(String[] args) 
            throws RowsExceededException, WriteException, IOException, BiffException {  

        writeFile();
        readFile();
    }

    private static void writeFile() 
            throws RowsExceededException, WriteException, IOException{

        WritableWorkbook workbook = Workbook.createWorkbook(file); 
        WritableSheet sheet = workbook.createSheet("First Sheet", 0); 

        Label label = new Label(0, 0, "Hello");
        sheet.addCell(label);

        workbook.write();
        workbook.close(); 

        System.out.println("File created");
    }

    private static void readFile() 
            throws BiffException, IOException{

        Workbook workbook = Workbook.getWorkbook(file); 
        Sheet sheet = workbook.getSheet(0); 

        LabelCell a1 = (LabelCell) sheet.getCell(0,0);

        byte[] b = a1.getString().getBytes("UTF-8");

        System.out.println(str);
    }
}

Android code below:

package ru.elvigl.hello;

import java.io.File;
import java.io.IOException;

import jxl.LabelCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class HellojExcelAPIActivity extends Activity {

    final File file = new File("/sdcard/Folder/File.xls");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void btnExport_Click(View v) throws IOException, RowsExceededException, WriteException {

        WritableWorkbook workbook = Workbook.createWorkbook(file); 
        WritableSheet sheet = workbook.createSheet("First Sheet", 0); 

        Label label = new Label(0, 0, "Hello");
        sheet.addCell(label);

        workbook.write();
        workbook.close(); 
    }

    public void btnImport_Click(View v) throws BiffException, IOException {

        Workbook workbook = Workbook.getWorkbook(file); 
        Sheet sheet = workbook.getSheet(0); 

        LabelCell a1 = (LabelCell) sheet.getCell(0,0);

        byte[] b = a1.getString().getBytes("UTF-8");
    }
}
Elvis
  • 831
  • 7
  • 9